Here is an easy one - pass two (or more) arrays (or hashes) to a subroutine:
use strict; my @a = (0..4); my @b = ('a'..'e'); &foo(@a,@b); &bar(\@a,\@b); sub foo { my (@a,@b) = @_; print "a: ", join(',',@a), "\n"; print "b: ", join(',',@b), "\n"; } sub bar { my ($a,$b) = @_; print "a: ", join(',',@$a), "\n"; print "b: ", join(',',@$b), "\n"; }
Subroutine foo() tries to pass two arrays, and as a result they are 'squashed' into one array. Subroutine bar() passes the two arrays by reference, and because it 'asks' for references in this line:
my ($a,$b) = @_;
the arrays are not squashed. Maybe it would be clearer if you understand that @_ is an array itself, from perldata (with a paraphrase by me):
You can actually put an array or hash anywhere in the list [subroutine arguments which are passed to @_], but the first one in the list will soak up all the values, and anything after it will become undefined.
Hence, in subroutine foo(), @a soaks up all of the elements that were meant for @b.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
F--F--F--F--F--F--F--F--
(the triplet paradiddle)

In reply to (jeffa) Re: Trying to learn about References by jeffa
in thread Trying to learn about References by dru145

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.