10.6.3. Discussion
Many built-in functions act differently when called in scalar context
than they do when called in list context. A user-defined function can
learn which context it was called in by checking
wantarray. List context is indicated by a true
return value. If wantarray returns a value that is
false but defined, then the function's return value will be used in
scalar context. If wantarray returns
undef, your function isn't being asked to provide
any value at all.
if (wantarray( )) {
print "In list context\n";
return @many_things;
} elsif (defined wantarray( )) {
print "In scalar context\n";
return $one_thing;
} else {
print "In void context\n";
return; # nothing
}
mysub( ); # void context
$a = mysub( ); # scalar context
if (mysub( )) { } # scalar context
@a = mysub( ); # list context
print mysub( ); # list context