However, if you create a variable and assign it an aggregate value, then you create a second variable and you assign the first variable to it, then all changes to the second variable are also reflected in the first variable. In other words, assigning an aggregate from one variable to another assigns the pointer.
a = 3; b = a; ++b; assert a == 3; assert b == 4; s1 = "aaaa"; s2 = s1; s2 = 'bbbb'; assert s1 == 'aaaa'; m = { x=1 }; n = m; assert n.x == 1; n.x = 2; assert m.x == 2; assert n.x == 2; a1 = [1,2,3]; a2 = a1; a2[1] = 35; assert a1[1] == 35;
I'm just wondering if this is correct. This behavior is similar to Java and probably some other languages, but do I want this ?
Perl has a lexical solution: you copy either values or pointers to objects:
my %a = (k1=>3); my %b = %a; #clone $b{k1} = 7; print "$a{k1}\n"; #prints 3 my $c = \%a; #copy $c->{k1} = 35; print "$a{k1}\n"; #prints 35