The argument of interest is the $wantarray argument. This indicates what return value is
expected of the subroutine from where it was called. The subroutine could have been
called in void context meaning the return value is thrown away. Or it could have been
called and the return value assigned to a scalar. Or it could have been called and the
return value assigned to a list of scalars.
sub CheckMyWantArray
{
my @info = caller(0);
my $wantarray = $info[5];
$wantarray='undef'
unless(defined($wantarray));
print "wantarray is '$wantarray'\n";
}
CheckMyWantArray; # undef
my $scal = CheckMyWantArray; # 0
my @arr = CheckMyWantArray; # 1
> wantarray is 'undef'
> wantarray is '0'
> wantarray is '1'
No comments:
Post a Comment