Right, but wouldn't a deep copy mean that the nested stuff also gets copied, so it's even slower? And the fast way would be to make an alias, i.e. copy just the reference to the whole array? Which... I can't figure out how to do. Google suggests

*cat = \@{$animals{'cat'}};

but that apparently also insists on using the global cat, so we're back to the original problem. I could do

$cat = \@{$animals{'cat'}};

but then the array would be referred to as @$cat, which would be confusing in the rest of the code. Or did I miss something?

EDIT: In the specific case at hand, it seems that a decent compromise might be a hash of references. Like so:

my @cat=(1,2,3); my @dog=(5,6); my @pig=(4,3,2,1); my %animals; $animals{'cat'}=\@cat; $animals{'dog'}=\@dog; $animals{'pig'}=\@pig; for('cat','dog','pig'){ # we want the specific order ${$animals{$_}}[1]=$_; #stuff(@{$animals{$_}}) } say @cat;

Slightly wordy, but fulfils all the criteria: No symbolic references, no globals, no copying, and @cat etc. look like they're supposed to for the remaining code. Right?

I'm still interested in learning about copying/aliasing though, even if it's not needed for this particular thing.


In reply to Re^7: Referencing the locals by Chuma
in thread Referencing the locals by Chuma

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.