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:
Then I want to copy it like this:my $self = { nest => [ [ [ 1, 2, 3 ], [ 4, 5, 6 ], ], ], more => [ 3, 2, 1 ] };
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.my $c1 = new CLASS; my $c2 = $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 |
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Deep Copying of Nested Data-Structures
by Anonymous Monk on Sep 01, 2003 at 10:03 UTC | |
by liz (Monsignor) on Sep 01, 2003 at 10:26 UTC | |
by stefan k (Curate) on Sep 01, 2003 at 10:12 UTC | |
Re: Deep Copying of Nested Data-Structures
by wufnik (Friar) on Sep 01, 2003 at 12:12 UTC | |
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 BazB (Priest) on Sep 01, 2003 at 10:52 UTC |
Back to
Seekers of Perl Wisdom