This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perldelta for #129164 / 92b69f650
[perl5.git] / pod / perlop.pod
index 631b391..d65e911 100644 (file)
@@ -71,7 +71,8 @@ values only, not array values.
     left       and
     left       or xor
 
-In the following sections, these operators are covered in precedence order.
+In the following sections, these operators are covered in detail, in the
+same order in which they appear in the table above.
 
 Many operators can be overloaded for objects.  See L<overload>.
 
@@ -128,13 +129,13 @@ To do what you meant properly, you must write:
 
     print(($foo & 255) + 1, "\n");
 
-See L<Named Unary Operators> for more discussion of this.
+See L</Named Unary Operators> for more discussion of this.
 
 Also parsed as terms are the S<C<do {}>> and S<C<eval {}>> constructs, as
 well as subroutine and method calls, and the anonymous
 constructors C<[]> and C<{}>.
 
-See also L<Quote and Quote-like Operators> toward the end of this section,
+See also L</Quote and Quote-like Operators> toward the end of this section,
 as well as L</"I/O Operators">.
 
 =head2 The Arrow Operator
@@ -235,8 +236,8 @@ B<Argument "the string" isn't numeric in negation (-) at ...>.
 X<-> X<negation, arithmetic>
 
 Unary C<"~"> performs bitwise negation, that is, 1's complement.  For
-example, S<C<0666 & ~027>> is 0640.  (See also L<Integer Arithmetic> and
-L<Bitwise String Operators>.)  Note that the width of the result is
+example, S<C<0666 & ~027>> is 0640.  (See also L</Integer Arithmetic> and
+L</Bitwise String Operators>.)  Note that the width of the result is
 platform-dependent: C<~0> is 32 bits wide on a 32-bit platform, but 64
 bits wide on a 64-bit platform, so if you are expecting a certain bit
 width, remember to use the C<"&"> operator to mask off the excess bits.
@@ -258,7 +259,7 @@ produces a warning unless you use S<C<no warnings 'experimental::bitwise'>>.
 Unary C<"+"> has no effect whatsoever, even on strings.  It is useful
 syntactically for separating a function name from a parenthesized expression
 that would otherwise be interpreted as the complete list of function
-arguments.  (See examples above under L<Terms and List Operators (Leftward)>.)
+arguments.  (See examples above under L</Terms and List Operators (Leftward)>.)
 X<+>
 
 Unary C<"\"> creates a reference to whatever follows it.  See L<perlreftut>
@@ -370,23 +371,34 @@ X<shl> X<shr> X<shift, right> X<shift, left>
 
 Binary C<<< "<<" >>> returns the value of its left argument shifted left by the
 number of bits specified by the right argument.  Arguments should be
-integers.  (See also L<Integer Arithmetic>.)
+integers.  (See also L</Integer Arithmetic>.)
 
 Binary C<<< ">>" >>> returns the value of its left argument shifted right by
 the number of bits specified by the right argument.  Arguments should
-be integers.  (See also L<Integer Arithmetic>.)
+be integers.  (See also L</Integer Arithmetic>.)
 
-Note that both C<<< << >>> and C<<< >> >>> in Perl are implemented directly using
-C<<< << >>> and C<<< >> >>>  in C.  If S<C<use integer>> (see L<Integer Arithmetic>) is
-in force then signed C integers are used, else unsigned C integers are
-used, even for negative shiftees.  Either way, the implementation isn't going to generate results
-larger than the size of the integer type Perl was built with (32 bits
-or 64 bits).
+If S<C<use integer>> (see L</Integer Arithmetic>) is in force then
+signed C integers are used (I<arithmetic shift>), otherwise unsigned C
+integers are used (I<logical shift>), even for negative shiftees.
+In arithmetic right shift the sign bit is replicated on the left,
+in logical shift zero bits come in from the left.
 
-The result of overflowing the range of the integers is undefined
-because it is undefined also in C.  In other words, using 32-bit
-integers, S<C<< 1 << 32 >>> is undefined.  Shifting by a negative number
-of bits is also undefined.
+Either way, the implementation isn't going to generate results larger
+than the size of the integer type Perl was built with (32 bits or 64 bits).
+
+Shifting by negative number of bits means the reverse shift: left
+shift becomes right shift, right shift becomes left shift.  This is
+unlike in C, where negative shift is undefined.
+
+Shifting by more bits than the size of the integers means most of the
+time zero (all bits fall off), except that under S<C<use integer>>
+right overshifting a negative shiftee results in -1.  This is unlike
+in C, where shifting by too many bits is undefined.  A common C
+behavior is "shift by modulo wordbits", so that for example
+
+    1 >> 64 == 1 >> (64 % 64) == 1 >> 0 == 1  # Common C behavior.
+
+but that is completely accidental.
 
 If you get tired of being subject to your platform's native integers,
 the S<C<use bigint>> pragma neatly sidesteps the issue altogether:
