First you need to remember the "limits" of perl:

As found at perlsub optionally specifying the returned value, which will be evaluated in the appropriate context (list, scalar, or void) depending on the context of the subroutine call. If you specify no return value, the subroutine returns an empty list in list context, the undefined value in scalar context, or nothing in void context. If you return one or more aggregates (arrays and hashes), these will be flattened together into one large indistinguishable list.

This is not as "limiting" as one might expect.

There are several ways of passing back. You are fundamentally limited to returning values (1) as a return value or (2) via changed parameters to the sub. (Ok a third way is via a out of bounds global thingy.)

Within both of these, there are several ways to do it, and the "best" is really context dependent.

If you are looking for a return value, you could return a scalar, which is a array ref which has array ref elements to each of your lists, or you can return a list, which has array ref elements to each of your lists.

Whether you use 1 or 2 as your mechanism should really depend upon how you want the rest of your functions to appear. If you're modifying existing code, which technique is domoniate - use that.

Tis good to remember that this provides far greater functionality than the simple c-language return value :)

..Otto


In reply to Re: Returning two lists from a sub by otto
in thread Returning two lists from a sub by cormanaz

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.