Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^4: Referencing the locals

by haukex (Bishop)
on Apr 29, 2021 at 12:35 UTC ( #11131866=note: print w/replies, xml ) Need Help??

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

    I don't understand that first part. Wouldn't a shallow copy by faster for large arrays, since the data isn't actually being copied?

    And "changes to the array"... oh, right, as opposed to changes to the elements of the array? I think I get it. That should be fine in this case, since I never actually wanted the original array and won't use it again. Good to know in other situations, though.

      Wouldn't a shallow copy by faster for large arrays, since the data isn't actually being copied?

      "Shallow" in this case refers to the fact that the elements of the array would be copied, hence it'd be slow and burn memory for large arrays, but if any of those elements are references to other data structures, then copying of those references means that the copies of the references in the new array still refer to the same data structures.

      use warnings; use strict; use Data::Dump; my %animals = ( cat => [ 1,2,3, ["nested","array"] ] ); my @cat = @{$animals{'cat'}}; push @cat, 4; $cat[1] = 9; $cat[3][0] = 'Nested'; dd \%animals; dd \@cat; __END__ { cat => [1, 2, 3, ["Nested", "array"]] } [1, 9, 3, ["Nested", "array"], 4]

      Note how the 9 and 4 only affected @cat, while the modification of "Nested" is visible via both %animals and @cat. ["nested","array"] is a single anonymous array stored somewhere in memory, if you copy a reference to that array, the copy of that reference still points to the same anonymous array. A deep copy can be achieved with, for example, dclone from Storable (often used because it's a core module), or e.g. Clone from CPAN.

      And "changes to the array"... oh, right, as opposed to changes to the elements of the array?


      Update: I should add that the distinction between shallow and deep copies usually bites people in that they write my %copy = %data; and want a deep copy instead of a shallow one. But if you say that your copies are meant to be read-only (and the arrays are small), then indeed a shallow copy doesn't really hurt - though I still would recommend using references instead of shallow copies.

        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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11131866]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (1)
As of 2021-07-31 11:41 GMT
Find Nodes?
    Voting Booth?

    No recent polls found