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.
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.
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