diff --git a/sections/arrays.pod b/sections/arrays.pod index e2e8f1a3..d5832602 100644 --- a/sections/arrays.pod +++ b/sections/arrays.pod @@ -115,6 +115,7 @@ Assign to individual positions in an array directly by index: $cats[0] = 'Daisy'; $cats[1] = 'Petunia'; $cats[4] = 'Brad'; + $cats[5] = 'Choco'; =end programlisting @@ -127,17 +128,13 @@ As an assignment shortcut, initialize an array from a list: =begin programlisting - my @cats = ( 'Daisy', 'Petunia', 'Tuxedo', 'Jack', 'Brad' ); + my @cats = ( 'Daisy', 'Petunia', 'Tuxedo', ... ); =end programlisting -=begin sidebar - -Remember that these parentheses I create a list. Without parentheses, -this would assign C as the first and only element of the array, due to -operator precedence (L). - -=end sidebar +... but remember that these parentheses I create a list. Without +parentheses, this would assign C as the first and only element of the +array, due to operator precedence (L). Any expression which produces a list in list context can assign to an array: @@ -217,7 +214,7 @@ slice evaluated in scalar context will produce a warning: =begin screen - Scalar value @cats[1] better written as $cats[1] at... + Scalar value @cats[1] better written as $cats[1]... =end screen @@ -281,7 +278,7 @@ the start of an array, respectively: =begin programlisting # expand our culinary horizons - unshift @meals, qw( tofu curry spanakopita taquitos ); + unshift @meals, qw( tofu spanakopita taquitos ); # rethink that whole soy idea shift @meals; @@ -292,12 +289,8 @@ C prepends a list of elements to the start of the array and returns the new number of elements in the array. C removes and returns the first element of the array. -=begin tip Zero, One, or Many - Few programs use the return values of C and C. -=end tip - X> X> @@ -367,7 +360,9 @@ nested arrays in Perl 5: =begin programlisting # creates a single array, not an array of arrays - my @array_of_arrays = ( 1 .. 10, ( 11 .. 20, ( 21 .. 30 ) ) ); + my @numbers = ( 1 .. 10, + ( 11 .. 20, + ( 21 .. 30 ) ) ); =end programlisting @@ -376,7 +371,7 @@ nested arrays in Perl 5: =begin programlisting # creates a single array, not an array of arrays - my @array_of_arrays = 1 .. 30; + my @numbers = 1 .. 30; =end programlisting @@ -400,7 +395,8 @@ C<$LIST_SEPARATOR>. Thus: my @alphabet = 'a' .. 'z'; say "[@alphabet]"; - B<[a b c d e f g h i j k l m n o p q r s t u v w x y z]> + B<[a b c d e f g h i j k l m> + B =end programlisting diff --git a/sections/coercion.pod b/sections/coercion.pod index ec3d9079..6c73ad04 100644 --- a/sections/coercion.pod +++ b/sections/coercion.pod @@ -94,12 +94,12 @@ manipulating nested data structures (L): my %users; - $users{Bradley}{id} = 228; - $users{Jack}{id} = 229; + $users{Brad}{id} = 228; + $users{Jack}{id} = 229; =end programlisting -Although the hash never contained values for C and C, Perl +Although the hash never contained values for C and C, Perl helpfully created hash references for them, then assigned each a key/value pair keyed on C. diff --git a/sections/control_flow.pod b/sections/control_flow.pod index cf49d1f1..6a108c71 100644 --- a/sections/control_flow.pod +++ b/sections/control_flow.pod @@ -232,7 +232,9 @@ produces one of two alternatives: =begin programlisting - my $time_suffix = after_noon($time) ? 'afternoon' : 'morning'; + my $time_suffix = after_noon($time) + ? 'afternoon' + : 'morning'; =end programlisting @@ -248,7 +250,8 @@ select between alternative I, not only values: =begin programlisting - push @{ rand() > 0.5 ? \@red_team : \@blue_team }, Player->new; + push @{ rand() > 0.5 ? \@red_team : \@blue_team }, + Player->new; =end programlisting @@ -269,10 +272,8 @@ example: =begin programlisting - use Test::More; - - say "Both true!" if ok( 1, 'first subexpression' ) - && ok( 1, 'second subexpression' ); + say "Both true!" if ok( 1, 'subexpression one' ) + && ok( 1, 'subexpression two' ); done_testing(); @@ -283,8 +284,8 @@ evaluating the first argument, so this code prints: =begin screen - ok 1 - first subexpression - ok 2 - second subexpression + ok 1 - subexpression one + ok 2 - subexpression two Both true! =end screen @@ -296,8 +297,8 @@ subexpressions, as the entire expression could not succeed: =begin programlisting - say "Both true!" if ok( 0, 'first subexpression' ) - && ok( 1, 'second subexpression' ); + say "Both true!" if ok( 0, 'subexpression one' ) + && ok( 1, 'subexpression two' ); =end programlisting @@ -305,7 +306,7 @@ This example prints: =begin screen - not ok 1 - first subexpression + not ok 1 - subexpression one =end screen @@ -315,8 +316,8 @@ operations: =begin programlisting - say "Either true!" if ok( 1, 'first subexpression' ) - || ok( 1, 'second subexpression' ); + say "Either true!" if ok( 1, 'subexpression one' ) + || ok( 1, 'subexpression two' ); =end programlisting @@ -324,7 +325,7 @@ This example prints: =begin screen - ok 1 - first subexpression + ok 1 - subexpression one Either true! =end screen @@ -340,8 +341,8 @@ using an undefined value might raise a warning: =begin programlisting - my $barbeque; - if (defined $barbeque and $barbeque eq 'pork shoulder') { ... } + my $bbq; + if (defined $bbq and $bbq eq 'brisket') { ... } =end programlisting @@ -386,12 +387,11 @@ to specify what your own data types produce when evaluated in various contexts. Z -Perl provides several directives for looping and iteration. - X> X> -The I-style loop evaluates an expression which produces a list and +Perl provides several directives for looping and iteration. The +I-style loop evaluates an expression which produces a list and executes a statement or block until it has consumed that list: =begin programlisting @@ -408,21 +408,18 @@ ten inclusive. The C directive loops over them, setting the topic variable C<$_> (L) to each in turn. Perl executes the block for each integer and prints the squares of the integers. -=begin sidebar +=begin tip C versus C X> X> -Perl treats the builtins C and C interchangeably. The remainder -of the syntax of the loop determines the behavior of the loop. Though -experienced Perl programmers tend to refer to the loop with automatic iteration -as a C loop, you can use the keyword C safely and clearly any -place you might want to write C. +Many Perl programmers refer to iteration as C loops, but Perl treats +the names C and C interchangeably. The subsequent code determines +the type and behavior of the loop. -=end sidebar +=end tip -Like C and C, the C loop has a postfix form suitable for -simple expressions: +Like C and C, this loop has a postfix form: =begin programlisting @@ -441,10 +438,11 @@ A C loop may use a named variable instead of the topic: =end programlisting -The scope of the variable C<$i> is only valid I the loop. Perl will set -this lexical to the value of each item in the iteration. Perl will not modify -the topic variable (C<$_>). If you have declared a lexical C<$i> in an outer -scope, its value will persist outside the loop: +When a C loop uses an iterator variable, the variable scope is I +the loop. Perl will set this lexical to the value of each item in the +iteration. Perl will not modify the topic variable (C<$_>). If you have +declared a lexical C<$i> in an outer scope, its value will persist outside the +loop: =begin programlisting @@ -455,7 +453,7 @@ scope, its value will persist outside the loop: say "$i * $i = ", $i * $i; } - is( $i, 'cow', 'Lexical variable not overwritten in outer scope' ); + is( $i, 'cow', 'Value preserved in outer scope' ); =end programlisting @@ -472,7 +470,7 @@ their scope.>: say "$i * $i = ", $i * $i; } - is( $i, 'horse', 'Lexical variable still not overwritten in outer scope' ); + is( $i, 'horse', 'Value preserved in outer scope' ); =end programlisting @@ -501,7 +499,7 @@ value in place: =end programlisting -This aliasing also works with the block style C loop: +This aliasing also works with the block style C loop: =begin programlisting @@ -642,18 +640,15 @@ stops. The final subexpression executes after each iteration of the loop body. =begin programlisting - # declared outside to avoid declaration in conditional - my $i; - for ( # loop initialization subexpression - say 'Initializing' and $i = 0; + say 'Initializing', my $i = 0; # conditional comparison subexpression say "Iteration: $i" and $i < 10; # iteration ending subexpression - say 'Incrementing $i' and $i++ + say 'Incrementing ' . $i++ ) { say "$i * $i = ", $i * $i; @@ -662,8 +657,8 @@ stops. The final subexpression executes after each iteration of the loop body. =end programlisting Note the lack of a semicolon after the final subexpression as well as the use -of the low-precedence C; this syntax is surprisingly finicky. When -possible, prefer the C style loop to the C loop. +of the comma operator and low-precedence C; this syntax is surprisingly +finicky. When possible, prefer the C-style loop to the C loop. All three subexpressions are optional. An infinite C loop might be: @@ -714,8 +709,6 @@ C<@values> array by modifying the array with each loop iteration: =end programlisting -=begin sidebar - Modifying C<@values> inside of the C condition check also works, but it has some subtleties related to the truthiness of each value. @@ -732,8 +725,6 @@ This loop will exit as soon as it reaches an element that evaluates to a false value, not necessarily when it has exhausted the array. That may be the desired behavior, but is often surprising to novices. -=end sidebar - X> The I loop reverses the sense of the test of the C loop. @@ -791,16 +782,10 @@ novices forget this. =end tip -Both C and C have postfix forms. Another infinite loop is: - -=begin programlisting - - 1 while 1; - -=end programlisting - -Any single expression is suitable for a postfix C or C, such as -the classic "Hello, world!" example from 8-bit computers of the early 1980s: +Both C and C have postfix forms, such as the infinite loop C<1 +while 1;>. Any single expression is suitable for a postfix C or +C, including the classic "Hello, world!" example from 8-bit computers of +the early 1980s: =begin programlisting @@ -1046,10 +1031,7 @@ an expression to the topic variable and introduces a block: =begin programlisting - given ($name) - { - ... - } + given ($name) { ... } =end programlisting @@ -1060,7 +1042,7 @@ expression in scalar context, and always assigns to the topic variable: given (my $username = find_user()) { - is( $username, $_, 'topic assignment happens automatically' ); + is( $username, $_, 'topic auto-assignment' ); } =end programlisting @@ -1076,11 +1058,7 @@ C also lexicalizes the topic variable: say; } - sub mouse_to_man - { - $_ = shift; - s/mouse/man/; - } + sub mouse_to_man { s/mouse/man/ } =end programlisting @@ -1094,7 +1072,8 @@ Scissors game: =begin programlisting - my @options = ( \&rock, \&paper, \&scissors ); + my @options = ( \&rock, \&paper, \&scissors ); + my $confused = "I don't understand your move."; do { @@ -1113,7 +1092,7 @@ Scissors game: when (/paper/) { say 'You win!' }; when (/rock/) { say 'We tie!' }; when (/scissors/) { say 'I win!' }; - default { say "I don't understand your move" }; + default { say $confused }; } } @@ -1126,7 +1105,7 @@ Scissors game: when (/paper/) { say 'We tie!' }; when (/rock/) { say 'I win!' }; when (/scissors/) { say 'You win!' }; - default { say "I don't understand your move" }; + default { say $confused }; } } @@ -1139,7 +1118,7 @@ Scissors game: when (/paper/) { say 'I win!' }; when (/rock/) { say 'You win!' }; when (/scissors/) { say 'We tie!' }; - default { say "I don't understand your move" }; + default { say $confused }; } } @@ -1198,8 +1177,8 @@ X> The builtin C operator has a form which calls a function as if the current function were never called, essentially erasing the bookkeeping for the -new function call. The syntax is ugly, and confuses people who've heard "Never -use C", but it works: +new function call. The ugly syntax confuses people who've heard "Never use +C", but it works: =begin programlisting diff --git a/sections/hashes.pod b/sections/hashes.pod index d47357e3..dec3572d 100644 --- a/sections/hashes.pod +++ b/sections/hashes.pod @@ -227,8 +227,10 @@ contains the given key: Utako => 'Cantor Hotel, Room 1', ); - say "Have Leonardo's address" if exists $addresses{Leonardo}; - say "Have Warnie's address" if exists $addresses{Warnie}; + say "Have Leonardo's address" + if exists $addresses{Leonardo}; + say "Have Warnie's address" + if exists $addresses{Warnie}; =end programlisting @@ -241,7 +243,8 @@ may exist with a value even if that value evaluates to a boolean false my %false_key_value = ( 0 => '' ); ok( %false_key_value, - 'hash containing false key & value should evaluate to a true value' ); + 'hash containing false key & value + should evaluate to a true value' ); =end programlisting @@ -323,7 +326,8 @@ latter will begin partway through the hash. During such iteration, beware not to call any function which may itself try to iterate over the hash with C. -Reset a hash's iterator with C or C in void context: +In practice this occurs rarely, but reset a hash's iterator with C or +C in void context when you need it: =begin programlisting @@ -337,13 +341,6 @@ Reset a hash's iterator with C or C in void context: =end programlisting -=begin tip Reset Your Iterators - -The single hash iterator is a well lamented caveat, but it doesn't come up as -often as you might expect. Be cautious, but use C when you need it. - -=end tip - =head2 Hash Slices X @@ -363,14 +360,21 @@ This is equivalent to the initialization: =begin programlisting - my %cats = map { $_ => 1 } qw( Jack Brad Mars Grumpy ); + my %cats = map { $_ => 1 } + qw( Jack Brad Mars Grumpy ); =end programlisting ... except that the hash slice initialization does not I the existing contents of the hash. -To retrieve multiple values from a hash with a slice: +X; sigil> +X> + +Hash slices also allow you to retrieve multiple values from a hash in a single +operation. As with array slices, the sigil of the hash changes to indicate list +context. The use of the curly braces indicates keyed access and makes the hash +unambiguous: =begin programlisting @@ -378,13 +382,6 @@ To retrieve multiple values from a hash with a slice: =end programlisting -X; sigil> -X> - -As with array slices, the sigil of the hash changes to indicate list context. -The use of the curly braces indicates keyed access and makes the hash -unambiguous. - Hash slices make it easy to merge two hashes: =begin programlisting @@ -392,7 +389,8 @@ Hash slices make it easy to merge two hashes: my %addresses = ( ... ); my %canada_addresses = ( ... ); - @addresses{ keys %canada_addresses } = values %canada_addresses; + @addresses{ keys %canada_addresses } + = values %canada_addresses; =end programlisting @@ -418,10 +416,12 @@ both would themselves evaluate to false values in a boolean context. ok( ! %empty, 'empty hash should evaluate to false' ); my %false_key = ( 0 => 'true value' ); - ok( %false_key, 'hash containing false key should evaluate to true' ); + ok( %false_key, 'hash containing false key + should evaluate to true' ); my %false_value = ( 'true key' => 0 ); - ok( %false_value, 'hash containing false value should evaluate to true' ); + ok( %false_value, 'hash containing false value + should evaluate to true' ); done_testing(); @@ -560,7 +560,8 @@ function arguments: ... } - make_sundae( flavor => 'Lemon Burst', topping => 'cookie bits' ); + make_sundae( flavor => 'Lemon Burst', + topping => 'cookie bits' ); =end programlisting diff --git a/sections/names.pod b/sections/names.pod index af4e4fc0..8abb72a0 100644 --- a/sections/names.pod +++ b/sections/names.pod @@ -68,9 +68,8 @@ X I always have a leading I (or symbol) which indicates the type of the variable's value. I (L) use the -dollar sign (C<$>) character. I (L) use the at sign -(C<@>) character. I (L) use the percent sign (C<%>) -character: +dollar sign (C<$>). I (L) use the at sign (C<@>). +I (L) use the percent sign (C<%>): =begin programlisting @@ -90,8 +89,7 @@ different types: =end programlisting -Again, names exist to help programmers. Perl won't get confused. People reading -this code will. +Though Perl won't get confused, people reading this code will. X @@ -146,26 +144,28 @@ X X Perl provides a mechanism to group similar functions and variables into their -own unique named spaces--I (L). A namespace is a -collection of one or more names joined by double colons (C<::>), such that -C refers to a logical collection of related variables and -functions, such as C and C. +own unique named spaces--I (L). A namespace is a named +collection of symbols. Perl allows multi-level namespaces, with names joined by +double colons (C<::>), where C refers to a logical +collection of related variables and functions, such as C and +C. Within a namespace, you may use the short name of its members. Outside of the -namespace, refer to a member using its I, which includes -the namespace, as in C. +namespace, refer to a member using its I. That is, within +C, C refers to the same function as +does C outside of the namespace. While standard naming rules apply to package names, by convention user-defined packages all start with uppercase letters. The Perl core reserves lowercase package names for core pragmas (L), such as C and C. This is a policy enforced primarily by community guidelines. -Namespaces do not nest in Perl 5. The relationship between -C and C is only a -storage mechanism, with no further implications on the relationships between -parent and child or sibling packages. When Perl looks up a symbol in +All namespaces in Perl 5 are globally visible. When Perl looks up a symbol in C, it looks in the C symbol table for a symbol representing the C namespace, then in there for the -C namespace, and so on. Only a programmer can make I -relationships between entities obvious--by choosing good names and organizing -them well. +C namespace, and so on. The C is visible from outside of +the C namespace. The nesting of the former within the latter is +only a storage mechanism, and implies nothing further about relationships +between parent and child or sibling packages. Only a programmer can make +I relationships between entities obvious--by choosing good names and +organizing them well. diff --git a/sections/nested_data_structures.pod b/sections/nested_data_structures.pod index 36c8b782..020d0ef7 100644 --- a/sections/nested_data_structures.pod +++ b/sections/nested_data_structures.pod @@ -12,26 +12,26 @@ possible through the use of references. =head2 Declaring Nested Data Structures -A simple declaration of an array of arrays might be: +Use the anonymous reference declaration syntax to declare a nested data +structure: =begin programlisting - my @famous_triplets = ( + my @famous_triplets = + ( [qw( eenie miney moe )], [qw( huey dewey louie )], [qw( duck duck goose )], ); -=end programlisting - -... and a simple declaration of a hash of hashes might be: - -=begin programlisting - - my %meals = ( - breakfast => { entree => 'eggs', side => 'hash browns' }, - lunch => { entree => 'panini', side => 'apple' }, - dinner => { entree => 'steak', side => 'avocado salad' }, + my %meals = + ( + breakfast => { entree => 'eggs', + side => 'hash browns' }, + lunch => { entree => 'panini', + side => 'apple' }, + dinner => { entree => 'steak', + side => 'avocado salad' }, ); =end programlisting @@ -66,12 +66,12 @@ arrow is superfluous. You may omit it for clarity: =end programlisting -=begin sidebar +=begin tip Save Your Arrows for Functions The arrow invocation syntax is clearest only in the case of invoking a function reference stored in a nested data structure. -=end sidebar +=end tip Use disambiguation blocks to access components of nested data structures as if they were first-class arrays or hashes: @@ -87,7 +87,8 @@ they were first-class arrays or hashes: =begin programlisting - my ($entree, $side) = @{ $meals{breakfast} }{qw( entree side )}; + my ($entree, $side) = @{ $meals{breakfast} } + {qw( entree side )}; =end programlisting @@ -96,8 +97,8 @@ Use temporary variables to clarify: =begin programlisting - my $breakfast_ref = $meals{breakfast}; - my ($entree, $side) = @$breakfast_ref{qw( entree side )}; + my $meal_ref = $meals{breakfast}; + my ($entree, $side) = @$meal_ref{qw( entree side )}; =end programlisting @@ -108,7 +109,8 @@ intermediate reference: =begin programlisting - my ($entree, $side) = @{ $_ }{qw( entree side )} for $meals{breakfast}; + my ($entree, $side) = @{ $_ }{qw( entree side )} + for $meals{breakfast}; =end programlisting @@ -153,8 +155,8 @@ X> This useful behavior is I. While it reduces the initialization code of nested data structures, it cannot distinguish between the honest intent to create missing elements in nested data structures and -typos. The CPAN's C pragma (L) lets you disable -autovivification in a lexical scope for specific types of operations. +typos. The C pragma (L) from the CPAN lets you +disable autovivification in a lexical scope for specific types of operations. You may wonder at the contradiction between taking advantage of autovivification while enabling Cures. The question is one of balance. @@ -218,9 +220,9 @@ children: =begin programlisting - my $alice = { mother => '', father => '', children => [] }; - my $robert = { mother => '', father => '', children => [] }; - my $cianne = { mother => $alice, father => $robert, children => [] }; + my $alice = { mother => '', father => '' }; + my $robert = { mother => '', father => '' }; + my $cianne = { mother => $alice, father => $robert }; push @{ $alice->{children} }, $cianne; push @{ $robert->{children} }, $cianne; @@ -248,9 +250,9 @@ from increasing: use Scalar::Util 'weaken'; - my $alice = { mother => '', father => '', children => [] }; - my $robert = { mother => '', father => '', children => [] }; - my $cianne = { mother => $alice, father => $robert, children => [] }; + my $alice = { mother => '', father => '' }; + my $robert = { mother => '', father => '' }; + my $cianne = { mother => $alice, father => $robert }; push @{ $alice->{children} }, $cianne; push @{ $robert->{children} }, $cianne; diff --git a/sections/packages.pod b/sections/packages.pod index 4a00b6a8..53b0621c 100644 --- a/sections/packages.pod +++ b/sections/packages.pod @@ -32,9 +32,9 @@ X All global variables and functions declared or referred to after the package declaration refer to symbols within the C namespace. You can refer to the C<@boxes> variable from the C
namespace only by its I name, C<@MyCode::boxes>. Similarly, you can call the C -function only by C. A fully qualified name includes a -complete package name. +qualified> name of C<@MyCode::boxes>. A fully qualified name includes a +complete package name, so you can call the C function only by +C. X diff --git a/sections/references.pod b/sections/references.pod index 83acc8ac..2ab2a3ea 100644 --- a/sections/references.pod +++ b/sections/references.pod @@ -230,7 +230,7 @@ of values with square brackets: =begin programlisting - my $suits_ref = [qw( Monkeys Robots Dinosaurs Cheese )]; + my $suits_ref = [qw( Monkeys Robots Dinos Cheese )]; =end programlisting @@ -385,10 +385,8 @@ X> X> The same goes for the array operators C, C, C, C, -C, C, and C. - -Similarly, Perl can also automatically dereference hash references used with -C, C, and C. +C, C, and C and the hash operators C, C, and +C. If the reference provided is not of the proper type--if it does not dereference properly--Perl will throw an exception. While this may seem more dangerous than @@ -456,6 +454,14 @@ function reference with the dereferencing arrow: =end programlisting +=begin tip Perl 4 Function Calls + +An alternate invocation syntax for function references uses the function sigil +(C<&>) instead of the dereferencing arrow. Avoid this syntax; it has subtle +implications for parsing and argument passing. + +=end tip + Think of the empty parentheses as denoting an invocation dereferencing operation in the same way that square brackets indicate an indexed lookup and curly brackets cause a hash lookup. Pass arguments to the function within the @@ -477,14 +483,6 @@ is useful when you've already looked up the method (L): =end programlisting -=begin sidebar - -An alternate invocation syntax for function references uses the function sigil -(C<&>) instead of the dereferencing arrow. Avoid this syntax; it has subtle -implications for parsing and argument passing. - -=end sidebar - =head2 Filehandle References Z @@ -495,14 +493,12 @@ X> X> When you use C's (and C's) lexical filehandle form, you deal -with filehandle references. Internally, these filehandles are objects of the -class C. You can call methods on them directly. As of Perl 5.14, this -is as simple as: +with filehandle references. Internally, these filehandles are C +objects. You can call methods on them directly. As of Perl 5.14, this is as +simple as: =begin programlisting - use autodie; - open my $out_fh, '>', 'output_file.txt'; $out_fh->say( 'Have some text!' ); @@ -516,9 +512,11 @@ You may see old code which takes references to typeglobs, such as: =begin programlisting - my $fh = do { + my $fh = do + { local *FH; - open FH, "> $file" or die "Can't write to '$file': $!\n"; + open FH, "> $file" + or die "Can't write to '$file': $!\n"; B<\*FH>; }; @@ -530,12 +528,19 @@ references to package-global filehandles such as C, C, C, or C--but these are all global names anyhow. Prefer lexical filehandles when possible. With the benefit of explicit scoping, -lexical filehandles allow you to manage the lifespan of filehandles. This is a -nice feature of how Perl 5 manages memory. +lexical filehandles allow you to manage the lifespan of filehandles as a +feature of how Perl 5 manages memory. =head2 Reference Counts Z +X + +Perl 5 uses a memory management technique known as I. +Every Perl value has a counter attached. Perl increases this counter every time +something takes a reference to the value, whether implicitly or explicitly. +Perl decreases that counter every time a reference goes away. When the counter +reaches zero, Perl can safely recycle that value. X @@ -544,9 +549,6 @@ does Perl know when it's safe to close the file opened in this inner scope: =begin programlisting - use autodie; - use IO::Handle; - sub show_off_scope { say 'file not open'; @@ -561,14 +563,6 @@ does Perl know when it's safe to close the file opened in this inner scope: =end programlisting -X - -Perl 5 uses a memory management technique known as I. -Every Perl value has a counter attached. Perl increases this counter every time -something takes a reference to the value, whether implicitly or explicitly. -Perl decreases that counter every time a reference goes away. When the counter -reaches zero, Perl can safely recycle that value. - Within the inner block in the example, there's one C<$fh>. (Multiple lines in the source code refer to it, but only one variable refers to it: C<$fh>.) C<$fh> is only in scope in the block. Its value never leaves the block. When @@ -585,10 +579,9 @@ around affect how Perl manages memory (see L). When you use references as arguments to functions, document your intent carefully. Modifying the values of a reference from within a function may -surprise the calling code, which expects no modifications. - -To modify the contents of a reference without affecting the reference itself, -copy its values to a new variable: +surprise the calling code, which doesn't expect anything else to modify its +data. To modify the contents of a reference without affecting the reference +itself, copy its values to a new variable: =begin programlisting diff --git a/sections/scalars.pod b/sections/scalars.pod index 37fda758..8acedb6e 100644 --- a/sections/scalars.pod +++ b/sections/scalars.pod @@ -3,18 +3,16 @@ Z X - -Perl 5's fundamental data type is the I, a single, discrete value. -That value may be a string, an integer, a floating point value, a filehandle, -or a reference--but it is always a single value. - X; sigil> X> -Scalars may be lexical, package, or global (L) variables. You may only -declare lexical or package variables. The names of scalar variables must -conform to standard variable naming guidelines (L). Scalar variables -always use the leading dollar-sign (C<$>) sigil (L). +Perl 5's fundamental data type is the I, a single, discrete value. +That value may be a string, an integer, a floating point value, a filehandle, +or a reference--but it is always a single value. Scalars may be lexical, +package, or global (L) variables. You may only declare lexical or +package variables. The names of scalar variables must conform to standard +variable naming guidelines (L). Scalar variables always use the leading +dollar-sign (C<$>) sigil (L). =begin tip Variant Sigils and Context @@ -129,10 +127,14 @@ the empty string (C<''>) and C<'0'> are false. All other strings are true. In boolean context, numbers which evaluate to zero (C<0>, C<0.0>, and C<0e0>) are false. All other numbers are true. +=begin tip What is Truth? + Be careful that the I C<'0.0'> and C<'0e0'> are true; this is one place where Perl 5 makes a distinction between what looks like a number and what really is a number. +=end tip + X> One other value is always false: C. This is the value of uninitialized diff --git a/sections/values.pod b/sections/values.pod index b2b1b058..6bb0b73c 100644 --- a/sections/values.pod +++ b/sections/values.pod @@ -47,7 +47,8 @@ the quote with a leading backslash: =begin programlisting - my $reminder = 'DonB<\'>t forget to escape the single quote!'; + my $reminder = 'DonB<\'>t forget to escape ' + . 'the single quote!'; =end programlisting @@ -56,7 +57,8 @@ the closing delimiter and producing a syntax error: =begin programlisting - my $exception = 'This string ends with a backslash, not a quote: B<\\>'; + my $exception = 'This string ends with a ' + . 'backslash, not a quote: B<\\>'; =end programlisting @@ -100,7 +102,7 @@ equivalent: my $escaped = "two\nlines"; my $literal = "two lines"; - is $escaped, $literal, '\n and newline are equivalent'; + is $escaped, $literal, 'equivalent \n and newline'; =end programlisting @@ -131,11 +133,10 @@ variable become part of the string as if you'd concatenated them: =begin programlisting - my $factoid = "Did you know that B<$name> lives at B<$address>?"; + my $factoid = "B<$name> lives at B<$address>!"; # equivalent to - - my $factoid = 'Did you know that ' . $name . ' lives at ' . $address . '?'; + my $factoid = $name . ' lives at ' . $address . '!'; =end programlisting @@ -168,8 +169,8 @@ both the starting and ending delimiter. =begin programlisting my $quote = B"Ouch", he said. "That I!"B<}>; - my $reminder = BDon't need to escape the single quote!B<^>; - my $complaint = BIt's too early to be awake.B<}>; + my $reminder = BDon't escape the single quote!B<^>; + my $complaint = BIt's too early to be awake.B<}>; =end programlisting @@ -183,12 +184,14 @@ use the I syntax to assign one or more lines of a string: my $blurb =<<'END_BLURB'; - He looked up. "Time is never on our side, my child. Do you see the irony? - All they know is change. Change is the constant on which they all can - agree. Whereas we, born out of time to remain perfect and perfectly - self-aware, can only suffer change if we pursue it. It is against our - nature. We rebel against that change. Shall we consider them greater - for it?" + He looked up. "Time is never on our side, my child. + Do you see the irony? All they know is change. + Change is the constant on which they all can agree. + We instead, born out of time, remain perfect and + perfectly self-aware. We only suffer change as we + pursue it. It is against our nature. We rebel + against that change. Shall we consider them + greater for it?" END_BLURB =end programlisting @@ -352,21 +355,13 @@ X> X> The core module C provides a function named C to convert a -scalar containing data to a Unicode string. For example, if you have UTF-8 -data: - -=begin programlisting - - my $string = decode('utf8', $data); - -=end programlisting - -The corresponding C function converts from Perl's internal encoding -to the desired output encoding: +scalar containing data to a Unicode string. The corresponding C +function converts from Perl's internal encoding to the desired output encoding: =begin programlisting - my $latin1 = encode('iso-8859-1', $string); + my $from_utf8 = decode('utf8', $data); + my $to_latin1 = encode('iso-8859-1', $string); =end programlisting @@ -425,7 +420,8 @@ C<\N{}> escape to refer to them: my $escaped_thorn = "\x{00FE}"; my $named_thorn = "\N{LATIN SMALL LETTER THORN}"; - is $escaped_thorn, $named_thorn, 'Thorn equivalence check'; + is $escaped_thorn, $named_thorn, + 'Thorn equivalence check'; =end programlisting @@ -658,7 +654,8 @@ value> warning: =begin screen - Use of uninitialized value $undefined in concatenation (.) or string... + Use of uninitialized value $undefined in + concatenation (.) or string... =end screen @@ -672,7 +669,7 @@ defined value (anything other than C): my $status = 'suffering from a cold'; say B $status; # 1, which is a true value - say B undef; # an empty string; a false value + say B undef; # empty string; a false value =end programlisting @@ -683,11 +680,10 @@ X; empty list> When used on the right-hand side of an assignment, the C<()> construct represents an empty list. In scalar context, this evaluates to C. In -list context, it is an empty list. - -When used on the left-hand side of an assignment, the C<()> construct imposes -list context. To count the number of elements returned from an expression in -list context without using a temporary variable, use the idiom (L): +list context, it is an empty list. When used on the left-hand side of an +assignment, the C<()> construct imposes list context. To count the number of +elements returned from an expression in list context without using a temporary +variable, use the idiom (L): =begin programlisting @@ -785,7 +781,7 @@ a single element) or list context (for a slice): =begin programlisting - # you do not need to understand all of this right now + # don't worry about the details right now sub context { my $context = wantarray();