in reply to Overwrite built-in wantarray()

Why are you trying to fake the context? What will that gain you? It's not like you can cheat a routine to return a different thing in an "incorrect" context. It simply won't make sense.

If you need to pass out-of-band information between cooperating routines, you can always use a localized package variable.

Replies are listed 'Best First'.
What I want to do with that
by Tobiwan (Beadle) on Aug 21, 2007 at 12:19 UTC
    I want to fake the context for tests. I want to do something like this
    FakeContext('void'); my @response = tested_sub(); is(scalar(@response), 0, 'void return'); FakeContext('scalar'); my @response = tested_sub(); is(scalar(@response), 1, 'scalar return');
    I only want to fake the context for this one sub-call. All contexts in calls in this tested_sub() should be real and unmodified. When I overwrite the caller() function and change the wantarray-flag, this doesn't have any effect for wantarray().

    And I've to decorate ist, because when I overwrite it complete, I can't fake only the first level of my sub-call.

    The only (not really god) idea I've is, to overwrite the wantarray() and use caller() to read out the context. But I think this will cause stragne side-effects.

    Any ideas? With PadWalker or Devel::Caller I can read out many things, but can I change the context? I don't know (yet).

    PS: with CORE::GLOBAL::wantarray I can overwrite the function, but I can't get any referene to the original sub.

      Ewww. Why not just:
      tested_sub(); ok(some side effect, "expected side effect"); my $scalar_response = tested_sub(); is($scalar_response, $expected, "gives proper scalar value"); my @list_response = tested_sub(); is_deeply(\@list_response,\@expected_response, "gives proper list valu +e");
      Don't be trying to freak Perl out. I'd never expect the right response from giving it the wrong context. That's just asking for hurt.