in reply to Re: how do I copy an array of objects?
in thread how do I copy an array of objects?
I should have read the question better before I answered :-)
The code I gave will only dereference down 1 layer.
The code below will explain this better, I hope.
Anyway, I think what you really want is
Clone.
I just downloaded it to test this code, and already I see it as an invaluable tool in my Perl toolbox.
Here is an example of how my earlier code fails, and how Clone can help you.
Good luck
thinker
The output of which is#!/usr/bin/perl -w use strict; use Clone qw(clone); my $original =[1,2,[3,4,5]]; my $shallow_original=$original; my $deep_original=[@$original]; #deep, but not deep enough!! my $clone_original=clone($original); # this behaves fine. $original->[1]=42; $original->[2][1]=77; print "original: @$original\n"; print "shallow: @$shallow_original\n"; print "deep: @$deep_original\n"; print "shallow[2][1]: $shallow_original->[2][1]\n"; print "deep[2][1]: $deep_original->[2][1]\n"; print "clone[2][1]: $clone_original->[2][1]\n";
original: 1 42 ARRAY(0x80cf9a4) shallow: 1 42 ARRAY(0x80cf9a4) deep: 1 2 ARRAY(0x80cf9a4) shallow[2][1]: 77 deep[2][1]: 77 clone[2][1]: 4
|
|---|