in reply to Maintaining context of the caller

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

Replies are listed 'Best First'.
Re^2: Maintaining context of the caller
by revdiablo (Prior) on Jul 04, 2005 at 21:31 UTC
    The following works if the call to the wrapped function is done at the end of the wrapping function

    FYI, in the case of calling the wrapped sub at the end of the wrapper, one could simply do:

    sub foo { # ... &$code; }

    As for the goto version, that's a nice trick that I occasionally use, but isn't necessary in this case.

    The following is a more general case

    That's an excellent idea, always storing the results in an array and enforcing the context on the right hand side of the assignment. This is probably the approach I'll take. (Also, your explanation of the array slice wasn't necessary for me, but it's quite good, and others may benefit from it.)

Re^2: Maintaining context of the caller
by diotalevi (Canon) on Jul 04, 2005 at 22:53 UTC
    That's not so great. If scalar context is going to be used, then you're using unnecessarilly wasteful operations. Store the result in a scalar and return the scalar. Don't store into an array, construct a list of indexes, and return a list slice.

    Then again, I'm just against wasting resources on something so trivial. I write my code to call wantarray() multiple times because I know that's cheaper (it should be, anyway) than dealing with all that listy business.

      The aesthetical "correctness" of code will vary depending on one's point of view, but I think your concerns stem more from my question than ikegami's answer. I asked for a way to avoid duplicating the context-checking logic, and he provided it. I happen to think his is a very nice solution, but of course reasonable folks can disagree.