This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Merge branch 'blead' of ssh://perl5.git.perl.org/gitroot/perl into blead
[perl5.git] / pod / perlop.pod
index 355e8aa..a6514bb 100644 (file)
@@ -53,7 +53,7 @@ values only, not array values.
     nonassoc   list operators (rightward)
     right      not
     left       and
-    left       or xor err
+    left       or xor
 
 In the following sections, these operators are covered in precedence order.
 
@@ -200,7 +200,7 @@ concatenated with the identifier is returned.  Otherwise, if the string
 starts with a plus or minus, a string starting with the opposite sign
 is returned.  One effect of these rules is that -bareword is equivalent
 to the string "-bareword".  If, however, the string begins with a
-non-alphabetic character (exluding "+" or "-"), Perl will attempt to convert
+non-alphabetic character (excluding "+" or "-"), Perl will attempt to convert
 the string to a numeric and the arithmetic negation is performed. If the
 string cannot be cleanly converted to a numeric, Perl will give the warning
 B<Argument "the string" isn't numeric in negation (-) at ...>.
@@ -260,9 +260,11 @@ X<*>
 Binary "/" divides two numbers.
 X</> X<slash>
 
-Binary "%" computes the modulus of two numbers.  Given integer
+Binary "%" is the modulo operator, which computes the division
+remainder of its first argument with respect to its second argument.
+Given integer
 operands C<$a> and C<$b>: If C<$b> is positive, then C<$a % $b> is
-C<$a> minus the largest multiple of C<$b> that is not greater than
+C<$a> minus the largest multiple of C<$b> less than or equal to
 C<$a>.  If C<$b> is negative, then C<$a % $b> is C<$a> minus the
 smallest multiple of C<$b> that is not less than C<$a> (i.e. the
 result will be less than or equal to zero).  If the operands
@@ -273,14 +275,14 @@ the integer portion of C<$a> and C<$b> will be used in the operation
 If the absolute value of the right operand (C<abs($b)>) is greater than
 or equal to C<(UV_MAX + 1)>, "%" computes the floating-point remainder
 C<$r> in the equation C<($r = $a - $i*$b)> where C<$i> is a certain
-integer that makes C<$r> should have the same sign as the right operand
+integer that makes C<$r> have the same sign as the right operand
 C<$b> (B<not> as the left operand C<$a> like C function C<fmod()>)
 and the absolute value less than that of C<$b>.
 Note that when C<use integer> is in scope, "%" gives you direct access
-to the modulus operator as implemented by your C compiler.  This
+to the modulo operator as implemented by your C compiler.  This
 operator is not as well defined for negative operands, but it will
 execute faster.
-X<%> X<remainder> X<modulus> X<mod>
+X<%> X<remainder> X<modulo> X<mod>
 
 Binary "x" is the repetition operator.  In scalar context or if the left
 operand is not enclosed in parentheses, it returns a string consisting
@@ -447,8 +449,6 @@ X<cmp>
 
 Binary "~~" does a smart match between its arguments. Smart matching
 is described in L<perlsyn/"Smart matching in detail">.
-This operator is only available if you enable the "~~" feature:
-see L<feature> for more information.
 X<~~>
 
 "lt", "le", "ge", "gt" and "cmp" use the collation (sort) order specified
@@ -522,9 +522,9 @@ 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<&&>, 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"
+As more readable alternatives to C<&&> and C<||> when used for
+control flow, Perl provides the 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
 list operator without the need for parentheses:
 
@@ -556,33 +556,33 @@ like this:
        # code
     }
 
-The range operator also works on strings, using the magical auto-increment,
-see below.
+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
-own boolean state.  It is false as long as its left operand is false.
+bistable, like a flip-flop, and emulates the line-range (comma)
+operator of B<sed>, B<awk>, and various editors. Each ".." operator
+maintains its own boolean state, even across calls to a subroutine
+that contains it. It is false as long as its left operand is false.
 Once the left operand is true, the range operator stays true until the
 right operand is true, I<AFTER> which the range operator becomes false
