There are two places in Perl that may confuse people to think arrays contain variables. While @array = ($one, $two, $three); does make copies of the values of those variables for my $x ($one, $two, $three) { doesn't make copies, but rather aliases $x to the variables one at a time:

my $one = 1; my $two = 2; my $three = 3; my @arr = ($one, $two, $three); $arr[0] += 10; print "\$one = $one\n"; # still 1 for my $x ($one, $two, $three) { $x += 0.5; } print "\$one = $one\n"; # changed to 1.5 for my $x (@arr) { $x += 0.1; } print "\$one = $one\n"; # still 1.5, no link between $arr[0] and $one

So arrays contain values, but for loops through the "things" specified in the list you specify and if that "thing" is a variable, you can change it.

There is an exception though. The @_ array used for parameters to subroutine calls:

my $one = 1; my $two = 2; my $three = 3; sub direct { $_[0] += 10; $_[1] += 9; } sub indirect { my ($a,$b,$c) = @_; $a += 99; $b += 99; } print "\$one = $one\n"; # 1 direct($one, $two, $three); print "\$one = $one\n"; # changed to 11. $_[0] was an alias to $one. indirect($one, $two, $three); print "\$one = $one\n"; # still 11. The $a was assigned a copy of the +value. It's not an alias

Jenda
Enoch was right!
Enjoy the last years of Rome.


In reply to Re: Array of variables by Jenda
in thread Array of variables by rtteal

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.