X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlop.pod;h=8af2d5b93be09763266db9bcd3e5ce7ddec6a7a2;hb=c963b151157dab5813a32fc6004b63916ad155e8;hp=6785d2452e48620b44873c9f01ed5124959569bd;hpb=d73f34fb2db5664941bbe71b34c335b0ae3b50b3;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlop.pod b/pod/perlop.pod index 6785d24..8af2d5b 100644 --- a/pod/perlop.pod +++ b/pod/perlop.pod @@ -26,7 +26,7 @@ values only, not array values. left & left | ^ left && - left || + left || // nonassoc .. ... right ?: right = += -= *= etc. @@ -34,7 +34,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. @@ -361,12 +361,23 @@ 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 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 (except that it returns the value of C<$a> +rather than the value of C) and is exactly equivalent to +C. 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. + +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: @@ -375,10 +386,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 C 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, C and C 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" @@ -600,7 +611,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 Binary "or" returns the logical disjunction of the two surrounding expressions. It's equivalent to || except for the very low precedence. @@ -623,7 +634,12 @@ takes higher 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 on an Bor, +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. @@ -847,6 +863,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 +(C or C?). 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 (so C becomes C). + If the C option is not used, C 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