-again.  It doesn't become false till the next time the range operator is
-evaluated.  It can test the right operand and become false on the same
-evaluation it became true (as in B<awk>), but it still returns true once.
-If you don't want it to test the right operand till the next
-evaluation, as in B<sed>, just use three dots ("...") instead of
+again.  It doesn't become false till the next time the range operator
+is evaluated.  It can test the right operand and become false on the
+same evaluation it became true (as in B<awk>), but it still returns
+true once. If you don't want it to test the right operand until the
+next evaluation, as in B<sed>, just use three dots ("...") instead of
 two.  In all other regards, "..." behaves just like ".." does.
 
 The right operand is not evaluated while the operator is in the
 "false" state, and the left operand is not evaluated while the
 operator is in the "true" state.  The precedence is a little lower
 than || and &&.  The value returned is either the empty string for
-false, or a sequence number (beginning with 1) for true.  The
-sequence number is reset for each range encountered.  The final
-sequence number in a range has the string "E0" appended to it, which
-doesn't affect its numeric value, but gives you something to search
-for if you want to exclude the endpoint.  You can exclude the
-beginning point by waiting for the sequence number to be greater
-than 1.
+false, or a sequence number (beginning with 1) for true.  The sequence
+number is reset for each range encountered.  The final sequence number
+in a range has the string "E0" appended to it, which doesn't affect
+its numeric value, but gives you something to search for if you want
+to exclude the endpoint.  You can exclude the beginning point by
+waiting for the sequence number to be greater than 1.
 
 If either operand of scalar ".." is a constant expression,
 that operand is considered true if it is equal (C<==>) to the current
@@ -602,10 +602,10 @@ Examples:
 As a scalar operator:
 
     if (101 .. 200) { print; } # print 2nd hundred lines, short for
-                               #   if ($. == 101 .. $. == 200) ...
+                               #   if ($. == 101 .. $. == 200) { print; }
 
     next LINE if (1 .. /^$/);  # skip header lines, short for
-                               #   ... if ($. == 1 .. /^$/);
+                               #   next LINE if ($. == 1 .. /^$/);
                                # (typically in a loop labeled LINE)
 
     s/^/> / if (/^$/ .. eof());  # quote body
