my $thispiece = {%$piece}; # create a deep copy
Be aware that {%$piece} creates a shallow copy. It may
appear to be deep, but only because $piece is shallow to
begin with, i.e., it has only one level. Try your code with a nested
structure for $piece:
Of course, if you're only dealing with a shallow structure in $piece to begin with, your code will be fine.Win8 Strawberry 5.8.9.5 (32) Fri 05/07/2021 7:11:33 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings my $Action; my $piece = { this => { that => { foo => { bar => 123 } } } }; foreach my $other (111 .. 113){ my $thispiece = {%$piece}; # create a SHALLOW copy $thispiece->{theother} = $other; push(@{$Action}, $thispiece); } use Data::Dumper; print Dumper($Action); ^Z $VAR1 = [ { 'theother' => 111, 'this' => { 'that' => { 'foo' => { 'bar' => 123 } } } }, { 'theother' => 112, 'this' => $VAR1->[0]{'this'} }, { 'theother' => 113, 'this' => $VAR1->[0]{'this'} } ];
Give a man a fish: <%-{-{-{-<
In reply to Re^3: Unwanted cloning of array elements
by AnomalousMonk
in thread Unwanted cloning of array elements
by oakbox
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |