diff --git a/sections/exceptions.pod b/sections/exceptions.pod index 85d0f92d..a81e2f06 100644 --- a/sections/exceptions.pod +++ b/sections/exceptions.pod @@ -80,8 +80,9 @@ X> An exception handler is a blunt tool. It will catch all exceptions in its dynamic scope. To check which exception you've caught (or if you've caught an -exception at all), check the value of C<$@>. As C<$@> is a I variable, -you should Cize it before you attempt to catch an exception: +exception at all), check the value of C<$@>. Be sure to Cize C<$@> +before you attempt to catch an exception; remember that C<$@> is a global +variable: =begin programlisting @@ -101,14 +102,15 @@ Copy C<$@> to a lexical variable immediately to avoid the possibility of subsequent code clobbering the global variable C<$@>. You never know what else has used an C block elsewhere and reset C<$@>. -C<$@> usually contains a string describing the exception. You can inspect its -contents to see whether you can handle the exception: +C<$@> usually contains a string describing the exception. Inspect its contents +to see whether you can handle the exception: =begin programlisting if (my $exception = $@) { - die $exception unless $exception =~ /^Can't open logging file/; + die $exception + unless $exception =~ /^Can't open logging/; $fh = log_to_syslog(); } @@ -118,15 +120,20 @@ Rethrow an exception by calling C again. Pass the existing exception or a new one as necessary. X + +Applying regular expressions to string exceptions can be fragile, because error +messages may change over time. This includes the core exceptions that Perl +itself throws. Fortunately, you may also provide a reference--even a blessed +reference--to C. This allows you to provide much more information in your +exception: line numbers, files, and other debugging information. Retrieving +this information from something structured is much easier than parsing it out +of a string. Catch these exceptions as you would any other exception. + X> X> -You may C with either a string or a reference--even an I. Applying -regular expressions to string exceptions can be fragile, because error messages -may change over time, even those built from Perl itself. Objects are more -robust; they contain more information. You can throw any object as an -exception, but consider using the CPAN distribution C to -define your own class or classes of exceptions: +The CPAN distribution C makes creating and using exception +objects easy: =begin programlisting @@ -140,27 +147,28 @@ define your own class or classes of exceptions: sub cage_open { my $self = shift; - Zoo::AnimalEscaped->throw unless $self->contains_animal; + Zoo::AnimalEscaped->throw + unless $self->contains_animal; ... } sub breakroom_open { my $self = shift; - Zoo::HandlerEscaped->throw unless $self->contains_handler; + Zoo::HandlerEscaped->throw + unless $self->contains_handler; ... } =end programlisting -Catch these exceptions as you would any other exception. - =head2 Exception Caveats Z X +Though throwing exceptions is relatively simple, catching them is less so. Using C<$@> correctly requires you to navigate several subtle risks: =over 4 @@ -181,16 +189,20 @@ change C<$@> X> X> -Perl 5.14 improves the safety of exception handling. The C CPAN -distribution improves the safety of exception handling I the syntax. It's -easy to use: +Perl 5.14 fixed some of these issues. Granted, they occur very rarely, but +they're often difficult to diagnose and to fix. The C CPAN +distribution improves the safety of exception handling I the syntaxN helped inspire improvements to Perl 5.14's exception +handling.>. + +C is easy to use: =begin programlisting use Try::Tiny; my $fh = try { open_log_file( 'monkeytown.log' ) } - catch { log_exception( "Something went wrong: '$_'" }; + catch { log_exception( $_ ) }; =end programlisting diff --git a/sections/pragmas.pod b/sections/pragmas.pod index 898cc1b2..19d54adc 100644 --- a/sections/pragmas.pod +++ b/sections/pragmas.pod @@ -15,22 +15,22 @@ modules. X -Pragmas work by exporting specific behavior or information into their caller -scopes--lexical scopes. Just as declaring a lexical variable makes a symbol +Pragmas work by exporting specific behavior or information into the lexical +scopes of their callers. Just as declaring a lexical variable makes a symbol name available within a scope, using a pragma makes its behavior effective within that scope: =begin programlisting { - # $lexical is B visible; strict is B in effect + # $lexical B visible; strict B in effect { use strict; my $lexical = 'available here'; # $lexical B visible; strict B in effect ... } - # $lexical is again B visible; strict is B in effect + # $lexical again invisible; strict B in effect } =end programlisting @@ -61,7 +61,7 @@ behavior: =begin programlisting - # require variable declarations; prohibit bareword function names + # require variable declarations, prohibit barewords use strict qw( subs vars ); =end programlisting @@ -69,8 +69,13 @@ behavior: X X> -Disable all or part of a pragma with the C builtin. This also respects -lexical scoping: +Pragmas have lexical effects, and sometimes you need to I all or part +of those effects within a further nested lexical scope. The C builtin +performs an unimport (L), which with well-behaved pragmas undoes +their effects. + +For example, to disable the protection of C when you need to do +something symbolic: =begin programlisting @@ -137,8 +142,9 @@ X Perl 5.10.0 added the ability to write your own lexical pragmas in pure Perl code. C explains how to do so, while the explanation of -C<$^H> in C explains how the feature works. The CPAN has begun -to gather useful pragmas, including: +C<$^H> in C explains how the feature works. + +The CPAN has begun to gather useful pragmas. Some of the most useful are: X> X> diff --git a/sections/style.pod b/sections/style.pod index 5daff698..62cd503d 100644 --- a/sections/style.pod +++ b/sections/style.pod @@ -91,7 +91,7 @@ documenting, packaging, testing, and distributing your code. (U), browse PerlMonks (U), and otherwise immerse yourself in the communityN.>. Read code and try to -write solutions--even if you never post them, this is a great opportunity to +answer questions--even if you never post them, this is a great opportunity to learn. =back