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

Dear all,

I have the following issue. I must build a wrapper subroutine around an existing subroutine. The "inner" subroutine whose code I cannot fiddle with may return different things, i.e. sometimes it returns a hash, sometimes a scalar and sometimes a list. After doing some other tasks, the wrapper must return the return value of the inner function. How do you suggest I handle this?

sub __func__ { #handle @arglist if () { return %myhash; } else if () { return $myscalar; } else { return @mylist; } } sub func() { #some input &__func__(@arglist); #how can i handle different return values of __func__? #some other stuff }

Thanks in advance,

Athanasia

Replies are listed 'Best First'.
Re: Grab return values of different type from a sub
by BrowserUk (Patriarch) on Jul 29, 2010 at 12:34 UTC

    Subs return lists; and both hashes and arrays flatten to lists. The only problematic one is the scalar:

    #! perl -slw use strict; use Data::Dump qw[ pp ]; sub func { my $arg = shift; my %hash = 'a'..'f'; my @array = 0..9; my $scalar = 'scalar'; $arg == 1 and return %hash; $arg == 2 and return @array; $arg == 3 and return $scalar; } sub wrapper { my @rv = func( @_ ); return @rv == 1 ? $rv[ 0 ] : @rv; } my %hash = wrapper( 1 ); pp \%hash; my @array = wrapper( 2 ); pp\@array;; my $scalar = wrapper( 3 ); pp $scalar; __END__ c:\test>junk12 { a => "b", c => "d", e => "f" } [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] "scalar"

    Of course, if you want to find out the current bucket allocation of the hash, or retrieve the last item of the array; you'll need to assign it to the appropriate variable in the calling code first.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Grab return values of different type from a sub
by Sue D. Nymme (Monk) on Jul 29, 2010 at 12:49 UTC

    Try looking at the Contextual::Return module on CPAN. It allows you to do all of the things you're asking about, and more. Used wisely, it can make your code simpler and easier to maintain.

    (Used unwisely, it can make your code a maintenance nightmare of undecipherable confusion. It's a pretty powerful module.)

Re: Grab return values of different type from a sub
by Utilitarian (Vicar) on Jul 29, 2010 at 11:14 UTC
    __func__ will always return an array or scalar as currently presented.

    I would suggest you return a reference ie. return \%myhash OR return \@myarray ... in the "inner function" and use ref to determine what was returned, but if you can't mess with the code there is a problem.

    You can't tell if a scalar an array or a hash was the intended return type without repeating the conditional flow.

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."