From fbf260c6bc6b0edb5bdd44c9de110bae75111cb3 Mon Sep 17 00:00:00 2001 From: chromatic Date: Thu, 20 Oct 2011 16:56:04 -0700 Subject: [PATCH] Edited chapter 10. --- sections/chapter_10.pod | 14 +- sections/files.pod | 1 + sections/globals.pod | 128 +++++------ sections/hashes.pod | 2 + sections/idioms.pod | 374 +++++++++++++++---------------- sections/regular_expressions.pod | 2 + 6 files changed, 255 insertions(+), 266 deletions(-) diff --git a/sections/chapter_10.pod b/sections/chapter_10.pod index bd978aef..3893ff3c 100644 --- a/sections/chapter_10.pod +++ b/sections/chapter_10.pod @@ -1,13 +1,11 @@ =head0 Perl Beyond Syntax -Perl 5 is a large language, like any language intended to solve problems in the -real world. Effective Perl programs require more than mere understanding of -syntax; you must also begin to understand how Perl's features interact and -common ways of solving well-understood problems in Perl. - -Prepare for the second learning curve of Perl: Perlish thinking. The effective -use of common patterns of behavior and builtin shortcuts allow you to write -concise and powerful code. +Baby Perl will only get you so far. Language fluency allows you to use the +natural patterns and idioms of the language. Effective programmers understand +how Perl's features interact and combine. + +Prepare for the second learning curve of Perl: Perlish thinking. The result is +concise, powerful, and Perlish code. L diff --git a/sections/files.pod b/sections/files.pod index 022b85da..d51f57e3 100644 --- a/sections/files.pod +++ b/sections/files.pod @@ -304,6 +304,7 @@ As usual, C handles these checks for you: =head3 Special File Handling Variables +Z X> X> diff --git a/sections/globals.pod b/sections/globals.pod index 6c348f25..a44ea9cb 100644 --- a/sections/globals.pod +++ b/sections/globals.pod @@ -6,28 +6,25 @@ X X Perl 5 provides several I that are truly global, not -restricted to any specific package. These super globals have two drawbacks. -First, they're global; any direct or indirect modifications may have effects on -other parts of the program. Second, they're terse. Experienced Perl 5 -programmers have memorized some of them. Few people have memorized all of -them. Only a handful are ever useful. C contains the -exhaustive list of such variables. +scoped to a package or file. Unfortunately, +their global availability means that any direct or indirect +modifications may have effects on other parts of the program--and they're terse. +Experienced Perl 5 programmers have memorized some of them. Few people +have memorized all of them. Only a handful are ever useful. C contains the exhaustive list of such variables. =head2 Managing Super Globals X X> -The best approach to managing the global behavior of these super globals is to -avoid using them. When you must use them, use C in the smallest -possible scope to constrain any modifications. You are still susceptible to -any changes code you I makes to those globals, but you reduce the -likelihood of surprising code I of your scope. - -Workarounds exist for some of this global behavior, but many of these variables -have existed since Perl 1 and will continue as part of Perl 5 throughout its -lifetime. As the easy file slurping idiom (L) -demonstrates, this is often possible: +Perl 5 continues to move more global behavior into lexical behavior, so you can +avoid many of these globals. When you can't avoid them, use C in the +smallest possible scope to constrain any modifications. You are still +susceptible to any changes code you I makes to those globals, but you +reduce the likelihood of surprising code I of your scope. As the easy +file slurping idiom (L) demonstrates, C is often the +right approach: =begin programlisting @@ -37,20 +34,20 @@ demonstrates, this is often possible: The effect of Cizing C<$/> lasts only through the end of the block. There is a low chance that any Perl code will run as a result of reading lines -from the filehandleN and -change the value of C<$/> within the C block. +from the filehandleN) is one of the few +possibilities.> and change the value of C<$/> within the C block. Not all cases of using super globals are this easy to guard, but this often works. X> X +X> Other times you need to I the value of a super global and hope that no -other code has modified it. Catching exceptions with an C block can be -susceptible to race conditionsN<< Use C instead! >>, in that -C methods invoked on lexicals that have gone out of scope may reset -C<$@>: +other code has modified it. Catching exceptions with an C block can be +susceptible to race conditions, in that C methods invoked on +lexicals that have gone out of scope may reset C<$@>: =begin programlisting @@ -62,14 +59,23 @@ C<$@>: =end programlisting -Copy C<$@> I to preserve its contents. +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 =head2 English Names X> -The core C module provides verbose names for the punctuation-heavy -super globals. Import them into a namespace with: +The core C module provides verbose names for punctuation-heavy super +globals. Import them into a namespace with: =begin programlisting @@ -77,8 +83,8 @@ super globals. Import them into a namespace with: =end programlisting -Subsequently you can use the verbose names documented in C -within the scope of this namespace. +This allows you to use the verbose names documented in C +within the scope of this pragma. X> X> @@ -89,9 +95,8 @@ X> Three regex-related super globals (C<$&>, C<$`>, and C<$'>) impose a global performance penalty for I regular expressions within a program. If you -neglect to provide that import flag, your program will suffer the penalty even -if you don't explicitly read from those variables. This is not the default -behavior for backwards-compatibility concerns. +forget the C<-no_match_vars> import, your program will suffer the penalty even +if you don't explicitly read from those variables. Modern Perl programs should use the C<@-> variable as a replacement for the terrible three. @@ -101,9 +106,8 @@ terrible three. X Most modern Perl 5 programs can get by with using only a couple of the super -globals. Several exist for special circumstances you're unlikely to encounter. -While C is the canonical documentation for most of these -variables, some deserve special mention. +globals. You're most likely to encounter only a few of these variables in real +programs. X> X> @@ -115,33 +119,32 @@ X> =item * C<$/> (or C<$INPUT_RECORD_SEPARATOR> from the C pragma) is a string of zero or more characters which denotes the end of a record when -reading input a line at a timeN should more accurately be -C, but the name has stuck by now.>. By default, this is your -platform-specific newline character sequence. If you undefine this value, Perl -will attempt to read the entire file into memory. If you set this value to a -I to an integer, Perl will try to read that many I per record -(so beware of Unicode concerns). +reading input a line at a time. By default, this is your platform-specific +newline character sequence. If you undefine this value, Perl will attempt to +read the entire file into memory. If you set this value to a I to an +integer, Perl will try to read that many I per record (so beware of +Unicode concerns). X> X> X> X> -=item * C<$.> (C<$INPUT_LINE_NUMBER>) contains the number of current record -read from the most recently-accessed filehandle. You can read from this -variable, but writing to it has no effect. Localizing this variable will -localize the filehandle to which it refers. +=item * C<$.> (C<$INPUT_LINE_NUMBER>) contains the number of records read from +the most recently-accessed filehandle. You can read from this variable, but +writing to it has no effect. Localizing this variable will localize the +filehandle to which it refers. X> X> X> X> -=item * C<$|> (C<$OUTPUT_AUTOFLUSH>) is the boolean value of this variable -governs whether Perl will flush everything written to the currently selected -filehandle immediately or only when Perl's buffer is full. Unbuffered output -is useful when writing to a pipe or socket or terminal which should not block -waiting for input. +=item * C<$|> (C<$OUTPUT_AUTOFLUSH>) governs whether Perl will flush everything +written to the currently selected filehandle immediately or only when Perl's +buffer is full. Unbuffered output is useful when writing to a pipe or socket or +terminal which should not block waiting for input. This variable will coerce +any values assigned to it to boolean values. X> X> @@ -155,13 +158,13 @@ X> =item * C<$!> (C<$ERRNO>) is a dualvar (L) which contains the result of the I system call. In numeric context, this corresponds to C's -C value, where anything other than zero indicates some kind of error. -In string context, returns the appropriate system error string. Localize this +C value, where anything other than zero indicates an error. In string +context, this evaluates to the appropriate system error string. Localize this variable before making a system call (implicitly or explicitly) to avoid overwriting the appropriate value for other code elsewhere. Many places within -Perl 5 itself make system calls without your knowledge. The value of this -variable can change out from under you, so copy it I after making -such a call yourself. +Perl 5 itself make system calls without your knowledge, so the value of this +variable can change out from under you. Copy it I after causing a +system call for the most accurate results. X> X> @@ -202,7 +205,7 @@ X> =item * C<$$> (C<$PID>) contains the process id of the currently running instance of the program, as the operating system understands it. This will -vary between Ced programs and may vary between threads in the same +vary between Ced programs and I vary between threads in the same program. X> @@ -228,16 +231,9 @@ X X> The worst culprits for action at a distance relate to IO and exceptional -conditions. Using C (L) will help insulate you -from the tricky semantics of proper exception handling. Cizing and +conditions. Using C (L) will help insulate you +from the tricky semantics of proper exception handling. Cizing and copying the value of C<$!> can help you avoid strange behaviors when Perl makes -implicit system calls. - -X> - -C allows you to call methods on filehandles -(L) to replace the manipulation of IO-related super -globals. Call the C method on a lexical filehandle instead of -C