This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
(perl #133706) remove exploit code from Storable
[perl5.git] / pod / perlsyn.pod
index e2e9c6a..b49227a 100644 (file)
@@ -116,16 +116,6 @@ C<do {}> that I<look> 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<truth> X<falsehood> X<true> X<false> X<!> X<not> X<negation> X<0>
-
-The number 0, the strings C<'0'> and C<"">, the empty list C<()>, and
-C<undef> are all false in a boolean context.  All other values are true.
-Negation of a true value by C<!> or C<not> 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<statement modifier> X<modifier> X<if> X<unless> X<while>
 X<until> X<when> X<foreach> X<for>
@@ -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<while> repeats the statement I<while> the condition is true.
+Postfix C<while> has the same magic treatment of some kinds of condition
+that prefix C<while> has.
 C<until> does the opposite, it repeats the statement I<until> 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<perlref>), 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<while> statement executes the block as long as the expression is
-L<true|/"Truth and Falsehood">.
+true.
 The C<until> 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<use warnings>
 pragma or the B<-w> flag.
 
+If the condition expression of a C<while> 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<C<readline>|perlfunc/readline EXPR>, the L<C<< <FILEHANDLE>
+>>|perlop/"I/O Operators"> input operator, L<C<readdir>|perlfunc/readdir
+DIRHANDLE>, L<C<glob>|perlfunc/glob EXPR>, the L<C<< <PATTERN>
+>>|perlop/"I/O Operators"> globbing operator, and L<C<each>|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<continue> 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<eof> X<end-of-file> X<end of file>
         # do something
     }
 
-Using C<readline> (or the operator form, C<< <EXPR> >>) as the
-conditional of a C<for> loop is shorthand for the following.  This
-behaviour is the same as a C<while> loop conditional.
-X<readline> X<< <> >>
-
-    for ( prompt(); defined( $_ = <STDIN> ); prompt() ) {
-        # do something
-    }
+The condition expression of a C<for> loop gets the same magic treatment of
+C<readline> et al that the condition expression of a C<while> loop gets.
 
 =head2 Foreach Loops
 X<for> X<foreach>
@@ -573,7 +581,7 @@ between the inner and outer loops later on, the new code won't be
 accidentally executed.  The C<next> explicitly iterates the other loop
 rather than merely terminating the inner one.  And it's faster because
 Perl executes a C<foreach> statement more rapidly than it would the
-equivalent C<for> loop.
+equivalent C-style C<for> loop.
 
 Perceptive Perl hackers may have noticed that a C<for> loop has a return
 value, and that this value can be captured by wrapping the loop in a C<do>
@@ -984,7 +992,7 @@ the form C<!/REGEX/>, C<$foo !~ /REGEX/>, or C<$foo !~ EXPR>.
 A smart match that uses an explicit C<~~> operator, such as C<EXPR ~~ EXPR>.
 
 B<NOTE:> 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.