Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Deep Copying of Nested Data-Structures

by stefan k (Curate)
on Sep 01, 2003 at 09:46 UTC ( #288102=perlquestion: print w/ replies, xml ) Need Help??
stefan k has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, having coded a lot in C++ in the last few months I'm now a bit paranoid and probably to C++-ish in my thinking. Therefore I'd be more than happy to be enlightened on the following problem by a real Perl guru :-)

I've created a class (let's call it CLASS) that has a nested data structure like this:

my $self = { nest => [ [ [ 1, 2, 3 ], [ 4, 5, 6 ], ], ], more => [ 3, 2, 1 ] };
Then I want to copy it like this:
my $c1 = new CLASS; my $c2 = $c1;
And next to that I need to change a value in the arrays of $c2. Since I have the instance variables as lots of references changing a value in $c2 always changes the corresponding value in $c1.

I had hoped that Perl would clean that mess for me but I doesn't seem so.

Now, do I need to write some sort of copy constructur (*yuck* even the word makes me shudder) and if so how do I do this or is there any other way of nicely solving this mess in a Perl-ish manner?

Thanks in advance for any pointers what to read or hints or code or other forms of enlightenment.

Regards... Stefan
you begin bashing the string with a +42 regexp of confusion

Comment on Deep Copying of Nested Data-Structures
Select or Download Code
Re: Deep Copying of Nested Data-Structures
by liz (Monsignor) on Sep 01, 2003 at 09:56 UTC
Re: Deep Copying of Nested Data-Structures
by Anonymous Monk on Sep 01, 2003 at 10:03 UTC
    perldoc -q copy Found in C:\Perl\lib\pod\perlfaq4.pod How do I print out or copy a recursive data structure? The Data::Dumper module on CPAN (or the 5.005 release of P +erl) is great for printing out data structures. The Storable mo +dule, found on CPAN, provides a function called "dclone" that recursively copies its argument. use Storable qw(dclone); $r2 = dclone($r1); Where $r1 can be a reference to any kind of data structure + you'd like. It will be deeply copied. Because "dclone" takes and returns references, you'd have to add extra punctuation if + you had a hash of arrays that you wanted to copy. %newhash = %{ dclone(\%oldhash) };
      I knew there had to be a simple solution. Thanks for pointing that out to me!

      Regards... Stefan
      you begin bashing the string with a +42 regexp of confusion

      ...The Storable module, found on CPAN...

      It should be noted that Storable became part of Perl core in 5.8.0. So you don't need to go to CPAN if you already have 5.8.0.

      I will create a doc patch for 5.8.1 that will note this properly.


Re: Deep Copying of Nested Data-Structures
by BazB (Priest) on Sep 01, 2003 at 10:52 UTC
    use Clone;

    If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
    That way everyone learns.

Re: Deep Copying of Nested Data-Structures
by wufnik (Friar) on Sep 01, 2003 at 12:12 UTC
    personally, i like the following, nicked shamelessly from the font of merlyn. nice esp. if you do not want the overhead of another module.

    sub deepcopy{ my $this = shift; if (not ref $this){ $this; } elsif (ref $this eq "ARRAY") { [map deepcopy($_), @$this]; } elsif (ref $this eq "HASH"){ scalar { map { $_ => deepcopy($this->{$_}) } keys %$this }; } else { die "what type is $_?"} }
    hope that helps,


    -- in the world of the mules there are no rules --

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://288102]
Approved by valdez
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2014-09-21 11:50 GMT
Find Nodes?
    Voting Booth?

    How do you remember the number of days in each month?

    Results (168 votes), past polls