This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perlexperiment: document the private_use experiment
[perl5.git] / pod / perlop.pod
index 416b844..33e558a 100644 (file)
@@ -65,27 +65,28 @@ operators of the same precedence (but never with operators of different
 precedence).  This chaining means that each comparison is performed
 on the two arguments surrounding it, with each interior argument taking
 part in two comparisons, and the comparison results are implicitly ANDed.
-Thus S<C<"$a E<lt> $b E<lt>= $c">> behaves exactly like S<C<"$a E<lt>
-$b && $b E<lt>= $c">>, assuming that C<"$b"> is as simple a scalar as
+Thus S<C<"$x E<lt> $y E<lt>= $z">> behaves exactly like S<C<"$x E<lt>
+$y && $y E<lt>= $z">>, assuming that C<"$y"> is as simple a scalar as
 it looks.  The ANDing short-circuits just like C<"&&"> does, stopping
 the sequence of comparisons as soon as one yields false.
 
 In a chained comparison, each argument expression is evaluated at most
-once, even if it takes part in two comparisons.  (It is not evaluated
+once, even if it takes part in two comparisons, but the result of the
+evaluation is fetched for each comparison.  (It is not evaluated
 at all if the short-circuiting means that it's not required for any
 comparisons.)  This matters if the computation of an interior argument
 is expensive or non-deterministic.  For example,
 
-    if($a < expensive_sub() <= $c) { ...
+    if($x < expensive_sub() <= $z) { ...
 
 is not entirely like
 
-    if($a < expensive_sub() && expensive_sub() <= $c) { ...
+    if($x < expensive_sub() && expensive_sub() <= $z) { ...
 
 but instead closer to
 
     my $tmp = expensive_sub();
-    if($a < $tmp && $tmp <= $c) { ...
+    if($x < $tmp && $tmp <= $z) { ...
 
 in that the subroutine is only called once.  However, it's not exactly
 like this latter code either, because the chained comparison doesn't
@@ -98,9 +99,23 @@ a tied scalar, then the expression is evaluated to produce that scalar
 at most once, but the value of that scalar may be fetched up to twice,
 once for each comparison in which it is actually used.
 
+In this example, the expression is evaluated only once, and the tied
+scalar (the result of the expression) is fetched for each comparison that
+uses it.
+
+    if ($x < $tied_scalar < $z) { ...
+
+In the next example, the expression is evaluated only once, and the tied
+scalar is fetched once as part of the operation within the expression.
+The result of that operation is fetched for each comparison, which
+normally doesn't matter unless that expression result is also magical due
+to operator overloading.
+
+    if ($x < $tied_scalar + 42 < $z) { ...
+
 Some operators are instead non-associative, meaning that it is a syntax
 error to use a sequence of those operators of the same precedence.
-For example, S<C<"$a .. $b .. $c">> is an error.
+For example, S<C<"$x .. $y .. $z">> is an error.
 
 Perl operators have the following associativity and precedence,
 listed from highest precedence to lowest.  Operators borrowed from
@@ -113,7 +128,7 @@ values only, not array values.
     left       ->
     nonassoc   ++ --
     right      **
-    right      ! ~ \ and unary + and -
+    right      ! ~ ~. \ and unary + and -
     left       =~ !~
     left       * / % x
     left       + - .
@@ -122,8 +137,8 @@ values only, not array values.
     chained    < > <= >= lt gt le ge
     chain/na   == != eq ne <=> cmp ~~
     nonassoc    isa
-    left       &
-    left       | ^
+    left       & &.
+    left       | |. ^ ^.
     left       &&
     left       || //
     nonassoc   ..  ...
@@ -560,8 +575,8 @@ Binary C<"ge"> returns true if the left argument is stringwise greater
 than or equal to the right argument.
 X<< ge >>
 
-A sequence of relational operators, such as S<C<"$a E<lt> $b E<lt>=
-$c">>, performs chained comparisons, in the manner described above in
+A sequence of relational operators, such as S<C<"$x E<lt> $y E<lt>=
+$z">>, performs chained comparisons, in the manner described above in
 the section L</"Operator Precedence and Associativity">.
 Beware that they do not chain with equality operators, which have lower
 precedence.
@@ -585,8 +600,8 @@ Binary C<"ne"> returns true if the left argument is stringwise not equal
 to the right argument.
 X<ne>
 
-A sequence of the above equality operators, such as S<C<"$a == $b ==
-$c">>, performs chained comparisons, in the manner described above in
+A sequence of the above equality operators, such as S<C<"$x == $y ==
+$z">>, performs chained comparisons, in the manner described above in
 the section L</"Operator Precedence and Associativity">.
 Beware that they do not chain with relational operators, which have
 higher precedence.
@@ -1199,6 +1214,14 @@ If you want to force strings to be interpreted as numbers, you could say
 
     @numbers = ( 0+$first .. 0+$last );
 
+B<Note:> In Perl versions 5.30 and below, I<any> string on the left-hand
+side beginning with C<"0">, including the string C<"0"> itself, would
+cause the magic string increment behavior. This means that on these Perl
+versions, C<"0".."-1"> would produce C<"0"> through C<"99">, which was
+inconsistent with C<0..-1>, which produces the empty list. This also means
+that C<"0".."9"> now produces a list of integers instead of a list of
+strings.
+
 =item *
 
 If the initial value specified isn't part of a magical increment
@@ -1571,6 +1594,8 @@ is a word character (meaning it matches C</\w/>):
 
 The following escape sequences are available in constructs that interpolate,
 and in transliterations whose delimiters aren't single quotes (C<"'">).
+In all the ones with braces, any number of blanks and/or tabs adjoining
+and within the braces are allowed (and ignored).
 X<\t> X<\n> X<\r> X<\f> X<\b> X<\a> X<\e> X<\x> X<\0> X<\c> X<\N> X<\N{}>
 X<\o{}>
 
@@ -1583,6 +1608,8 @@ X<\o{}>
     \a                  alarm (bell)      (BEL)
     \e                  escape            (ESC)
     \x{263A}     [1,8]  hex char          (example shown: SMILEY)
+    \x{ 263A }          Same, but shows optional blanks inside and
+                        adjoining the braces
     \x1b         [2,8]  restricted range hex char (example: ESC)
     \N{name}     [3]    named Unicode character or character sequence
     \N{U+263D}   [4,8]  Unicode character (example: FIRST QUARTER MOON)
@@ -1590,6 +1617,11 @@ X<\o{}>
     \o{23072}    [6,8]  octal char        (example: SMILEY)
     \033         [7,8]  restricted range octal char  (example: ESC)
 
+Note that any escape sequence using braces inside interpolated
+constructs may have optional blanks (tab or space characters) adjoining
+with and inside of the braces, as illustrated above by the second
+S<C<\x{ }>> example.
+
 =over 4
 
 =item [1]
@@ -1597,10 +1629,13 @@ X<\o{}>
 The result is the character specified by the hexadecimal number between
 the braces.  See L</[8]> below for details on which character.
 
-Only hexadecimal digits are valid between the braces.  If an invalid
-character is encountered, a warning will be issued and the invalid
-character and all subsequent characters (valid or invalid) within the
-braces will be discarded.
+Blanks (tab or space characters) may separate the number from either or
+both of the braces.
+
+Otherwise, only hexadecimal digits are valid between the braces.  If an
+invalid character is encountered, a warning will be issued and the
+invalid character and all subsequent characters (valid or invalid)
+within the braces will be discarded.
 
 If there are no valid digits between the braces, the generated character is
 the NULL character (C<\x{00}>).  However, an explicit empty brace (C<\x{}>)
@@ -1686,10 +1721,13 @@ To get platform independent controls, you can use C<\N{...}>.
 The result is the character specified by the octal number between the braces.
 See L</[8]> below for details on which character.
 
-If a character that isn't an octal digit is encountered, a warning is raised,
-and the value is based on the octal digits before it, discarding it and all
-following characters up to the closing brace.  It is a fatal error if there are
-no octal digits at all.
+Blanks (tab or space characters) may separate the number from either or
+both of the braces.
+
+Otherwise, if a character that isn't an octal digit is encountered, a
+warning is raised, and the value is based on the octal digits before it,
+discarding it and all following characters up to the closing brace.  It
+is a fatal error if there are no octal digits at all.
 
 =item [7]
 
@@ -2396,13 +2434,16 @@ X<qx> X<`> X<``> X<backtick>
 =item C<`I<STRING>`>
 
 A string which is (possibly) interpolated and then executed as a
-system command with F</bin/sh> or its equivalent.  Shell wildcards,
-pipes, and redirections will be honored.  The collected standard
-output of the command is returned; standard error is unaffected.  In
-scalar context, it comes back as a single (potentially multi-line)
-string, or C<undef> if the command failed.  In list context, returns a
+system command, via F</bin/sh> or its equivalent if required.  Shell
+wildcards, pipes, and redirections will be honored.  Similarly to
+C<system>, if the string contains no shell metacharacters then it will
+executed directly.  The collected standard output of the command is
+returned; standard error is unaffected.  In scalar context, it comes
+back as a single (potentially multi-line) string, or C<undef> if the
+shell (or command) could not be started.  In list context, returns a
 list of lines (however you've defined lines with C<$/> or
-C<$INPUT_RECORD_SEPARATOR>), or an empty list if the command failed.
+C<$INPUT_RECORD_SEPARATOR>), or an empty list if the shell (or command)
+could not be started.
 
 Because backticks do not affect standard error, use shell file descriptor
 syntax (assuming the shell supports this) if you care to address this.
@@ -2558,7 +2599,8 @@ Unless the C</r> option is used, the string specified with C<=~> must be a
 scalar variable, an array element, a hash element, or an assignment to one
 of those; in other words, an lvalue.
 
-If the characters delimiting I<SEARCHLIST> and I<REPLACEMENTLIST>
+The characters delimitting I<SEARCHLIST> and I<REPLACEMENTLIST>
+can be any printable character, not just forward slashes.  If they
 are single quotes (C<tr'I<SEARCHLIST>'I<REPLACEMENTLIST>'>), the only
 interpolation is removal of C<\> from pairs of C<\\>.
 
@@ -2644,8 +2686,8 @@ If the C</s> modifier is specified, sequences of characters, all in a
 row, that were transliterated to the same character are squashed down to
 a single instance of that character.
 
- my $a = "aaaba"
- $a =~ tr/a/a/s     # $a now is "aba"
+ my $a = "aaabbbca";
+ $a =~ tr/ab/dd/s;     # $a now is "dcd"
 
 If the C</d> modifier is used, the I<REPLACEMENTLIST> is always interpreted
 exactly as specified.  Otherwise, if the I<REPLACEMENTLIST> is shorter
@@ -3349,7 +3391,8 @@ way, so use with care.
 C<< <I<FILEHANDLE>> >>  may also be spelled C<readline(*I<FILEHANDLE>)>.
 See L<perlfunc/readline>.
 
-The null filehandle C<< <> >> is special: it can be used to emulate the
+The null filehandle C<< <> >> (sometimes called the diamond operator) is
+special: it can be used to emulate the
 behavior of B<sed> and B<awk>, and any other Unix filter program
 that takes a list of filenames, doing the same to each line
 of input from all of them.  Input from C<< <> >> comes either from
@@ -3390,7 +3433,8 @@ it interprets special characters, so if you have a script like this:
 and call it with S<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, or use the double bracket:
+can use the module C<ARGV::readonly> from CPAN, or use the double
+diamond bracket:
 
     while (<<>>) {
         print;