in reply to Re: Re: Dynamically allocating variables
in thread Dynamically allocating variables
does it push the value, or just a pointer?
When you're passing around objects, really you're passing around special references. So even if you have two scalars pointing to one struct, it's that same struct you're modifying, regardless of how it's named.
It might be easier to illustrate with an example:
To get "full" copy effects, you need to make a copy of each of the fields:use Class::Struct; use strict; struct Test => [ xyz => '$' ]; our $T = new Test(xyz => 123); print "\$T->xyz starts as: ", $T->xyz, "\n"; no strict 'refs'; my $symbolic = "T"; ${$symbolic}->xyz(456); print "\$T->xyz is now: ", $T->xyz, "\n"; my %A $A{t} = $T; $A{t}->xyz(789); print "\$T->xyz is now: ", $T->xyz, "\n"; __END__ $T->xyz starts as: 123 $T->xyz is now: 456 $T->xyz is now: 789
my $a = new Test ( xyz => 123 ); # original my $b = new Test ( xyz => $a->xyz ); # copy of a $b->xyz(456); printf "a=%d b=%d\n", $a->xyz, $b->xyz; # a=123 b=456
I don't know of an easy way to "deep copy" struct objects, though the Clone module (or Storable's 'dclone' function) may help.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: Dynamically allocating variables
by newatperl (Acolyte) on Dec 02, 2001 at 06:18 UTC | |
by tilly (Archbishop) on Dec 02, 2001 at 11:13 UTC | |
by Fastolfe (Vicar) on Dec 02, 2001 at 08:55 UTC |