@@ -432,7 +444,7 @@ parenthesis rule.  That means, for example, that C<-f($file).".bak"> is
 equivalent to S<C<-f "$file.bak">>.
 X<-X> X<filetest> X<operator, filetest>
 
-See also L<"Terms and List Operators (Leftward)">.
+See also L</"Terms and List Operators (Leftward)">.
 
 =head2 Relational Operators
 X<relational operator> X<operator, relational>
@@ -764,19 +776,11 @@ might have.  For example:
         ...
     }
 
-or, if other non-required fields are allowed, use ARRAY ~~ HASH:
-
-    use v5.10.1;
-    sub make_dogtag {
-        state $REQUIRED_FIELDS = { name=>1, rank=>1, serial_num=>1 };
-
-        my ($class, $init_fields) = @_;
-
-        die "Must supply (at least) name, rank, and serial number"
-            unless [keys %{$init_fields}] ~~ $REQUIRED_FIELDS;
-
-        ...
-    }
+However, this only does what you mean if C<$init_fields> is indeed a hash
+reference. The condition C<$init_fields ~~ $REQUIRED_FIELDS> also allows the
+strings C<"name">, C<"rank">, C<"serial_num"> as well as any array reference
+that contains C<"name"> or C<"rank"> or C<"serial_num"> anywhere to pass
+through.
 
 The smartmatch operator is most often used as the implicit operator of a
 C<when> clause.  See the section on "Switch Statements" in L<perlsyn>.
@@ -834,7 +838,7 @@ X<operator, bitwise, and> X<bitwise and> X<&>
 Binary C<"&"> returns its operands ANDed together bit by bit.  Although no
 warning is currently raised, the result is not well defined when this operation
 is performed on operands that aren't either numbers (see
-L<Integer Arithmetic>) nor bitstrings (see L<Bitwise String Operators>).
+L</Integer Arithmetic>) nor bitstrings (see L</Bitwise String Operators>).
 
 Note that C<"&"> has lower priority than relational operators, so for example
 the parentheses are essential in a test like
@@ -856,7 +860,7 @@ Binary C<"^"> returns its operands XORed together bit by bit.
 
 Although no warning is currently raised, the results are not well
 defined when these operations are performed on operands that aren't either
