=head2 Symbolic Unary Operators
X<unary operator> X<operator, unary>
-Unary C<"!"> performs logical negation, that is, "not". See also C<not> for a lower
-precedence version of this.
+Unary C<"!"> performs logical negation, that is, "not". See also
+L<C<not>|/Logical Not> for a lower precedence version of this.
X<!>
Unary C<"-"> performs arithmetic negation if the operand is numeric,
the S<C<use bigint>> pragma neatly sidesteps the issue altogether:
print 20 << 20; # 20971520
- print 20 << 40; # 5120 on 32-bit machines,
+ print 20 << 40; # 5120 on 32-bit machines,
# 21990232555520 on 64-bit machines
use bigint;
print 20 << 100; # 25353012004564588029934064107520
=head2 Relational Operators
X<relational operator> X<operator, relational>
-Perl operators that return true or false generally return values
+Perl operators that return true or false generally return values
that can be safely used as numbers. For example, the relational
operators in this section and the equality operators in the next
one return C<1> for true and a special version of the defined empty
actually happens is mostly determined by the type of the second operand,
the table is sorted on the right operand instead of on the left.
- Left Right Description and pseudocode
+ Left Right Description and pseudocode
===============================================================
- Any undef check whether Any is undefined
+ Any undef check whether Any is undefined
like: !defined Any
Any Object invoke ~~ overloading on Object, or die
Right operand is an ARRAY:
- Left Right Description and pseudocode
+ Left Right Description and pseudocode
===============================================================
ARRAY1 ARRAY2 recurse on paired elements of ARRAY1 and ARRAY2[2]
like: (ARRAY1[0] ~~ ARRAY2[0])
&& (ARRAY1[1] ~~ ARRAY2[1]) && ...
- HASH ARRAY any ARRAY elements exist as HASH keys
+ HASH ARRAY any ARRAY elements exist as HASH keys
like: grep { exists HASH->{$_} } ARRAY
Regexp ARRAY any ARRAY elements pattern match Regexp
like: grep { /Regexp/ } ARRAY
- undef ARRAY undef in ARRAY
+ undef ARRAY undef in ARRAY
like: grep { !defined } ARRAY
- Any ARRAY smartmatch each ARRAY element[3]
+ Any ARRAY smartmatch each ARRAY element[3]
like: grep { Any ~~ $_ } ARRAY
Right operand is a HASH:
- Left Right Description and pseudocode
+ Left Right Description and pseudocode
===============================================================
- HASH1 HASH2 all same keys in both HASHes
+ HASH1 HASH2 all same keys in both HASHes
like: keys HASH1 ==
grep { exists HASH2->{$_} } keys HASH1
- ARRAY HASH any ARRAY elements exist as HASH keys
+ ARRAY HASH any ARRAY elements exist as HASH keys
like: grep { exists HASH->{$_} } ARRAY
- Regexp HASH any HASH keys pattern match Regexp
+ Regexp HASH any HASH keys pattern match Regexp
like: grep { /Regexp/ } keys HASH
- undef HASH always false (undef can't be a key)
+ undef HASH always false (undef can't be a key)
like: 0 == 1
- Any HASH HASH key existence
+ Any HASH HASH key existence
like: exists HASH->{Any}
Right operand is CODE:
- Left Right Description and pseudocode
+ Left Right Description and pseudocode
===============================================================
ARRAY CODE sub returns true on all ARRAY elements[1]
like: !grep { !CODE->($_) } ARRAY
HASH CODE sub returns true on all HASH keys[1]
like: !grep { !CODE->($_) } keys HASH
- Any CODE sub passed Any returns true
+ Any CODE sub passed Any returns true
like: CODE->(Any)
Right operand is a Regexp:
- Left Right Description and pseudocode
+ Left Right Description and pseudocode
===============================================================
- ARRAY Regexp any ARRAY elements match Regexp
+ ARRAY Regexp any ARRAY elements match Regexp
like: grep { /Regexp/ } ARRAY
- HASH Regexp any HASH keys match Regexp
+ HASH Regexp any HASH keys match Regexp
like: grep { /Regexp/ } keys HASH
- Any Regexp pattern match
+ Any Regexp pattern match
like: Any =~ /Regexp/
Other:
- Left Right Description and pseudocode
+ Left Right Description and pseudocode
===============================================================
Object Any invoke ~~ overloading on Object,
or fall back to...
- Any Num numeric equality
+ Any Num numeric equality
like: Any == Num
Num nummy[4] numeric equality
like: Num == nummy
undef Any check whether undefined
like: !defined(Any)
- Any Any string equality
+ Any Any string equality
like: Any eq Any
=over
=item 1.
-Empty hashes or arrays match.
+Empty hashes or arrays match.
=item 2.
That is, each element smartmatches the element of the same index in the other array.[3]
=item 3.
-If a circular reference is found, fall back to referential equality.
+If a circular reference is found, fall back to referential equality.
=item 4.
Either an actual number, or a string that looks like one.
my @bigger = ("red", "blue", [ "orange", "green" ] );
if (@little ~~ @bigger) { # true!
say "little is contained in bigger";
- }
+ }
Because the smartmatch operator recurses on nested arrays, this
will still report that "red" is in the array.
copies of each others' values, as this example reports:
use v5.12.0;
- my @a = (0, 1, 2, [3, [4, 5], 6], 7);
- my @b = (0, 1, 2, [3, [4, 5], 6], 7);
+ my @a = (0, 1, 2, [3, [4, 5], 6], 7);
+ my @b = (0, 1, 2, [3, [4, 5], 6], 7);
if (@a ~~ @b && @b ~~ @a) {
say "a and b are deep copies of each other";
- }
+ }
elsif (@a ~~ @b) {
say "a smartmatches in b";
- }
+ }
elsif (@b ~~ @a) {
say "b smartmatches in a";
- }
+ }
else {
say "a and b don't smartmatch each other at all";
- }
+ }
If you were to set S<C<$b[3] = 4>>, then instead of reporting that "a and b
numbers, "in" becomes equivalent to this:
$object ~~ $number ref($object) == $number
- $object ~~ $string ref($object) eq $string
+ $object ~~ $string ref($object) eq $string
For example, this reports that the handle smells IOish
(but please don't really do this!):
my $fh = IO::Handle->new();
if ($fh ~~ /\bIO\b/) {
say "handle smells IOish";
- }
+ }
That's because it treats C<$fh> as a string like
C<"IO::Handle=GLOB(0x8039e0)">, then pattern matches against that.
unless(unlink("alpha", "beta", "gamma")) {
gripe();
next LINE;
- }
+ }
Using C<"or"> for assignment is unlikely to do what you want; see below.
increment would produce, the sequence goes until the next value would
be longer than the final value specified.
+As of Perl 5.26, the list-context range operator on strings works as expected
+in the scope of L<< S<C<"use feature 'unicode_strings">>|feature/The
+'unicode_strings' feature >>. In previous versions, and outside the scope of
+that feature, it exhibits L<perlunicode/The "Unicode Bug">: its behavior
+depends on the internal encoding of the range endpoint.
+
If the initial value specified isn't part of a magical increment
sequence (that is, a non-empty string matching C</^[a-zA-Z]*[0-9]*\z/>),
only the initial value will be returned. So the following will only
you could use this instead:
use charnames "greek";
- my @greek_small = map { chr } ( ord("\N{alpha}")
+ my @greek_small = map { chr } ( ord("\N{alpha}")
..
- ord("\N{omega}")
+ ord("\N{omega}")
);
However, because there are I<many> other lowercase Greek characters than
C<"and">, C<"or">, and C<"not">, which may be used to evaluate calls to list
operators without the need for parentheses:
- open HANDLE, "< :utf8", "filename" or die "Can't open: $!\n";
+ open HANDLE, "< :encoding(UTF-8)", "filename"
+ or die "Can't open: $!\n";
However, some people find that code harder to read than writing
it with parentheses:
- open(HANDLE, "< :utf8", "filename") or die "Can't open: $!\n";
+ open(HANDLE, "< :encoding(UTF-8)", "filename")
+ or die "Can't open: $!\n";
in which case you might as well just use the more customary C<"||"> operator:
- open(HANDLE, "< :utf8", "filename") || die "Can't open: $!\n";
+ open(HANDLE, "< :encoding(UTF-8)", "filename")
+ || die "Can't open: $!\n";
See also discussion of list operators in L</Terms and List Operators (Leftward)>.
character in the 256th position (indexed by 0) in Unicode is
C<LATIN CAPITAL LETTER A WITH MACRON>.
-There are a couple of exceptions to the above rule. S<C<\N{U+I<hex number>}>> is
+An exception to the above rule is that S<C<\N{U+I<hex number>}>> is
always interpreted as a Unicode code point, so that C<\N{U+0050}> is C<"P"> even
-on EBCDIC platforms. And if C<S<L<use encoding|encoding>>> is in effect, the
-number is considered to be in that encoding, and is translated from that into
-the platform's native encoding if there is a corresponding native character;
-otherwise to Unicode.
+on EBCDIC platforms.
=back
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";
-however, dereferencing it is not well defined (you currently get the
+however, dereferencing it is not well defined (you currently get the
normalized version of the original pattern, but this may change).
m Treat string as multiple lines.
s Treat string as single line. (Make . match a newline)
i Do case-insensitive pattern matching.
- x Use extended regular expressions.
+ x Use extended regular expressions; specifying two
+ x's means \t and the SPACE character are ignored within
+ square-bracketed character classes
p When matching preserve a copy of the matched string so
that ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH} will be
defined (ignored starting in v5.20) as these are always
- defined starting in that relese
+ defined starting in that release
o Compile pattern only once.
- a ASCII-restrict: Use ASCII for \d, \s, \w; specifying two
- a's further restricts things to that that no ASCII
- character will match a non-ASCII one under /i.
+ a ASCII-restrict: Use ASCII for \d, \s, \w and [[:posix:]]
+ character classes; specifying two a's adds the further
+ restriction that no ASCII character will match a
+ non-ASCII one under /i.
l Use the current run-time locale's rules.
u Use Unicode rules.
d Use Unicode or native charset, as in 5.12 and earlier.
list consisting of the subexpressions matched by the parentheses in the
pattern, that is, (C<$1>, C<$2>, C<$3>...) (Note that here C<$1> etc. are
also set). When there are no parentheses in the pattern, the return
-value is the list C<(1)> for success.
+value is the list C<(1)> for success.
With or without parentheses, an empty list is returned upon failure.
Examples:
=item C<m?I<PATTERN>?msixpodualngc>
X<?> X<operator, match-once>
-=item C<?I<PATTERN>?msixpodualngc>
-
This is just like the C<m/I<PATTERN>/> search, except that it matches
only once between calls to the C<reset()> operator. This is a useful
optimization when you want to see only the first occurrence of
capable of dealing with multiline commands, so putting newlines in
the string may not get you what you want. You may be able to evaluate
multiple commands in a single line by separating them with the command
-separator character, if your shell supports that (for example, C<;> on
+separator character, if your shell supports that (for example, C<;> on
many Unix shells and C<&> on the Windows NT C<cmd> shell).
Perl will attempt to flush all files opened for
C<[]>, C<{}>, or C<< <> >>), the right part needs another pair of
delimiters such as C<s(){}> and C<tr[]//>. In these cases, whitespace
and comments are allowed between the two parts, although the comment must follow
-at least one whitespace character; otherwise a character expected as the
+at least one whitespace character; otherwise a character expected as the
start of the comment may be regarded as the starting delimiter of the right part.
During this search no attention is paid to the semantics of the construct.
is emitted if the S<C<use warnings>> pragma or the B<-w> command-line flag
(that is, the C<$^W> variable) was set.
-=item C<RE> in C<?RE?>, C</RE/>, C<m/RE/>, C<s/RE/foo/>,
+=item C<RE> in C<m?RE?>, C</RE/>, C<m/RE/>, C<s/RE/foo/>,
Processing of C<\Q>, C<\U>, C<\u>, C<\L>, C<\l>, C<\F>, C<\E>,
and interpolation happens (almost) as with C<qq//> constructs.
the previous step, and C<\\/> will be left as is. Because C</> is
equivalent to C<\/> inside a regular expression, this does not
matter unless the delimiter happens to be character special to the
-RE engine, such as in C<s*foo*bar*>, C<m[foo]>, or C<?foo?>; or an
+RE engine, such as in C<s*foo*bar*>, C<m[foo]>, or C<m?foo?>; or an
alphanumeric char, as in:
m m ^ a \s* b mmx;
print while ($_ = <STDIN>);
print while <STDIN>;
-This also behaves similarly, but assigns to a lexical variable
+This also behaves similarly, but assigns to a lexical variable
instead of to C<$_>:
while (my $line = <STDIN>) { print $line }
compile time. You can say
'Now is the time for all'
- . "\n"
+ . "\n"
. 'good men to come to.'
and this all reduces to one string internally. Likewise, if