in reply to (jeffa) Re: Returning data from wrapped sub
in thread Returning data from wrapped sub

ahh.... but that calls &$ref in list context no matter what yes? Which could screw things up... say for subs that return an array ref is called scalar and a list if called in list context...

                - Ant
                - Some of my best work - Fish Dinner

  • Comment on Re: (jeffa) Re: Returning data from wrapped sub

Replies are listed 'Best First'.
(jeffa) 3Re: Returning data from wrapped sub
by jeffa (Bishop) on Aug 30, 2001 at 00:53 UTC
    Nope - try this:
    use strict; use Data::Dumper; my $ref = \&bar; my @arry = foo($ref); $ref = \&baz; my $scal = foo($ref); print Dumper @arry; print Dumper $scal; sub foo { my $ref = shift; my @a = &$ref; return wantarray ? @a : $a[0]; } sub bar { (1..4) } sub baz { [(1..4)] }

    jeffa

      That does not answer his question. If the return value of a function call is being stored in a list, the function is called in "array" context.
      @x = func() ; # list context ($x) = func() ; # list context $x = (func())[1]; # list context
      Read my article on lists.

      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      Try it with:
      $ref = sub {localtime(@_)};
      You will very quickly find that your wrapper has changed rather dramatically what happens in scalar context.
      Yep... this does work... I guess I just didn't fully understand how context stuff works...

                      - Ant
                      - Some of my best work - Fish Dinner