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>.
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
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.
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>
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>.)
-If S<C<use integer>> (see L<Integer Arithmetic>) is in force then
+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,
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>
...
}
-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>.
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
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
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>
This operator quotes (and possibly compiles) its I<STRING> as a regular
expression. I<STRING> is interpolated the same way as I<PATTERN>
-in C<m/I<PATTERN>/>. If C<"'"> is used as the delimiter, no interpolation
-is done. Returns a Perl value which may be used instead of the
+in C<m/I<PATTERN>/>. If C<"'"> is used as the delimiter, no variable
+interpolation is done. Returns a Perl value which may be used instead of the
corresponding C</I<STRING>/msixpodualn> expression. The returned value is a
normalized version of the original pattern. It magically differs from
a string containing the same characters: C<ref(qr/x/)> returns "Regexp";
that contain C<"/">, to avoid LTS (leaning toothpick syndrome). If C<"?"> is
the delimiter, then a match-only-once rule applies,
described in C<m?I<PATTERN>?> below. If C<"'"> (single quote) is the delimiter,
-no interpolation is performed on the I<PATTERN>.
+no variable interpolation is performed on the I<PATTERN>.
When using a delimiter character valid in an identifier, whitespace is required
after the C<m>.
hash element, or an assignment to one of those; that is, some sort of
scalar lvalue.
-If the delimiter chosen is a single quote, no interpolation is
+If the delimiter chosen is a single quote, no variable interpolation is
done on either the I<PATTERN> or the I<REPLACEMENT>. Otherwise, if the
I<PATTERN> contains a C<$> that looks like a variable rather than an
end-of-string test, the variable will be interpolated into the pattern
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>/>
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
+double-quoted strings. But there is no variable 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>.
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).
+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
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
+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.
the quoted material, and all lines following the current line down to
the terminating string are the value of the item.
+Prefixing the terminating string with a C<~> specifies that you
+want to use L</Indented Here-docs> (see below).
+
The terminating string may be either an identifier (a word), or some
quoted text. An unquoted identifier works like double quotes.
There may not be a space between the C<< << >> and the identifier,
=back
+=over 4
+
+=item Indented Here-docs
+
+The here-doc modifier C<~> allows you to indent your here-docs to make
+the code more readable:
+
+ if ($some_var) {
+ print <<~EOF;
+ This is a here-doc
+ EOF
+ }
+
+This will print...
+
+ This is a here-doc
+
+...with no leading whitespace.
+
+The delimiter is used to determine the B<exact> whitespace to
+remove from the beginning of each line. All lines B<must> have
+at least the same starting whitespace (except lines only
+containing a newline) or perl will croak. Tabs and spaces can
+be mixed, but are matched exactly. One tab will not be equal to
+8 spaces!
+
+Additional beginning whitespace (beyond what preceded the
+delimiter) will be preserved:
+
+ print <<~EOF;
+ This text is not indented
+ This text is indented with two spaces
+ This text is indented with two tabs
+ EOF
+
+Finally, the modifier may be used with all of the forms
+mentioned above:
+
+ <<~\EOF;
+ <<~'EOF'
+ <<~"EOF"
+ <<~`EOF`
+
+And whitespace may be used between the C<~> and quoted delimiters:
+
+ <<~ 'EOF'; # ... "EOF", `EOF`
+
+=back
+
It is possible to stack multiple here-docs in a row:
print <<"foo", <<"bar"; # you can stack them
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