Actually, wantarray has three distinct return values: true, false but defined, and undef. These correspond to list context, scalar context, and void context.
sub fred {
...
unless( defined wantarray ) { ...void context }
elsif( wantarray ) { ...list context }
else { ...scalar context }
}
You then ensure you're calling fred in the right context.
fred(); # void
my $fred = fred(); # scalar
my( $fred ) = fred(); # list (because of the parens)
my @fred = fred; # list
print fred(); # list, print is a list operator
print scalar fred(); # scalar
For instance, you might look at Hook::LexWrap (or my TPJ article on it: "Wrapping Subroutines to Trace Code Execution".
|