@@ -615,9 +615,9 @@ As a scalar operator:
         $in_header =   1  .. /^$/;
         $in_body   = /^$/ .. eof;
         if ($in_header) {
-            # ...
+            # do something
         } else { # in body
-            # ...
+            # do something else
         }
     } continue {
         close ARGV if eof;             # reset $. each file
@@ -783,10 +783,17 @@ In list context, it's just the list argument separator, and inserts
 both its arguments into the list.  These arguments are also evaluated
 from left to right.
 
-The C<< => >> operator is a synonym for the comma, but forces any word
-(consisting entirely of word characters) to its left to be interpreted
-as a string (as of 5.001).  This includes words that might otherwise be
-considered a constant or function call.
+The C<< => >> operator is a synonym for the comma except that it causes
+its left operand to be interpreted as a string if it begins with a letter
+or underscore and is composed only of letters, digits and underscores.
+This includes operands that might otherwise be interpreted as operators,
+constants, single number v-strings or function calls. If in doubt about
+this behaviour, the left operand can be quoted explicitly.
+
+Otherwise, the C<< => >> operator behaves exactly as the comma operator
+or list argument separator, according to context.
+
+For example:
 
     use constant FOO => "something";
 
@@ -800,15 +807,73 @@ It is I<NOT>:
 
     my %h = ("something", 23);
 
-If the argument on the left is not a word, it is first interpreted as
-an expression, and then the string value of that is used.
-
 The C<< => >> operator is helpful in documenting the correspondence
 between keys and values in hashes, and other paired elements in lists.
 
         %hash = ( $key => $value );
         login( $username => $password );
 
+=head2 Yada Yada Operator
+X<...> X<... operator> X<yada yada operator>
+
+The yada yada operator (noted C<...>) is a placeholder for code. Perl
+parses it without error, but when you try to execute a yada yada, it
+throws an exception with the text C<Unimplemented>:
+
+       sub unimplemented { ... }
+       
+       eval { unimplemented() };
+       if( $@ eq 'Unimplemented' ) {
+         print "I found the yada yada!\n";
+         }
+
+You can only use the yada yada to stand in for a complete statement.
+These examples of the yada yada work:
+
+       { ... }
+       
+       sub foo { ... }
+       
+       ...;
+       
+       eval { ... };
+       
+       sub foo {
+                       my( $self ) = shift;
+                       
+                       ...;
+                       }
+                       
+       do { my $n; ...; print 'Hurrah!' };
+
+The yada yada cannot stand in for an expression that is part of a
+larger statement since the C<...> is also the three-dot version of the
+range operator (see L<Range Operators>). These examples of the yada
+yada are still syntax errors:
+
+       print ...;
+       
+       open my($fh), '>', '/dev/passwd' or ...;
+       
+       if( $condition && ... ) { print "Hello\n" };
+
+There are some cases where Perl can't immediately tell the difference
+between an expression and a statement. For instance, the syntax for a
+block and an anonymous hash reference constructor look the same unless
+there's something in the braces that give Perl a hint. The yada yada
+is a syntax error if Perl doesn't guess that the C<{ ... }> is a
+block. In that case, it doesn't think the C<...> is the yada yada
+because it's expecting an expression instead of a statement:
+
+       my @transformed = map { ... } @input;  # syntax error
+
+You can use a C<;> inside your block to denote that the C<{ ... }> is
+a block and not a hash reference constructor. Now the yada yada works:
+
+       my @transformed = map {; ... } @input; # ; disambiguates
+
+       my @transformed = map { ...; } @input; # ; disambiguates
+
 =head2 List Operators (Rightward)
 X<operator, list, rightward> X<list operator>
 
@@ -838,9 +903,9 @@ precedence.  This means that it short-circuits: i.e., the right
 expression is evaluated only if the left expression is true.
 
 =head2 Logical or, Defined or, and Exclusive Or
-X<operator, logical, or> X<operator, logical, xor> X<operator, logical, err>
+X<operator, logical, or> X<operator, logical, xor>
 X<operator, logical, defined or> X<operator, logical, exclusive or>
-X<or> X<xor> X<err>
+X<or> X<xor>
 
 Binary "or" returns the logical disjunction of the two surrounding
 expressions.  It's equivalent to || except for the very low precedence.
@@ -865,13 +930,6 @@ takes higher precedence.
 
 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')>. This
-keyword is only available when the 'err' feature is enabled: see
-L<feature> for more information.
-
 Binary "xor" returns the exclusive-OR of the two surrounding expressions.
 It cannot short circuit, of course.
 
@@ -1141,11 +1199,13 @@ process modifiers are available:
     c  Do not reset search position on a failed match when /g is in effect.
 
 If "/" is the delimiter then the initial C<m> is optional.  With the C<m>
-you can use any pair of non-alphanumeric, non-whitespace characters
+you can use any pair of non-whitespace characters
 as delimiters.  This is particularly useful for matching path names
 that contain "/", to avoid LTS (leaning toothpick syndrome).  If "?" is
 the delimiter, then the match-only-once rule of C<?PATTERN?> applies.
 If "'" is the delimiter, no interpolation is performed on the PATTERN.
+When using a character valid in an identifier, whitespace is required
+after the C<m>.
 
 PATTERN may contain variables, which will be interpolated (and the
 pattern recompiled) every time the pattern search is evaluated, except
@@ -1158,6 +1218,8 @@ the life of the script.  However, mentioning C</o> constitutes a promise
 that you won't change the variables in the pattern.  If you change them,
 Perl won't even notice.  See also L<"qr/STRING/msixpo">.
 
+=item The empty pattern //
+
 If the PATTERN evaluates to the empty string, the last
 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 -
@@ -1174,6 +1236,8 @@ 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//>).
 
+=item Matching in list context
+
 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
 pattern, i.e., (C<$1>, C<$2>, C<$3>...).  (Note that here C<$1> etc. are
@@ -1220,6 +1284,8 @@ search position to the beginning of the string, but you can avoid that
 by adding the C</c> modifier (e.g. C<m//gc>).  Modifying the target
 string also resets the search position.
 
+=item \G assertion
+
 You can intermix C<m//g> matches with C<m/\G.../g>, where C<\G> is a
 zero-width assertion that matches the exact position where the previous
 C<m//g>, if any, left off.  Without the C</g> modifier, the C<\G> assertion
@@ -1267,7 +1333,7 @@ The last example should print:
 
 Notice that the final match matched C<q> instead of C<p>, which a match
 without the C<\G> anchor would have done. Also note that the final match
-did not update C<pos> -- C<pos> is only updated on a C</g> match. If the
+did not update C<pos>. C<pos> is only updated on a C</g> match. If the
 final match did indeed match C<p>, it's a good bet that you're running an
 older (pre-5.6.0) Perl.
 
@@ -1277,7 +1343,7 @@ doing different actions depending on which regexp matched.  Each
 regexp tries to match where the previous one leaves off.
 
  $_ = <<'EOL';
-      $url = URI::URL->new( "http://www/" );   die if $url eq "xXx";
+      $url = URI::URL->new( "http://example.com/" ); die if $url eq "xXx";
  EOL
  LOOP:
     {
@@ -1349,13 +1415,13 @@ specific options:
     e  Evaluate the right side as an expression.
     ee  Evaluate the right side as a string then eval the result
 
-Any non-alphanumeric, non-whitespace delimiter may replace the
-slashes.  If single quotes are used, no interpretation is done on the
-replacement string (the C</e> modifier overrides this, however).  Unlike
-Perl 4, Perl 5 treats backticks as normal delimiters; the replacement
-text is not evaluated as a command.  If the
-PATTERN is delimited by bracketing quotes, the REPLACEMENT has its own
-pair of quotes, which may or may not be bracketing quotes, e.g.,
+Any non-whitespace delimiter may replace the slashes.  Add space after
+the C<s> when using a character allowed in identifiers.  If single quotes
+are used, no interpretation is done on the replacement string (the C</e>
+modifier overrides this, however).  Unlike Perl 4, Perl 5 treats backticks
+as normal delimiters; the replacement text is not evaluated as a command.
+If the PATTERN is delimited by bracketing quotes, the REPLACEMENT has
+its own pair of quotes, which may or may not be bracketing quotes, e.g.,
 C<s(foo)(bar)> or C<< s<foo>/bar/ >>.  A C</e> will cause the
 replacement portion to be treated as a full-fledged Perl expression
 and evaluated right then and there.  It is, however, syntax checked at
@@ -1787,7 +1853,7 @@ 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 end of string identifier are not
-related to Perl's quoting rules -- C<q()>, C<qq()>, and the like 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:
 
@@ -2051,7 +2117,7 @@ which are processed further.
 X<regexp, parse>
 
 Previous steps were performed during the compilation of Perl code,
-but this one happens at run time--although it may be optimized to
+but this one happens at run timealthough it may be optimized to
 be calculated at compile time if appropriate.  After preprocessing
 described above, and possibly after evaluation if concatenation,
 joining, casing translation, or metaquoting are involved, the
@@ -2164,8 +2230,8 @@ to terminate the loop, they should be tested for explicitly:
     while (($_ = <STDIN>) ne '0') { ... }
     while (<STDIN>) { last unless $_; ... }
 
-In other boolean contexts, C<< <I<filehandle>> >> without an
-explicit C<defined> test or comparison elicit a warning if the
+In other boolean contexts, C<< <filehandle> >> without an
+explicit C<defined> test or comparison elicits a warning if the
 C<use warnings> pragma or the B<-w>
 command-line switch (the C<$^W> variable) is in effect.
 
@@ -2210,10 +2276,22 @@ is equivalent to the following Perl-like pseudo code:
 except that it isn't so cumbersome to say, and will actually work.
 It really does shift the @ARGV array and put the current filename
 into the $ARGV variable.  It also uses filehandle I<ARGV>
-internally--<> is just a synonym for <ARGV>, which
+internally<> is just a synonym for <ARGV>, which
 is magical.  (The pseudo code above doesn't work because it treats
 <ARGV> as non-magical.)
 
+Since the null filehandle uses the two argument form of L<perlfunc/open>
+it interprets special characters, so if you have a script like this:
+
+    while (<>) {
+        print;
+    }
+
+and call it with C<perl dangerous.pl 'rm -rfv *|'>, it actually opens a
+pipe, executes the C<rm> command and reads C<rm>'s output from that pipe.
+If you want all items in C<@ARGV> to be interpreted as file names, you
+can use the module C<ARGV::readonly> from CPAN.
+
 You can modify @ARGV before the first <> as long as the array ends up
 containing the list of filenames you really want.  Line numbers (C<$.>)
 continue as though the input were one big happy file.  See the example