diff --git a/sections/globals.pod b/sections/globals.pod index 9174d538..4d9c4115 100644 --- a/sections/globals.pod +++ b/sections/globals.pod @@ -28,7 +28,7 @@ right approach: =begin programlisting - my $file = do { B = <$fh> }; + my $file; { B; $file = <$fh> }; =end programlisting @@ -60,15 +60,7 @@ lexicals that have gone out of scope may reset C<$@>: =end programlisting Copy C<$@> I after catching an exception to preserve its contents. - -=begin tip Safer Exception Handling - -X> - -While Perl 5.14 has fixed some odd bugs related to exception handling, -C is still the most reliable mechanism to catch exceptions. - -=end tip +See also C instead (L). =head2 English Names diff --git a/sections/idioms.pod b/sections/idioms.pod index 19f27697..2cd3fc62 100644 --- a/sections/idioms.pod +++ b/sections/idioms.pod @@ -63,7 +63,7 @@ if it were a single argument: { B - my $ice_cream = get_ice_cream( $args{ice_cream} ); + my $dessert = get_ice_cream( $args{ice_cream} ); ... } @@ -146,7 +146,8 @@ fashion--in this case, two-element anonymous arrays: =begin programlisting - my @pairs = map { [ $_, $extensions{$_} ] } keys %extensions; + my @pairs = map { [ $_, $extensions{$_} ] } + keys %extensions; =end programlisting @@ -162,7 +163,8 @@ C takes the list of anonymous arrays and compares their second elements =begin programlisting - my @sorted_pairs = sort { $a->[1] cmp $b->[1] } @pairs; + my @sorted_pairs = sort { $a->[1] cmp $b->[1] } + @pairs; =end programlisting @@ -190,7 +192,8 @@ to a more usable form: =begin programlisting - my @formatted_exts = map { "$_->[1], ext. $_->[0]" } @sorted_pairs; + my @formatted_exts = map { "$_->[1], ext. $_->[0]" } + @sorted_pairs; =end programlisting @@ -249,11 +252,9 @@ files into a scalar in a single expression: my $file = do { local $/ = <$fh> }; # or - my $file = do { local $/; <$fh> }; # or - my $file; { local $/; $file = <$fh> }; =end programlisting @@ -322,10 +323,8 @@ Z The effective difference between a program and a module is in its intended use. Users invoke programs directly, while programs load modules after execution has -already begun. Yet a module is Perl code, just like a program except for how -you run it. - -Making a module executable is easy. So is making a program behave as a module +already begun. Yet a module is Perl code, in the same way that a program is. +Making a module executable is easy. So is making a program behave as a module (useful for testing parts of an existing program without formally making a module). All you need to do is to discover I Perl began to execute a piece of code. @@ -371,15 +370,14 @@ write: =begin programlisting - use Carp; + use Carp 'croak'; sub groom_monkeys { if (@_ != 2) { - croak 'Monkey grooming requires two monkeys!'; + croak 'Grooming requires two monkeys!'; } - ... } @@ -390,7 +388,7 @@ the check and deserve to be at the I of the expression: =begin programlisting - croak 'Monkey grooming requires two monkeys!' if @_ != 2; + croak 'Grooming requires two monkeys!' if @_ != 2; =end programlisting @@ -398,7 +396,8 @@ the check and deserve to be at the I of the expression: =begin programlisting - croak 'Monkey grooming requires two monkeys!' unless @_ == 2; + croak 'Grooming requires two monkeys!' + unless @_ == 2; =end programlisting @@ -413,8 +412,7 @@ Z X X -Many Perl 5 idioms rely on the language design where expressions evaluate to -values, as in: +Many Perl 5 idioms rely on the fact that expressions evaluate to values: =begin programlisting @@ -450,6 +448,13 @@ user name for a system account, you could write: =end programlisting +=begin tip /r in Perl 5.14 + +Perl 5.14 added the non-destructive substitution modifier C, so that you +can write C;>. + +=end tip + First, assign the value of C<$name> to C<$normalized_name>, as the parentheses affect the precedence so that assignment happens first. The assignment expression evaluates to the I C<$normalized_name>, so that that @@ -466,13 +471,6 @@ This technique works on other in-place modification operators: =end programlisting -=begin tip /r in Perl 5.14 - -Perl 5.14 added the non-destructive substitution modifier C, so that you -can write C;>. - -=end tip - =head2 Unary Coercions Z