suaveant has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to wrap a sub to get data on how long it ran, etc... kind of like Devel::DProf except not, since this is an FCGI which doesn't lend itself the DProf... so when I do
return &$ref;
it works fine but when I do
my @a; if(wantarray) { @a = &$ref; } else { $a[0] = &$ref; } ... do something ... return @a; #or return wantarray ? @a : $a[0];
Any idea what I am doing wrong, since this second way makes weird things happen, which tell me some data isn't getting returned right...

                - Ant
                - Some of my best work - Fish Dinner

Replies are listed 'Best First'.
Re: Returning data from wrapped sub
by dragonchild (Archbishop) on Aug 30, 2001 at 00:59 UTC
    The actual question is why
    return wantarry ? @a : $a[0];
    isn't the same as
    my @a; if(wantarray) { @a = &$ref; } else { $a[0] = &$ref; } ... do something ... return @a;
    Try doing
    return (wantarray ? @a : $a[0]);
    That should work. The issue is how return works with wantarray.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Vote paco for President!

(tye)Re: Returning data from wrapped sub
by tye (Sage) on Aug 30, 2001 at 02:48 UTC
(jeffa) Re: Returning data from wrapped sub
by jeffa (Bishop) on Aug 30, 2001 at 00:40 UTC
    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

      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