-numbers (see L<Integer Arithmetic>) nor bitstrings (see L<Bitwise String
+numbers (see L</Integer Arithmetic>) nor bitstrings (see L</Bitwise String
 Operators>).
 
 Note that C<"|"> and C<"^"> have lower priority than relational operators, so
@@ -913,9 +917,9 @@ portable way to find out the home directory might be:
 In particular, this means that you shouldn't use this
 for selecting between two aggregates for assignment:
 
-    @a = @b || @c;             # this is wrong
-    @a = scalar(@b) || @c;     # really meant this
-    @a = @b ? @b : @c;         # this works fine, though
+    @a = @b || @c;            # This doesn't do the right thing
+    @a = scalar(@b) || @c;    # because it really means this.
+    @a = @b ? @b : @c;        # This works fine, though.
 
 As alternatives to C<&&> and C<||> when used for
 control flow, Perl provides the C<and> and C<or> operators (see below).
@@ -1269,7 +1273,7 @@ in which case you might as well just use the more customary C<"||"> operator:
 
     open(HANDLE, "< :utf8", "filename") || die "Can't open: $!\n";
 
-See also discussion of list operators in L<Terms and List Operators (Leftward)>.
+See also discussion of list operators in L</Terms and List Operators (Leftward)>.
 
 =head2 Logical Not
 X<operator, logical, not> X<not>
@@ -2298,6 +2302,27 @@ when they're the right way to get something done.  Perl was made to be
 a glue language, and one of the things it glues together is commands.
 Just understand what you're getting yourself into.
 
+Like C<system>, backticks put the child process exit code in C<$?>.
+If you'd like to manually inspect failure, you can check all possible
+failure modes by inspecting C<$?> like this:
+
+    if ($? == -1) {
+        print "failed to execute: $!\n";
+    }
+    elsif ($? & 127) {
+        printf "child died with signal %d, %s coredump\n",
+            ($? & 127),  ($? & 128) ? 'with' : 'without';
+    }
+    else {
+        printf "child exited with value %d\n", $? >> 8;
+    }
+
+Use the L<open> pragma to control the I/O layers used when reading the
+output of the command, for example:
+
+  use open IN => ":encoding(UTF-8)";
+  my $x = `cmd-producing-utf-8`;
+
 See L</"I/O Operators"> for more discussion.
 
 =item C<qw/I<STRING>/>
@@ -2352,35 +2377,63 @@ of those; in other words, an lvalue.
 A character range may be specified with a hyphen, so C<tr/A-J/0-9/>
 does the same replacement as C<tr/ACEGIBDFHJ/0246813579/>.
 For B<sed> devotees, C<y> is provided as a synonym for C<tr>.  If the
-I<SEARCHLIST> is delimited by bracketing quotes, the I<REPLACEMENTLIST> has
-its own pair of quotes, which may or may not be bracketing quotes;
-for example, C<tr[aeiouy][yuoiea]> or C<tr(+\-*/)/ABCD/>.
+I<SEARCHLIST> is delimited by bracketing quotes, the I<REPLACEMENTLIST>
+must have its own pair of quotes, which may or may not be bracketing
+quotes; for example, C<tr[aeiouy][yuoiea]> or C<tr(+\-*/)/ABCD/>.
 
 Characters may be literals or any of the escape sequences accepted in
 double-quoted strings.  But there is no interpolation, so C<"$"> and
 C<"@"> are treated as literals.  A hyphen at the beginning or end, or
 preceded by a backslash is considered a literal.  Escape sequence
 details are in L<the table near the beginning of this section|/Quote and
-Quote-like Operators>.  It is a bug in Perl v5.22 that something like
-
- tr/\N{U+20}-\N{U+7E}foobar//
-
-does not treat that range as fully Unicode.
+Quote-like Operators>.
 
 Note that C<tr> does B<not> do regular expression character classes such as
 C<\d> or C<\pL>.  The C<tr> operator is not equivalent to the C<L<tr(1)>>
-utility.  If you want to map strings between lower/upper cases, see
-L<perlfunc/lc> and L<perlfunc/uc>, and in general consider using the C<s>
-operator if you need regular expressions.  The C<\U>, C<\u>, C<\L>, and
-C<\l> string-interpolation escapes on the right side of a substitution
-operator will perform correct case-mappings, but C<tr[a-z][A-Z]> will not
-(except sometimes on legacy 7-bit data).
-
-Note also that the whole range idea is rather unportable between
-character sets--and even within character sets they may cause results
-you probably didn't expect.  A sound principle is to use only ranges
-that begin from and end at either alphabets of equal case (a-e, A-E),
-or digits (0-4).  Anything else is unsafe.  If in doubt, spell out the
+utility.  C<tr[a-z][A-Z]> will uppercase the 26 letters "a" through "z",
+but for case changing not confined to ASCII, use
+L<C<lc>|perlfunc/lc>, L<C<uc>|perlfunc/uc>,
+L<C<lcfirst>|perlfunc/lcfirst>, L<C<ucfirst>|perlfunc/ucfirst>
+(all documented in L<perlfunc>), or the
+L<substitution operator C<sE<sol>I<PATTERN>E<sol>I<REPLACEMENT>E<sol>>|/sE<sol>PATTERNE<sol>REPLACEMENTE<sol>msixpodualngcer>
+(with C<\U>, C<\u>, C<\L>, and C<\l> string-interpolation escapes in the
+I<REPLACEMENT> portion).
+
+Most ranges are unportable between character sets, but certain ones
+signal Perl to do special handling to make them portable.  There are two
+classes of portable ranges.  The first are any subsets of the ranges
+C<A-Z>, C<a-z>, and C<0-9>, when expressed as literal characters.
+
+  tr/h-k/H-K/
+
+capitalizes the letters C<"h">, C<"i">, C<"j">, and C<"k"> and nothing
+else, no matter what the platform's character set is.  In contrast, all
+of
+
+  tr/\x68-\x6B/\x48-\x4B/
+  tr/h-\x6B/H-\x4B/
+  tr/\x68-k/\x48-K/
+
+do the same capitalizations as the previous example when run on ASCII
+platforms, but something completely different on EBCDIC ones.
+
+The second class of portable ranges is invoked when one or both of the
+range's end points are expressed as C<\N{...}>
+
+ $string =~ tr/\N{U+20}-\N{U+7E}//d;
+
+removes from C<$string> all the platform's characters which are
+equivalent to any of Unicode U+0020, U+0021, ... U+007D, U+007E.  This
+is a portable range, and has the same effect on every platform it is
+run on.  It turns out that in this example, these are the ASCII
+printable characters.  So after this is run, C<$string> has only
+controls and characters which have no ASCII equivalents.
+
+But, even for portable ranges, it is not generally obvious what is
+included without having to look things up.  A sound principle is to use
+only ranges that begin from and end at either ASCII alphabetics of equal
+case (C<b-e>, C<B-E>), or digits (C<1-4>).  Anything else is unclear
+(and unportable unless C<\N{...}> is used).  If in doubt, spell out the
 character sets in full.
 
 Options:
@@ -3272,7 +3325,7 @@ still get C<1.4142135623731> or so.
 
 Used on numbers, the bitwise operators (C<&> C<|> C<^> C<~> C<< << >>
 C<< >> >>) always produce integral results.  (But see also
-L<Bitwise String Operators>.)  However, S<C<use integer>> still has meaning for
+L</Bitwise String Operators>.)  However, S<C<use integer>> still has meaning for
 them.  By default, their results are interpreted as unsigned integers, but
 if S<C<use integer>> is in effect, their results are interpreted
 as signed integers.  For example, C<~0> usually evaluates to a large