11.12. Copying Data StructuresProblemYou need to copy a complex data structure. Solution
Use the use Storable; $r2 = dclone($r1); DiscussionTwo 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
The Storable module, found on CPAN, provides a function called 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
Because %newhash = %{ dclone(\%oldhash) }; See AlsoThe documentation for the CPAN modules Storable, Data::Dumper, and FreezeThaw Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|