sub context {
print
defined wantarray
? wantarray ? "list" : "scalar"
: "void"
, "\n";
$_[0];
}
($a, $b) = (context('a'), context('b'));
print "a=[$a],b=[$b]\n\n";
($a, $b) = (context('a'), context('b')) || die "foo";
print "a=[$a],b=[$b]\n";
list
list
a=[a],b=[b]
void
scalar
a=[b],b=[]
####
from the perl 5.8 perlfunc scalar() section:
Because "scalar" is unary operator, if you
accidentally use for EXPR a parenthesized
list, this behaves as a scalar comma expression,
evaluating all but the last element in void context
and returning the final element evaluated in scalar
context. This is seldom what you want.
##
##
$c = scalar(context('a'), context('b'));
print "$c\n";
void
scalar
b