summaryrefslogtreecommitdiff
path: root/pod/perlop.pod
diff options
context:
space:
mode:
Diffstat (limited to 'pod/perlop.pod')
-rw-r--r--pod/perlop.pod48
1 files changed, 37 insertions, 11 deletions
diff --git a/pod/perlop.pod b/pod/perlop.pod
index ee4e22fa7e..3fda038fe1 100644
--- a/pod/perlop.pod
+++ b/pod/perlop.pod
@@ -45,7 +45,7 @@ values only, not array values.
left &
left | ^
left &&
- left ||
+ left || //
nonassoc .. ...
right ?:
right = += -= *= etc.
@@ -53,7 +53,7 @@ values only, not array values.
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.
@@ -484,6 +484,18 @@ if the left operand is false, the right operand is not even evaluated.
Scalar or list context propagates down to the right operand if it
is evaluated.
+=head2 C-style Logical Defined-Or
+X<//> X<operator, 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)>.
+
=head2 C-style Logical Or
X<||> X<operator, logical, or>
@@ -492,12 +504,12 @@ if the left operand is true, the right operand is not even evaluated.
Scalar or list context propagates down to the right operand if it
is evaluated.
-The C<||> and C<&&> operators return the last value evaluated
+The C<||>, C<//> and C<&&> operators return the last value evaluated
(unlike C's C<||> and C<&&>, which return 0 or 1). Thus, a reasonably
portable 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:
@@ -506,10 +518,10 @@ 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"
@@ -729,7 +741,7 @@ The following are recognized:
**= += *= &= <<= &&=
-= /= |= >>= ||=
- .= %= ^=
+ .= %= ^= //=
x=
Although these are grouped by family, they all have the precedence
@@ -821,7 +833,7 @@ expressions. It's equivalent to && except for the very low
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
X<operator, logical, or> X<operator, logical, xor> X<operator, logical, err>
X<operator, logical, defined or> X<operator, logical, exclusive or>
X<or> X<xor> X<err>
@@ -849,6 +861,11 @@ takes higher precedence.
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.
@@ -1142,6 +1159,15 @@ the other flags are taken from the original pattern. If no match has
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//>).
+
=item Matching in list context
If the C</g> option is not used, C<m//> in list context returns a