I'm guessing functions are called differently (internally) based on the expected context, which would explain why (wantarray ? @r : $r) = func() doesn't work. If so, that means the second call to the wrapped function cannot be eliminated.
The following works if the call to the wrapped function is done at the end of the wrapping function:
#!perl -l my $code = sub { wantarray ? 1 : 0 }; sub wrapper { # ... later return wantarray ? &$code : scalar &$code; } print wrapper; # 1 print scalar wrapper; # 0
The following is a more general case:
#!perl -l my $code = sub { wantarray ? 1 : 0 }; sub wrapper { my @r = wantarray ? &$code : scalar &$code; # ... later # Convert the array to a list to alter the # behaviour when assigning to a scalar. return @r[0..$#r]; } print wrapper; # 1 print scalar wrapper; # 0
So what's that whole @r[0..$#r] junk? Let me explain by example:
@r = &{ sub { return (4, 5, 6); } }; # 4, 5, 6 @r = &{ sub { my @r = (4, 5, 6); return @r; } }; # 4, 5, 6 @r = &{ sub { my @r = (4, 5, 6); return @r[0..$#r]; } }; # 4, 5, 6 $r = &{ sub { return (4, 5, 6); } }; # 6 $r = &{ sub { my @r = (4, 5, 6); return @r; } }; # 3 $r = &{ sub { my @r = (4, 5, 6); return @r[0..$#r]; } }; # 6
Update: Non-code changes to make things more readable.
Update: The following also works in the case where the call to the wrapped function is done at the end of the wrapping function. However, wrapper will not appear in the call stack (as seen by caller, cluck and confess, for example).
#!perl -l my $code = sub { wantarray ? 1 : 0 }; sub wrapper { # ... later goto &$code; } print wrapper; # 1 print scalar wrapper; # 0
In reply to Re: Maintaining context of the caller
by ikegami
in thread Maintaining context of the caller
by revdiablo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |