X-Git-Url: https://perl5.git.perl.org/perl5.git/blobdiff_plain/29d69c3c41c7e93f884256b1087face64d5fdd1e..fb5f378b17e3b41db03064c19b9205db64a3354c:/pod/perlsyn.pod diff --git a/pod/perlsyn.pod b/pod/perlsyn.pod index e2e9c6a..b49227a 100644 --- a/pod/perlsyn.pod +++ b/pod/perlsyn.pod @@ -116,16 +116,6 @@ C that I like compound statements, but aren't--they're just TERMs in an expression--and thus need an explicit termination when used as the last item in a statement. -=head2 Truth and Falsehood -X X X X X X X X<0> - -The number 0, the strings C<'0'> and C<"">, the empty list C<()>, and -C are all false in a boolean context. All other values are true. -Negation of a true value by C or C returns a special false value. -When evaluated as a string it is treated as C<"">, but as a number, it -is treated as 0. Most Perl operators -that return true or false behave this way. - =head2 Statement Modifiers X X X X X X X X X @@ -158,6 +148,8 @@ for each item in the LIST (with C<$_> aliased to each item in turn). print "Hello $_!\n" for qw(world Dolly nurse); C repeats the statement I the condition is true. +Postfix C has the same magic treatment of some kinds of condition +that prefix C has. C does the opposite, it repeats the statement I the condition is true (or while the condition is false): @@ -243,8 +235,16 @@ Sometimes a block is delimited by the file containing it (in the case of a required file, or the program as a whole), and sometimes a block is delimited by the extent of a string (in the case of an eval). -But generally, a block is delimited by curly brackets, also known as braces. -We will call this syntactic construct a BLOCK. +But generally, a block is delimited by curly brackets, also known as +braces. We will call this syntactic construct a BLOCK. Because enclosing +braces are also the syntax for hash reference constructor expressions +(see L), you may occasionally need to disambiguate by placing a +C<;> immediately after an opening brace so that Perl realises the brace +is the start of a block. You will more frequently need to disambiguate +the other way, by placing a C<+> immediately before an opening brace to +force it to be interpreted as a hash reference constructor expression. +It is considered good style to use these disambiguating mechanisms +liberally, not only when Perl would otherwise guess incorrectly. The following compound statements may be used to control flow: @@ -304,7 +304,7 @@ language construct, as everyone reading your code will have to think at least twice before they can understand what's going on. The C statement executes the block as long as the expression is -L. +true. The C statement executes the block as long as the expression is false. The LABEL is optional, and if present, consists of an identifier followed @@ -316,6 +316,20 @@ looking back your call-stack at run time to find the LABEL. Such desperate behavior triggers a warning if you use the C pragma or the B<-w> flag. +If the condition expression of a C statement is based +on any of a group of iterative expression types then it gets +some magic treatment. The affected iterative expression types +are L|perlfunc/readline EXPR>, the L +>>|perlop/"I/O Operators"> input operator, L|perlfunc/readdir +DIRHANDLE>, L|perlfunc/glob EXPR>, the L +>>|perlop/"I/O Operators"> globbing operator, and L|perlfunc/each +HASH>. If the condition expression is one of these expression types, then +the value yielded by the iterative operator will be implicitly assigned +to C<$_>. If the condition expression is one of these expression types +or an explicit assignment of one of them to a scalar, then the condition +actually tests for definedness of the expression's value, not for its +regular truth value. + If there is a C BLOCK, it is always executed just before the conditional is about to be evaluated again. Thus it can be used to increment a loop variable, even when the loop has been continued via @@ -469,14 +483,8 @@ X X X # do something } -Using C (or the operator form, C<< >>) as the -conditional of a C loop is shorthand for the following. This -behaviour is the same as a C loop conditional. -X X<< <> >> - - for ( prompt(); defined( $_ = ); prompt() ) { - # do something - } +The condition expression of a C loop gets the same magic treatment of +C et al that the condition expression of a C loop gets. =head2 Foreach Loops X X @@ -573,7 +581,7 @@ between the inner and outer loops later on, the new code won't be 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. +equivalent C-style C loop. Perceptive Perl hackers may have noticed that a C loop has a return value, and that this value can be captured by wrapping the loop in a C @@ -984,7 +992,7 @@ the form C, C<$foo !~ /REGEX/>, or C<$foo !~ EXPR>. A smart match that uses an explicit C<~~> operator, such as C. B You will often have to use C<$c ~~ $_> because the default case -uses C<$_ ~~ $c> , which is frequentlythe opposite of what you want. +uses C<$_ ~~ $c> , which is frequently the opposite of what you want. =item Z<>4.