Pod formatting nit, found by Merijn and Jos Boumans.
[p5sagit/p5-mst-13.2.git] / pod / perlop.pod
index 9ae3918..8af2d5b 100644 (file)
@@ -26,7 +26,7 @@ values only, not array values.
     left       &
     left       | ^
     left       &&
-    left       ||
+    left       || //
     nonassoc   ..  ...
     right      ?:
     right      = += -= *= etc.
@@ -34,7 +34,7 @@ values only, not array values.
     nonassoc   list operators (rightward)
     right      not
     left       and
-    left       or xor
+    left       or xor err
 
 In the following sections, these operators are covered in precedence order.
 
@@ -361,12 +361,23 @@ if the left operand is true, the right operand is not even evaluated.
 Scalar or list context propagates down to the right operand if it
 is evaluated.
 
-The C<||> and C<&&> operators differ from C's in that, rather than returning
+=head2 C-style Logical Defined-Or
+
+Although it has no direct equivalent in C, Perl's C<//> operator is related
+to its C-style or.  In fact, it's exactly the same as C<||>, except that it 
+tests the left hand side's definedness instead of its truth.  Thus, C<$a // $b>
+is similar to C<defined($a) || $b> (except that it returns the value of C<$a> 
+rather than the value of C<defined($a)>) and is exactly equivalent to 
+C<defined($a) ? $a : $b>.  This is very useful for providing default values
+for variables.  If you actually want to test if at least one of C<$a> and C<$b> is
+defined, use C<defined($a // $b)>.
+
+The C<||>, C<//> and C<&&> operators differ from C's in that, rather than returning
 0 or 1, they return the last value evaluated.  Thus, a reasonably portable
-way to find out the home directory (assuming it's not "0") might be:
+way to find out the home directory might be:
 
-    $home = $ENV{'HOME'} || $ENV{'LOGDIR'} ||
-       (getpwuid($<))[7] || die "You're homeless!\n";
+    $home = $ENV{'HOME'} // $ENV{'LOGDIR'} //
+       (getpwuid($<))[7] // die "You're homeless!\n";
 
 In particular, this means that you shouldn't use this
 for selecting between two aggregates for assignment:
@@ -375,10 +386,10 @@ for selecting between two aggregates for assignment:
     @a = scalar(@b) || @c;     # really meant this
     @a = @b ? @b : @c;         # this works fine, though
 
-As more readable alternatives to C<&&> and C<||> when used for
-control flow, Perl provides C<and> and C<or> operators (see below).
-The short-circuit behavior is identical.  The precedence of "and" and
-"or" is much lower, however, so that you can safely use them after a
+As more readable alternatives to C<&&>, C<//> and C<||> when used for
+control flow, Perl provides C<and>, C<err> and C<or> operators (see below).
+The short-circuit behavior is identical.  The precedence of "and", "err" 
+and "or" is much lower, however, so that you can safely use them after a
 list operator without the need for parentheses:
 
     unlink "alpha", "beta", "gamma"
@@ -395,10 +406,10 @@ Using "or" for assignment is unlikely to do what you want; see below.
 
 Binary ".." is the range operator, which is really two different
 operators depending on the context.  In list context, it returns an
-array of values counting (up by ones) from the left value to the right
+list of values counting (up by ones) from the left value to the right
 value.  If the left value is greater than the right value then it
 returns the empty array.  The range operator is useful for writing
-C<foreach (1..10)> loops and for doing slice operations on arrays.  In
+C<foreach (1..10)> loops and for doing slice operations on arrays. In
 the current implementation, no temporary array is created when the
 range operator is used as the expression in C<foreach> loops, but older
 versions of Perl might burn a lot of memory when you write something
@@ -408,6 +419,9 @@ like this:
        # code
     }
 
+The range operator also works on strings, using the magical auto-increment,
+see below.
+
 In scalar context, ".." returns a boolean value.  The operator is
 bistable, like a flip-flop, and emulates the line-range (comma) operator
 of B<sed>, B<awk>, and various editors.  Each ".." operator maintains its
@@ -462,7 +476,7 @@ can say
 
     @alphabet = ('A' .. 'Z');
 
-to get all normal letters of the alphabet, or
+to get all normal letters of the English alphabet, or
 
     $hexdigit = (0 .. 9, 'a' .. 'f')[$num & 15];
 
@@ -597,7 +611,7 @@ expressions.  It's equivalent to && except for the very low
 precedence.  This means that it short-circuits: i.e., the right
 expression is evaluated only if the left expression is true.
 
-=head2 Logical or and Exclusive Or
+=head2 Logical or, Defined or, and Exclusive Or
 
 Binary "or" returns the logical disjunction of the two surrounding
 expressions.  It's equivalent to || except for the very low precedence.
@@ -620,7 +634,12 @@ takes higher precedence.
     @info = stat($file) || die;     # oops, scalar sense of stat!
     @info = stat($file) or die;     # better, now @info gets its due
 
-Then again, you could always use parentheses. 
+Then again, you could always use parentheses.
+
+Binary "err" is equivalent to C<//>--it's just like binary "or", except it tests
+its left argument's definedness instead of its truth.  There are two ways to 
+remember "err":  either because many functions return C<undef> on an B<err>or,
+or as a sort of correction:  C<$a=($b err 'default')>
 
 Binary "xor" returns the exclusive-OR of the two surrounding expressions.
 It cannot short circuit, of course.
@@ -664,6 +683,7 @@ any pair of delimiters you choose.
                qr{}          Pattern             yes*
                 s{}{}      Substitution          yes*
                tr{}{}    Transliteration         no (but see below)
+        <<EOF                 here-doc            yes*
 
        * unless the delimiter is ''.
 
@@ -708,7 +728,7 @@ and in transliterations.
     \x1b       hex char        (ESC)
     \x{263a}   wide hex char   (SMILEY)
     \c[                control char    (ESC)
-    \N{name}   named char
+    \N{name}   named Unicode character
 
 The following escape sequences are available in constructs that interpolate
 but not in transliterations.
@@ -720,9 +740,12 @@ but not in transliterations.
     \E         end case modification
     \Q         quote non-word characters till \E
 
-If C<use locale> is in effect, the case map used by C<\l>, C<\L>, C<\u>
-and C<\U> is taken from the current locale.  See L<perllocale>.  For
-documentation of C<\N{name}>, see L<charnames>.
+If C<use locale> is in effect, the case map used by C<\l>, C<\L>,
+C<\u> and C<\U> is taken from the current locale.  See L<perllocale>.
+If Unicode (for example, C<\N{}> or wide hex characters of 0x100 or
+beyond) is being used, the case map used by C<\l>, C<\L>, C<\u> and
+C<\U> is as defined by Unicode.  For documentation of C<\N{name}>,
+see L<charnames>.
 
 All systems use the virtual C<"\n"> to represent a line terminator,
 called a "newline".  There is no such thing as an unvarying, physical
@@ -834,7 +857,20 @@ that you won't change the variables in the pattern.  If you change them,
 Perl won't even notice.  See also L<"qr/STRING/imosx">.
 
 If the PATTERN evaluates to the empty string, the last
-I<successfully> matched regular expression is used instead.
+I<successfully> matched regular expression is used instead. In this
+case, only the C<g> and C<c> flags on the empty pattern is honoured -
+the other flags are taken from the original pattern. If no match has
+previously succeeded, this will (silently) act instead as a genuine
+empty pattern (which will always match).
+
+Note that it's possible to confuse Perl into thinking C<//> (the empty 
+regex) is really C<//> (the defined-or operator).  Perl is usually pretty 
+good about this, but some pathological cases might trigger this, such as 
+C<$a///> (is that C<($a) / (//)> or C<$a // />?) and C<print $fh //> 
+(C<print $fh(//> or C<print($fh //>?).  In all of these examples, Perl 
+will assume you meant defined-or.  If you meant the empty regex, just 
+use parentheses or spaces to disambiguate, or even prefix the empty 
+regex with an C<m> (so C<//> becomes C<m//>).
 
 If the C</g> option is not used, C<m//> in list context returns a
 list consisting of the subexpressions matched by the parentheses in the
@@ -888,7 +924,8 @@ C<m//g>, if any, left off.  Without the C</g> modifier, the C<\G> assertion
 still anchors at pos(), but the match is of course only attempted once.
 Using C<\G> without C</g> on a target string that has not previously had a
 C</g> match applied to it is the same as using the C<\A> assertion to match
-the beginning of the string.
+the beginning of the string.  Note also that, currently, C<\G> is only
+properly supported when anchored at the very beginning of the pattern.
 
 Examples:
 
@@ -1343,6 +1380,98 @@ must use an eval():
 
     eval "tr/$oldlist/$newlist/, 1" or die $@;
 
+=item <<EOF
+
+A line-oriented form of quoting is based on the shell "here-document"
+syntax.  Following a C<< << >> you specify a string to terminate
+the quoted material, and all lines following the current line down to
+the terminating string are the value of the item.  The terminating
+string may be either an identifier (a word), or some quoted text.  If
+quoted, the type of quotes you use determines the treatment of the
+text, just as in regular quoting.  An unquoted identifier works like
+double quotes.  There must be no space between the C<< << >> and
+the identifier, unless the identifier is quoted.  (If you put a space it
+will be treated as a null identifier, which is valid, and matches the first
+empty line.)  The terminating string must appear by itself (unquoted and
+with no surrounding whitespace) on the terminating line.
+
+       print <<EOF;
+    The price is $Price.
+    EOF
+
+       print << "EOF"; # same as above
+    The price is $Price.
+    EOF
+
+       print << `EOC`; # execute commands
+    echo hi there
+    echo lo there
+    EOC
+
+       print <<"foo", <<"bar"; # you can stack them
+    I said foo.
+    foo
+    I said bar.
+    bar
+
+       myfunc(<< "THIS", 23, <<'THAT');
+    Here's a line
+    or two.
+    THIS
+    and here's another.
+    THAT
+
+Just don't forget that you have to put a semicolon on the end
+to finish the statement, as Perl doesn't know you're not going to
+try to do this:
+
+       print <<ABC
+    179231
+    ABC
+       + 20;
+
+If you want your here-docs to be indented with the 
+rest of the code, you'll need to remove leading whitespace
+from each line manually:
+
+    ($quote = <<'FINIS') =~ s/^\s+//gm;
+       The Road goes ever on and on, 
+       down from the door where it began.
+    FINIS
+
+If you use a here-doc within a delimited construct, such as in C<s///eg>,
+the quoted material must come on the lines following the final delimiter.
+So instead of
+
+    s/this/<<E . 'that'
+    the other
+    E
+     . 'more '/eg;
+
+you have to write
+
+    s/this/<<E . 'that' 
+     . 'more '/eg; 
+    the other 
+    E 
+
+If the terminating identifier is on the last line of the program, you
+must be sure there is a newline after it; otherwise, Perl will give the
+warning B<Can't find string terminator "END" anywhere before EOF...>.
+
+Additionally, the quoting rules for the identifier are not related to
+Perl's quoting rules -- C<q()>, C<qq()>, and the like are not supported
+in place of C<''> and C<"">, and the only interpolation is for backslashing
+the quoting character:
+
+    print << "abc\"def";
+    testing...
+    abc"def
+
+Finally, quoted strings cannot span multiple lines.  The general rule is
+that the identifier must be a string literal.  Stick with that, and you
+should be safe.
+
 =back
 
 =head2 Gory details of parsing quoted constructs