Imagine you have a reference to a subroutine that may return a reference to a list, or a reference to a hash, or a list, or a hash.

If we were only getting references back, life would be good :
sub determineType { $sub_ref = shift; $rval = $sub_ref(@_); return @$rval if( ref($rval) eq 'ARRAY' ); return %$rval if( ref($rval) eq 'HASH' ); }
What I need is some analog for non-reference return values. For instace, if the subroutine returns a list I don't want to evaluate it in scalar context and end up with the length.

Can I only reflect on references and not on base types?

Is there some way I can cast the return value of the subroutine to a reference on the fly; so that I may inspect it?

UPDATE

Thanks to all those who responded. Particularly phaylon, friedo, and ikegami. As I stated below, I'm basiclly trying to pass through the return value of the subroutine reference. Here is the "final" proof of concept.
sub execSubroutine { my $sub = shift; my @sub_ret = &{$sub}(@_); # Return an list if we get a list with more than one value # this will satisfy callers expecting lists and/or hashes return @sub_ret if (@sub_ret > 1); my $rval = $sub_ret[0]; # If we have a ref de-reference and return. if (my $type = ref($rval)) { return @$rval if ($type eq 'ARRAY'); return %$rval if ($type eq 'HASH'); } # If not a ref and the caller wants a list, give it to them if (wantarray) { return @sub_ret; } # Otherwise return a scalar return $rval; }
If you are interested in seeing the code I used to test this routine, drop me a line and I'll add it.

In reply to Determining subroutine return type by satchm0h

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.