The first part of your advice is rock solid: If you don't want to modify the content of the element of the array you're iterating over, knowing that the iterant is an alias to that element, don't modify it. It's kind of like when the patient goes to the doctor and says, "It hurts when I press here.", and the doctor replies, "Don't press there." This is simply what aliasing means, and covered in perlsyn.

However, you're wrong about just giving an array to Data::Dumper. Run the following snippet:

use strict; use warnings; use Data::Dumper; my @array = qw/This That Other/; print Dumper @array; print Dumper \@array;

The POD for Data::Dumper has the following two important statements:

Given a list of scalars or reference variables, writes out their contents in perl syntax.

....And later on...

Due to limitations of Perl subroutine call semantics, you cannot pass an array or hash. Prepend it with a \ to pass its reference instead. This will be remedied in time, now that Perl has subroutine prototypes. For now, you need to use the extended usage form, and prepend the name with a * to output it as a hash or array.

++ to your post though, aliases do modify the element they're aliased to, if you modify the value of the alias. In fact, if the list being iterated over is a literal list (not an array variable), you can't even modify the alias's value. So if the intent is to preserve the original content of the array, just don't go modifying the alias's value.


Dave


In reply to Re^2: Preserve array contents in for() loop by davido
in thread Preserve array contents in for() loop by dda

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.