develooper Front page | perl.perl5.porters | Postings from May 2010

[perl #7233] Data::Dumper Purity mode t/dumper.t fails if slightly rearranged

Thread Previous
From:
Alexandr Ciornii via RT
Date:
May 1, 2010 03:33
Subject:
[perl #7233] Data::Dumper Purity mode t/dumper.t fails if slightly rearranged
Message ID:
rt-3.6.HEAD-27388-1272710020-950.7233-14-0@perl.org
Similar results on 5.12.0.

On Wed Jul 04 06:11:31 2001, merlyn@stonehenge.com wrote:
>     use Data::Dumper;
>     my @dogs = ( 'Fido', 'Wags' );
>     my %kennel = (
>       First => \$dogs[0],
>       Second =>  \$dogs[1],
>     );
>     $dogs[2] = \%kennel;
>     my $mutts = \%kennel;
> 
>     print "CORRECT:\n";
>     print Data::Dumper->new(
>       [\@dogs, \%kennel, $mutts],
>       [qw($dogs $kennel $mutts)])->Purity(1)->Dump;
> 
>     print "WRONG:\n";
>     print Data::Dumper->new(
>       [\%kennel, \@dogs, $mutts],
>       [qw($kennel $dogs $mutts)])->Purity(1)->Dump;
> 
> Output:
> 
>     CORRECT:
>     $dogs = [
>               'Fido',
>               'Wags',
>               {
>                 'First' => '',
>                 'Second' => ''
>               }
>             ];
>     $dogs->[2]{'First'} = \$dogs->[0];
>     $dogs->[2]{'Second'} = \$dogs->[1];
>     $kennel = $dogs->[2];
>     $mutts = $dogs->[2];
>     WRONG:
>     $kennel = {
>                 'First' => \'Fido',
>                 'Second' => \'Wags'
>               };
>     $dogs = [
>               ${$kennel->{'First'}},
>               ${$kennel->{'Second'}},
>               {}
>             ];
>     $dogs->[2] = $kennel;
>     $mutts = $kennel;
> 
> The WRONG is wrong because the $dogs->[0] is set to a separate copy of
> the 'Fido' string, and no longer is associated with $kennel->{First}.
> Ditto for 'Wags'.
> 
> I'm not sure how to fix this... you really need to dump $dogs before
> $kennel here so you can take a reference to the array element in
> place.  Or it has to be patched up afterward.  I'm turning Purity on,
> which is usually pretty good about constructing the patchups.  But it
> wasn't sufficient here.

-- 
Alexandr Ciornii, http://chorny.net


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About