You can easily return multiple values from a subroutine - it must be some weird other language you're thinking about, like C or Pascal, that can only return one value and not a list from a subroutine.

You can also pass in lists to a subroutine, but Perl automatically flattens all lists into one big list, and you lose the information which element came from one list:

use strict; sub foo { my (@args) = @_; print "$_\n" for @args; }; my @a = qw(a1 a2 a3); my @b = qw(b1 b2 b3 b4 b5); foo(1,2,3,4,5,@a,@b);

If you want to keep the distinction between the arrays, it is customary to pass the arrays via reference :

use strict; sub foo { my ($val1,$val2,$arr_ref1,$arr_ref2) = @_; print "$_\n" for ($val1,$val2,$arr_ref1,$arr_ref2); }; my @a = qw(a1 a2 a3); my @b = qw(b1 b2 b3 b4 b5); foo(1,2,\@a,\@b);

To get at the values in @a and @b, you'll need to dereference $arr_ref1 and $arr_ref2:

use strict; sub foo { my ($val1,$val2,$arr_ref1,$arr_ref2) = @_; print "$_\n" for ($val1,$val2); for my $array ($arr_ref1,$arr_ref2) { for my $element (@$array) { print "array: $element\n"; }; }; }; my @a = qw(a1 a2 a3); my @b = qw(b1 b2 b3 b4 b5); foo(1,2,\@a,\@b);

See also tyes References quick reference for a much more and better explanation.


In reply to Re: I've read perlref by Corion
in thread I've read perlref by Anonymous Monk

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.