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.
RIP an inspiration; A true Folk's Guy

In reply to Re: Grab return values of different type from a sub by BrowserUk
in thread Grab return values of different type from a sub by athanasia

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.