Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^6: Referencing the locals

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


in reply to Re^5: Referencing the locals
in thread Referencing the locals

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?

Both!

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.

Replies are listed 'Best First'.
Re^7: Referencing the locals
by Chuma (Scribe) on Apr 29, 2021 at 15:28 UTC

    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.

      Right, but wouldn't a deep copy mean that the nested stuff also gets copied, so it's even slower?

      Yes, a deep copy would be even slower. When I said "The shallow copy would be a problem if your arrays are large", the emphasis was supposed to be on copy - in other words, it would be wasteful to make a copy of a large array, no matter whether it's a shallow or deep copy, but if the array is always small, making a copy may be acceptable (though as I said, I still don't recommend it).

      And the fast way would be to make an alias, i.e. copy just the reference to the whole array? ... I'm still interested in learning about copying/aliasing though, even if it's not needed for this particular thing.

      Note that "aliases" in Perl are distinct from references. There are ways to do aliasing, e.g. Data::Alias:

      use warnings; use strict; use Data::Alias; use Data::Dump; my %animals = ( cat => [1,2,3], dog => [5,6], pig => [4,3,2,1], ); alias my @cat = @{ $animals{cat} }; $cat[1] = 'cat'; push @cat, 4; dd \%animals; __END__ { cat => [1, "cat", 3, 4], dog => [5, 6], pig => [4, 3, 2, 1] }

      But note its documentation says "The core's aliasing facilities are implemented more robustly than this module and are better supported. If you can rely on having a sufficiently recent Perl version, you should prefer to use the core facility rather than use this module. If you are already using this module and are now using a sufficiently recent Perl, you should attempt to migrate to the core facility." which refers to the currently still experimental refaliasing I mentioned. That's why I suggested references, since those are fully supported everywhere and let you build complex data structures.

      but then the array would be referred to as @$cat, which would be confusing in the rest of the code.

      Yes, the reference would have to be dereferenced. Since nested data structures are pretty common, I'd suggest practicing using references so it's less confusing.

      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;
      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?

      Note that this code does pretty much the same as the code in my first reply, with the exception that this also creates the three arrays as lexicals. But otherwise yes, this is one way to do it.

      \@{$animals{'cat'}} ... ${$animals{$_}}[1]

      Note that these two are written more simply as $animals{cat} and $animals{$_}[1], respectively. I would strongly recommend a read of perlreftut, perlref, and perldsc.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2021-09-24 09:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?