in reply to How do I check the return value of wantarray?
wantarray's kinda wonky - returning 1 if list context, "" if scalar and undefined if void -- you can normalize that to 1 for list, 0 for scalar and -1 for void. The only trick is to force the "" into an integer (IV) by doing an integer operation (+0).
my $w = defined wantarray() ? wantarray()+0 : -1; my %v = ( -1 => 'void', 0 => 'scalar', 1 => 'array' ); print $v{$w}, " context\n";
|
|---|