left &
left | ^
left &&
- left ||
+ left || //
nonassoc .. ...
right ?:
right = += -= *= etc.
nonassoc list operators (rightward)
right not
left and
- left or xor
+ left or xor err
In the following sections, these operators are covered in precedence order.
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).
-Note than when C<use integer> is in scope, "%" gives you direct access
+Note that when C<use integer> is in scope, "%" gives you direct access
to the modulus operator as implemented by your C compiler. This
operator is not as well defined for negative operands, but it will
execute faster.
=head2 Bitwise And
-Binary "&" returns its operators ANDed together bit by bit.
+Binary "&" returns its operands ANDed together bit by bit.
(See also L<Integer Arithmetic> and L<Bitwise String Operators>.)
+Note that "&" has lower priority than relational operators, so for example
+the brackets are essential in a test like
+
+ print "Even\n" if ($x & 1) == 0;
+
=head2 Bitwise Or and Exclusive Or
-Binary "|" returns its operators ORed together bit by bit.
+Binary "|" returns its operands ORed together bit by bit.
(See also L<Integer Arithmetic> and L<Bitwise String Operators>.)
-Binary "^" returns its operators XORed together bit by bit.
+Binary "^" returns its operands XORed together bit by bit.
(See also L<Integer Arithmetic> and L<Bitwise String Operators>.)
+Note that "|" and "^" have lower priority than relational operators, so
+for example the brackets are essential in a test like
+
+ print "false\n" if (8 | 2) != 10;
+
=head2 C-style Logical And
Binary "&&" performs a short-circuit logical AND operation. That is,
Scalar or list context propagates down to the right operand if it
is evaluated.
-The C<||> and C<&&> operators differ from C's in that, rather than returning
+=head2 C-style Logical Defined-Or
+
+Although it has no direct equivalent in C, Perl's C<//> operator is related
+to its C-style or. In fact, it's exactly the same as C<||>, except that it
+tests the left hand side's definedness instead of its truth. Thus, C<$a // $b>
+is similar to C<defined($a) || $b> (except that it returns the value of C<$a>
+rather than the value of C<defined($a)>) and is exactly equivalent to
+C<defined($a) ? $a : $b>. This is very useful for providing default values
+for variables. If you actually want to test if at least one of C<$a> and C<$b> is
+defined, use C<defined($a // $b)>.
+
+The C<||>, C<//> and C<&&> operators differ from C's in that, rather than returning
0 or 1, they return the last value evaluated. Thus, a reasonably portable
-way to find out the home directory (assuming it's not "0") might be:
+way to find out the home directory might be:
- $home = $ENV{'HOME'} || $ENV{'LOGDIR'} ||
- (getpwuid($<))[7] || die "You're homeless!\n";
+ $home = $ENV{'HOME'} // $ENV{'LOGDIR'} //
+ (getpwuid($<))[7] // die "You're homeless!\n";
In particular, this means that you shouldn't use this
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<&&> and C<||> when used for
-control flow, Perl provides 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
+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"
+and "or" is much lower, however, so that you can safely use them after a
list operator without the need for parentheses:
unlink "alpha", "beta", "gamma"
precedence. This means that it short-circuits: i.e., the right
expression is evaluated only if the left expression is true.
-=head2 Logical or and Exclusive Or
+=head2 Logical or, Defined or, and Exclusive Or
Binary "or" returns the logical disjunction of the two surrounding
expressions. It's equivalent to || except for the very low precedence.
@info = stat($file) || die; # oops, scalar sense of stat!
@info = stat($file) or die; # better, now @info gets its due
-Then again, you could always use parentheses.
+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')>
Binary "xor" returns the exclusive-OR of the two surrounding expressions.
It cannot short circuit, of course.
For constructs that do interpolate, variables beginning with "C<$>"
or "C<@>" are interpolated. Subscripted variables such as C<$a[3]> or
-C<$href->{key}[0]> are also interpolated, as are array and hash slices.
-But method calls such as C<$obj->meth> are not.
+C<< $href->{key}[0] >> are also interpolated, as are array and hash slices.
+But method calls such as C<< $obj->meth >> are not.
Interpolating an array or slice interpolates the elements in order,
separated by the value of C<$">, so is equivalent to interpolating
previously succeeded, this will (silently) act instead as a genuine
empty pattern (which will always match).
+Note that it's possible to confuse Perl into thinking C<//> (the empty
+regex) is really C<//> (the defined-or operator). Perl is usually pretty
+good about this, but some pathological cases might trigger this, such as
+C<$a///> (is that C<($a) / (//)> or C<$a // />?) and C<print $fh //>
+(C<print $fh(//> or C<print($fh //>?). In all of these examples, Perl
+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//>).
+
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