- 论坛徽章:
- 145
|
回复 4# Eva326
There you are ...
operator and search //
$ perldoc perlop
NAME
perlop - Perl operators and precedence
DESCRIPTIONESC
Operator Precedence and Associativity
Operator precedence and associativity work in Perl more or less like they
do in mathematics.
...
C-style Logical Defined-Or
Although it has no direct equivalent in C, Perl's "//" operator is related
to its C-style or. In fact, it's exactly the same as "||", except that it
tests the left hand side's definedness instead of its truth. Thus, "$a //
$b" is similar to "defined($a) || $b" (except that it returns the value of
$a rather than the value of "defined($a)") and yields the same result as
"defined($a) ? $a : $b" (except that the ternary-operator form can be used
as a lvalue, while "$a // $b" cannot). This is very useful for providing
default values for variables. If you actually want to test if at least one
of $a and $b is defined, use "defined($a // $b)".
The "||", "//" and "&&" operators return the last value evaluated (unlike
C's "||" and "&&", 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";
In particular, this means that you shouldn't use this for selecting
between two aggregates for assignment:
@a = @b || @c; # this is wrong
@a = scalar(@b) || @c; # really meant this
@a = @b ? @b : @c; # this works fine, though
...
|
|