X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlsyn.pod;h=9cf39a3d5a6ce94fda48d9094de293426724fe08;hb=724421654fcf33f3acf0a7b8f4356b9255c9265a;hp=c3ef4501dde4062dccca1263fadb69f69d3441cd;hpb=9b1b54d0581812b8017b125420e5d2fb98df5bed;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlsyn.pod b/pod/perlsyn.pod index c3ef450..9cf39a3 100644 --- a/pod/perlsyn.pod +++ b/pod/perlsyn.pod @@ -35,7 +35,7 @@ take effect at compile time. Typically all the declarations are put at the beginning or the end of the script. However, if you're using lexically-scoped private variables created with my(), you'll have to make sure your format or subroutine definition is within the same block scope -as the my if you expect to to be able to access those private variables. +as the my if you expect to be able to access those private variables. Declaring a subroutine allows a subroutine name to be used as if it were a list operator from that point forward in the program. You can declare a @@ -63,7 +63,7 @@ The only kind of simple statement is an expression evaluated for its side effects. Every simple statement must be terminated with a semicolon, unless it is the final statement in a block, in which case the semicolon is optional. (A semicolon is still encouraged there if the -block takes up more than one line, since you may eventually add another line.) +block takes up more than one line, because you may eventually add another line.) Note that there are some operators like C and C that look like compound statements, but aren't (they're just TERMs in an expression), and thus need an explicit termination if used as the last item in a statement. @@ -91,7 +91,7 @@ can write loops like: } until $line eq ".\n"; See L. Note also that the loop control -statements described later will I work in this construct, since +statements described later will I work in this construct, because modifiers don't take loop labels. Sorry. You can always wrap another block around it to do that sort of thing. @@ -128,7 +128,7 @@ all do the same thing: open(FOO) ? 'hi mom' : die "Can't open $FOO: $!"; # a bit exotic, that last one -The C statement is straightforward. Since BLOCKs are always +The C statement is straightforward. Because BLOCKs are always bounded by curly brackets, there is never any ambiguity about which C an C goes with. If you use C in place of C, the sense of the test is reversed. @@ -196,7 +196,7 @@ which is Perl short-hand for the more explicitly written version: # now process $line } -Or here's a a simpleminded Pascal comment stripper (warning: assumes no { or } in strings) +Or here's a simpleminded Pascal comment stripper (warning: assumes no { or } in strings). LINE: while () { while (s|({.*}.*){.*}|$1 |) {} @@ -244,6 +244,9 @@ is the same as this: $i++; } +(There is one minor difference: The first form implies a lexical scope +for variables declared with C in the initialization expression.) + Besides the normal array index looping, C can lend itself to many other interesting applications. Here's one that avoids the problem you get into if you explicitly test for end-of-file on @@ -259,12 +262,14 @@ hang. =head2 Foreach Loops The C loop iterates over a normal list value and sets the -variable VAR to be each element of the list in turn. The variable is -implicitly local to the loop and regains its former value upon exiting the -loop. If the variable was previously declared with C, it uses that -variable instead of the global one, but it's still localized to the loop. -This can cause problems if you have subroutine or format declarations -within that block's scope. +variable VAR to be each element of the list in turn. If the variable +is preceded with the keyword C, then it is lexically scoped, and +is therefore visible only within the loop. Otherwise, the variable is +implicitly local to the loop and regains its former value upon exiting +the loop. If the variable was previously declared with C, it uses +that variable instead of the global one, but it's still localized to +the loop. (Note that a lexically scoped variable can cause problems +with you have subroutine or format declarations.) The C keyword is actually a synonym for the C keyword, so you can use C for readability or C for brevity. If VAR is @@ -278,7 +283,7 @@ Examples: for (@ary) { s/foo/bar/ } - foreach $elem (@elements) { + foreach my $elem (@elements) { $elem *= 2; } @@ -294,8 +299,8 @@ Examples: Here's how a C programmer might code up a particular algorithm in Perl: - for ($i = 0; $i < @ary1; $i++) { - for ($j = 0; $j < @ary2; $j++) { + for (my $i = 0; $i < @ary1; $i++) { + for (my $j = 0; $j < @ary2; $j++) { if ($ary1[$i] > $ary2[$j]) { last; # can't go to outer :-( } @@ -304,11 +309,11 @@ Here's how a C programmer might code up a particular algorithm in Perl: # this is where that last takes me } -Whereas here's how a Perl programmer more confortable with the idiom might +Whereas here's how a Perl programmer more comfortable with the idiom might do it: - OUTER: foreach $wid (@ary1) { - INNER: foreach $jet (@ary2) { + OUTER: foreach my $wid (@ary1) { + INNER: foreach my $jet (@ary2) { next OUTER if $wid > $jet; $wid += $jet; } @@ -317,19 +322,19 @@ do it: See how much easier this is? It's cleaner, safer, and faster. It's cleaner because it's less noisy. It's safer because if code gets added between the inner and outer loops later on, the new code won't be -accidentally executed, the C explicitly iterates the other loop +accidentally executed. The C explicitly iterates the other loop rather than merely terminating the inner one. And it's faster because Perl executes a C statement more rapidly than it would the equivalent C loop. =head2 Basic BLOCKs and Switch Statements -A BLOCK by itself (labeled or not) is semantically equivalent to a loop -that executes once. Thus you can use any of the loop control -statements in it to leave or restart the block. (Note that this -is I true in C, C, or contrary to popular belief C blocks, -which do I count as loops.) The C block -is optional. +A BLOCK by itself (labeled or not) is semantically equivalent to a +loop that executes once. Thus you can use any of the loop control +statements in it to leave or restart the block. (Note that this is +I true in C, C, or contrary to popular belief +C blocks, which do I count as loops.) The C +block is optional. The BLOCK construct is particularly nice for doing case structures. @@ -491,8 +496,8 @@ and your documentation text freely, as in ......... } -Note that pod translators should only look at paragraphs beginning -with a pod diretive (it makes parsing easier), whereas the compiler +Note that pod translators should look at only paragraphs beginning +with a pod directive (it makes parsing easier), whereas the compiler actually knows to look for pod escapes even in the middle of a paragraph. This means that the following secret stuff will be ignored by both the compiler and the translators.