Skip to content

Commit

Permalink
Improved line and pagebreaking for chapter 10.
Browse files Browse the repository at this point in the history
  • Loading branch information
chromatic committed Dec 8, 2011
1 parent 5e958de commit 3cabfec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 34 deletions.
12 changes: 2 additions & 10 deletions sections/globals.pod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ right approach:

=begin programlisting

my $file = do { B<local $/> = <$fh> };
my $file; { B<local $/>; $file = <$fh> };

=end programlisting

Expand Down Expand Up @@ -60,15 +60,7 @@ lexicals that have gone out of scope may reset C<$@>:
=end programlisting

Copy C<$@> I<immediately> after catching an exception to preserve its contents.

=begin tip Safer Exception Handling

X<CPAN; C<Try::Tiny>>

While Perl 5.14 has fixed some odd bugs related to exception handling,
C<Try::Tiny> is still the most reliable mechanism to catch exceptions.

=end tip
See also C<Try::Tiny> instead (L<exception_caveats>).

=head2 English Names

Expand Down
46 changes: 22 additions & 24 deletions sections/idioms.pod
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ if it were a single argument:
{
B<my %args = @_;>

my $ice_cream = get_ice_cream( $args{ice_cream} );
my $dessert = get_ice_cream( $args{ice_cream} );
...
}

Expand Down Expand Up @@ -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

Expand All @@ -162,7 +163,8 @@ C<sort> 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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -322,10 +323,8 @@ Z<controlled_execution>

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<how> Perl began to execute a piece
of code.
Expand Down Expand Up @@ -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!';
}

...
}

Expand All @@ -390,15 +388,16 @@ the check and deserve to be at the I<start> of the expression:

=begin programlisting

croak 'Monkey grooming requires two monkeys!' if @_ != 2;
croak 'Grooming requires two monkeys!' if @_ != 2;

=end programlisting

... which may read more simply as:

=begin programlisting

croak 'Monkey grooming requires two monkeys!' unless @_ == 2;
croak 'Grooming requires two monkeys!'
unless @_ == 2;

=end programlisting

Expand All @@ -413,8 +412,7 @@ Z<regex_en_passant>
X<regex; modification>
X<regex; substitution>

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

Expand Down Expand Up @@ -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</r>, so that you
can write C<my $normalized_name = $name =~ tr/A-Za-z//dcB<r>;>.

=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<variable> C<$normalized_name>, so that that
Expand All @@ -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</r>, so that you
can write C<my $normalized_name = $name =~ tr/A-Za-z//dcB<r>;>.

=end tip

=head2 Unary Coercions

Z<unary_coercions>
Expand Down

0 comments on commit 3cabfec

Please sign in to comment.