in reply to Returning data from wrapped sub

i am a little confused as to what you are trying to do specifically, but how about this:
use strict; use Data::Dumper; my $ref = \&bar; my @arry = foo($ref); 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) }

jeffa

Replies are listed 'Best First'.
Re: (jeffa) Re: Returning data from wrapped sub
by suaveant (Parson) on Aug 30, 2001 at 00:47 UTC
    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

      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