in reply to Determining subroutine return type

The caller can't tell the difference between a list of one item and a scalar, so you won't get perfect results. Maybe the following will do what you want, though:

sub determineType { my $sub_ref = shift; my @rval = $sub_ref(@_); return $rval[0] if @rval <= 1; return @rval if @rval >= 2; my $rval = $rval[0]; return @$rval if (ref($rval) eq 'ARRAY'); return %$rval if (ref($rval) eq 'HASH'); }

By the way, you said "reference to a list", but there is no such thing. You meant "reference to an array".

Replies are listed 'Best First'.
Re^2: Determining subroutine return type
by satchm0h (Beadle) on Feb 24, 2005 at 22:43 UTC
    Thanks for the response, and yes I meant "reference to an array" ;)

    It seems to me that I left out a particularly important piece of information. All I'm really trying to do is pass through my routine to another caller and verify that I am providing the results that the caller is expecting. As both you and phaylon point out, calling the provided subroutine ref in list context is the solution. The missing piece of the puzzle is how to handle scalars.

    friedo provided the answer. If I wind up with a list of length one, I can return a scalar if my wrapper subroutine is called in scalar context as determined by wantarray.

    Thanks to all!