[ACCEPTED]-What's the best way to deep copy a hash of hashes in Perl?-deep-copy

Accepted answer
Score: 37
use Storable qw(dclone);
$group2 = dclone(\%group);


Score: 30

From the Storable::dclone documentation 2 I found Clone:

my $copy = clone (\@array);

# or

my %copy = %{ clone (\%hash) };

Don't need flexibility, and claims 1 to be faster than Storable::dclone.

Score: 7

Deep data structure 101:

  • Use Storable's dclone to make a deep copy of a structure, and freeze and thaw to serialize/deserialize them for storage (say in a database, or an http cookie (but you should encrypt anything you send to the user to make it harder to tamper with).
  • Use Data::Compare (or Test::Deep or Test::Differences inside a unit test) to compare two deep data structures.
  • Use Data::Dumper or Data::Dump in debugging to see what your objects look like. But don't use that as a licence to tamper with another object's internals; use the API. :)


More Related questions