Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Final page- and linebreaking changes.
  • Loading branch information
chromatic committed Jan 17, 2012
1 parent 04e6431 commit 46dbab7
Show file tree
Hide file tree
Showing 31 changed files with 440 additions and 598 deletions.
1 change: 0 additions & 1 deletion build/latex/copyright.tex
@@ -1,4 +1,3 @@
\chapter*{}
\thispagestyle{empty}

\huge{\booktitle}
Expand Down
7 changes: 3 additions & 4 deletions sections/advanced_oo.pod
Expand Up @@ -21,10 +21,9 @@ with code declared elsewhere?
X<OO; is-a>
X<OO; has-a>

Inheritance is but one of many tools. Although C<Car> may extend
C<Vehicle::Wheeled> (an I<is-a relationship>), it's likely better for C<Car> to
I<contain> contain several C<Wheel> objects as instance attributes (a I<has-a
relationship>).
Inheritance is but one of many tools. A C<Car> may extend C<Vehicle::Wheeled>
(an I<is-a relationship>), but C<Car> may better I<contain> several C<Wheel>
objects as instance attributes (a I<has-a relationship>).

Decomposing complex classes into smaller, focused entities (whether classes or
roles) improves encapsulation and reduces the possibility that any one class or
Expand Down
26 changes: 10 additions & 16 deletions sections/anonymous_functions.pod
Expand Up @@ -99,8 +99,11 @@ You may also see anonymous functions passed as function arguments:

X<anonymous functions; names>

X<CPAN; C<Sub::Identify>>

Given a reference to a function, you can determine whether the function is
named or anonymous with introspection:
named or anonymous with introspectionN<... or C<sub_name> from the CPAN module
C<Sub::Identify>.>:

=begin programlisting

Expand All @@ -123,15 +126,6 @@ named or anonymous with introspection:

=end programlisting

=begin tip Who Are You?

X<CPAN; C<Sub::Identify>>

The CPAN module C<Sub::Identify>'s C<sub_name> function simplifies this
introspection.

=end tip

The result may be surprising:

Called from ShowCaller::B<main>
Expand Down Expand Up @@ -183,7 +177,10 @@ Perl 5 allows the declaration of anonymous functions implicitly through the use
of prototypes (L<prototypes>). Though this feature exists nominally to enable
programmers to write their own syntax such as that for C<map> and C<eval>, an
interesting example is the use of I<delayed> functions that don't look like
functions. Consider the CPAN module C<Test::Fatal>:
functions.

Consider the CPAN module C<Test::Fatal>, which takes an anonymous function as
the first argument to its C<exception()> function:

=begin programlisting

Expand All @@ -200,8 +197,7 @@ functions. Consider the CPAN module C<Test::Fatal>:

=end programlisting

The function C<exception()> takes an anonymous function as its first argument.
These calls are equivalent to:
You might rewrite this more verbosely as:

=begin programlisting

Expand All @@ -210,9 +206,7 @@ These calls are equivalent to:

=end programlisting

... but are easier to read.

This syntax allows you to pass named functions by reference as well:
... or to pass named functions by reference:

=begin programlisting

Expand Down
178 changes: 77 additions & 101 deletions sections/arrays.pod
@@ -1,17 +1,15 @@
=head1 Arrays

Z<arrays>

X<arrays>
X<C<@> sigil>
X<sigils; C<@>>

Perl 5 I<arrays> are I<first-class> data structures--the language supports them
as a built-in data type--which store zero or more scalars. You can access
individual members of the array by integer indexes, and you can add or remove
elements at will.

X<C<@> sigil>
X<sigils; C<@>>

The C<@> sigil denotes an array. To declare an array:
elements at will. The C<@> sigil denotes an array. To declare an array:

=begin programlisting

Expand Down Expand Up @@ -58,37 +56,27 @@ addition, or boolean context) evaluates to the number of elements in the array:

=end programlisting

To get the I<index> of the final element of an array, subtract one from the
number of elements of the array (remember that array indexes start at 0):

=begin programlisting

my $first_index = 0;
my $last_index = @cats - 1;

say "My first cat has an index of $first_index, "
. "and my last cat has an index of $last_index."

=end programlisting

X<C<$#>; sigil>
X<sigils; C<$#>>

The less wieldy syntax C<$#cats> also evaluates to the last index of the array:
To get the I<index> of the final element of an array, subtract one from the
number of elements of the array (remember that array indexes start at 0) or use the unwieldy C<$#cats> syntax:

=begin programlisting

my $first_index = 0;
B<my $last_index = $#cats;>
my $last_index = @cats - 1;
# or
# my $last_index = $#cats;

say "My first cat has an index of $first_index, "
. "and my last cat has an index of $last_index."

=end programlisting

... though it's often clearer to use negative array indices instead. The last
element of an array is available at the index C<-1>. The second to last element
of the array is available at index C<-2>, and so on:
When the index matters less than the position of an element, use negative array
indices instead. The last element of an array is available at the index C<-1>.
The second to last element of the array is available at index C<-2>, and so on:

=begin programlisting

Expand Down Expand Up @@ -166,68 +154,6 @@ freshly-declared arrays start out empty.

=end tip

=head2 Array Slices

Z<array_slices>

X<arrays; slices>
X<slices; array>
X<C<@>; sigil>
X<sigils; C<@>>

The I<array slice> construct allows you to access elements of an array in list
context. Unlike scalar access of an array element, this indexing operation
takes a list of zero or more indices and uses the array sigil (C<@>):

=begin programlisting

my @youngest_cats = @cats[-1, -2];
my @oldest_cats = @cats[0 .. 2];
my @selected_cats = @cats[ @indexes ];

=end programlisting

Array slices are useful for assignment:

=begin programlisting

@users[ @replace_indices ] = @replace_users;

=end programlisting

A slice can contain zero or more elements--including one:

=begin programlisting

# single-element array slice; function call in I<list> context
@cats[-1] = get_more_cats();

# single-element array access; function call in I<scalar> context
$cats[-1] = get_more_cats();

=end programlisting

The only syntactic difference between an array slice of one element and the
scalar access of an array element is the leading sigil. The I<semantic>
difference is greater: an array slice always imposes list context. An array
slice evaluated in scalar context will produce a warning:

=begin screen

Scalar value @cats[1] better written as $cats[1]...

=end screen

An array slice imposes list context (L<context_philosophy>) on the expression
used as its index:

=begin programlisting

# function called in list context
my @hungry_cats = @cats[ get_cat_indices() ];

=end programlisting

=head2 Array Operations

Sometimes an array is more convenient as an ordered, mutable collection of
Expand Down Expand Up @@ -326,6 +252,67 @@ As of Perl 5.12, C<each> can iterate over an array by index and value:

=end programlisting

=head2 Array Slices

Z<array_slices>

X<arrays; slices>
X<slices; array>
X<C<@>; sigil>
X<sigils; C<@>>

The I<array slice> construct allows you to access elements of an array in list
context. Unlike scalar access of an array element, this indexing operation
takes a list of zero or more indices and uses the array sigil (C<@>):

=begin programlisting

my @youngest_cats = @cats[-1, -2];
my @oldest_cats = @cats[0 .. 2];
my @selected_cats = @cats[ @indexes ];

=end programlisting

Array slices are useful for assignment:

=begin programlisting

@users[ @replace_indices ] = @replace_users;

=end programlisting

A slice can contain zero or more elements--including one:

=begin programlisting

# single-element array slice; I<list> context
@cats[-1] = get_more_cats();

# single-element array access; I<scalar> context
$cats[-1] = get_more_cats();

=end programlisting

The only syntactic difference between an array slice of one element and the
scalar access of an array element is the leading sigil. The I<semantic>
difference is greater: an array slice always imposes list context. An array
slice evaluated in scalar context will produce a warning:

=begin screen

Scalar value @cats[1] better written as $cats[1]...

=end screen

An array slice imposes list context on the expression used as its index:

=begin programlisting

# function called in list context
my @hungry_cats = @cats[ get_cat_indices() ];

=end programlisting

=head2 Arrays and Context

X<list context; arrays>
Expand Down Expand Up @@ -400,24 +387,13 @@ C<$LIST_SEPARATOR>. Thus:

=end programlisting

Temporarily localizing and assigning another value to C<$"> for debugging
purposes is very handyN<Credit goes to Mark Jason Dominus for demonstrating its
efficacy.>:
Localize C<$"> with a delimiter to ease your debuggingN<Credit goes to Mark Jason Dominus for this technique.>:

=begin programlisting

# what's in this array again?
{
local $" = ')(';
say "(@sweet_treats)";
}
local $" = ')(';
say "(@sweet_treats)";
B<(pie)(cake)(doughnuts)(cookies)(raisin bread)>

=end programlisting

... which produces the result:

=begin screen

(pie)(cake)(doughnuts)(cookies)(raisin bread)

=end screen
8 changes: 3 additions & 5 deletions sections/attributes.pod
Expand Up @@ -36,11 +36,9 @@ CPAN uses such parametric arguments to good effect:

=begin programlisting

sub setup_tests :Test(setup) { ... }

sub test_monkey_creation :Test(10) { ... }

sub shutdown_tests :Test(teardown) { ... }
sub setup_tests :Test(setup) { ... }
sub test_monkey_creation :Test(10) { ... }
sub shutdown_tests :Test(teardown) { ... }

=end programlisting

Expand Down

0 comments on commit 46dbab7

Please sign in to comment.