in reply to Re^3: Referencing the locals
in thread Referencing the locals
Huh, I didn't know that, about the shallow copy. But in this case, since I'm just trying to "rename" the array, shallow copy seems like the right thing.
The shallow copy would be a problem if your arrays are large, or if you expect changes to the array to be reflected in the original data structure. (Because it's only a shallow copy, changes to nested arrays or hashes would be reflected in the original data.)
There are of course situations where you can't avoid repetition, but I always figured this was a neat shortcut in Perl (I can't think of any other language that would allow it). ... I'm not aware of any other way that's as succinct and simple as looping over the strings.
If by this you mean using symbolic references, then as I said, the IMHO better appraoch is hashes. They also have the advantage that instead of for('cat','dog','pig') or for(\@cat,\@dog,\@pig) you can just write for (keys %animals), allowing your code to be more generic.
There are a lot of people who argue that you should always use "strict", basically changing the language into a stricter version of itself. ... Different preferences, I guess.
If you look, you'll find lots of information on why strict is a best practice. I recently wrote a bit about it here, and I encourage you to read it since I think it applies here as well.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Referencing the locals
by Chuma (Scribe) on Apr 29, 2021 at 14:27 UTC | |
by haukex (Archbishop) on Apr 29, 2021 at 14:37 UTC | |
by Chuma (Scribe) on Apr 29, 2021 at 15:28 UTC | |
by haukex (Archbishop) on Apr 29, 2021 at 17:55 UTC |