O'Reilly logo

Perl Cookbook by Nathan Torkington, Tom Christiansen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Copying Data Structures

Problem

You need to copy a complex data structure.

Solution

Use the dclone function from the Storable module from CPAN:

use Storable;

$r2 = dclone($r1);

Discussion

Two types of “copy” are sometimes confused. A surface copy (also known as shallow copy) simply copies references without creating copies of the data behind them:

@original = ( \@a, \@b, \@c );
@surface = @original;

A deep copy creates an entirely new structure with no overlapping references. This copies references to 1 layer deep:

@deep = map { [ @$_ ] } @original;

If @a, @b, and @c themselves contain references, the preceding map is no longer adequate. Writing your own code to deep-copy structures is laborious and rapidly becomes tiresome.

The Storable module, found on CPAN, provides a function called dclone that recursively copies its argument:

use Storable qw(dclone); 
$r2 = dclone($r1);

This only works on references or blessed objects of type SCALAR, ARRAY, or HASH; references of type CODE, GLOB, and IO and more esoteric types are not supported. The safeFreeze function from the FreezeThaw module supports these when used in the same address space by using a reference cache that could interfere with garbage collection and object destructors under some circumstances.

Because dclone takes and returns references, you must add extra punctuation if you have a hash of arrays that you want to copy:

%newhash = %{ dclone(\%oldhash) };
               
               

See Also

The documentation for the CPAN modules Storable, Data::Dumper, and ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required