Perl Cookbook

Perl CookbookSearch this book
Previous: 11.11. Printing Data StructuresChapter 11
References and Records
Next: 11.13. Storing Data Structures to Disk
 

11.12. 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 FreezeThaw


Previous: 11.11. Printing Data StructuresPerl CookbookNext: 11.13. Storing Data Structures to Disk
11.11. Printing Data StructuresBook Index11.13. Storing Data Structures to Disk