Subject: | Loaded self-referential structures have only one reference per referent |
The problem is best explained with an example. This works as you would expect:
$ perl -e 'use YAML::XS; my $a = [["a"], ["b"]]; push @$a, $a->[0]; $a->[0] = "x"; print Dump($a), "\n";'
---
- x
- - b
- - a
But if you dump and reload $a between the push and the second assignment, you get this:
$ perl -e 'use YAML::XS; my $a = [["a"], ["b"]]; push @$a, $a->[0]; $a = Load Dump $a; $a->[0] = "x"; print Dump($a), "\n";'
---
- x
- - b
- x
Assigning to $a->[0] mysteriously changed $a->[2] as well. Sorta-kinda-workaround: YAML.pm doesn't have this bug. Also, recreating $a after loading it prevents the weirdness:
$ perl -e 'use YAML::XS; my $a = [["a"], ["b"]]; push @$a, $a->[0]; $a = Load Dump $a; $a = [@$a]; $a->[0] = "x"; print Dump($a), "\n";'
---
- x
- - b
- - a
(This is perl v5.10.0 built for i486-linux-gnu-thread-multi. Output of uname -a: Linux Thoth 2.6.31-16-generic #53-Ubuntu SMP Tue Dec 8 04:01:29 UTC 2009 i686 GNU/Linux.)
$ perl -e 'use YAML::XS; my $a = [["a"], ["b"]]; push @$a, $a->[0]; $a->[0] = "x"; print Dump($a), "\n";'
---
- x
- - b
- - a
But if you dump and reload $a between the push and the second assignment, you get this:
$ perl -e 'use YAML::XS; my $a = [["a"], ["b"]]; push @$a, $a->[0]; $a = Load Dump $a; $a->[0] = "x"; print Dump($a), "\n";'
---
- x
- - b
- x
Assigning to $a->[0] mysteriously changed $a->[2] as well. Sorta-kinda-workaround: YAML.pm doesn't have this bug. Also, recreating $a after loading it prevents the weirdness:
$ perl -e 'use YAML::XS; my $a = [["a"], ["b"]]; push @$a, $a->[0]; $a = Load Dump $a; $a = [@$a]; $a->[0] = "x"; print Dump($a), "\n";'
---
- x
- - b
- - a
(This is perl v5.10.0 built for i486-linux-gnu-thread-multi. Output of uname -a: Linux Thoth 2.6.31-16-generic #53-Ubuntu SMP Tue Dec 8 04:01:29 UTC 2009 i686 GNU/Linux.)