Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

evaling dumped cyclic structures

by marvell (Pilgrim)
on Nov 27, 2002 at 16:09 UTC ( #216099=perlquestion: print w/replies, xml ) Need Help??
marvell has asked for the wisdom of the Perl Monks concerning the following question:

The following code does not work.
#!/usr/bin/perl use Data::Dumper; $old = { name => "bob" }; $young = { name => "bob jr" }; $old->{child} = $young; $young->{parent} = $old; $string = Data::Dumper->Dump([$old],[qw(old)]); undef $old; undef $young; eval $string; $check = Data::Dumper->Dump([$old],[qw(old)]); if ($string ne $check) { print "eek!\n---\n$string---\n$check"; }

This, I imagine, is due to the cyclic nature of the structure.

Any ideas?

¤ Steve Marvell

Replies are listed 'Best First'.
Re: evaling dumped cyclic structures
by valdez (Monsignor) on Nov 27, 2002 at 16:47 UTC

    Set $Data::Dumper::Purity to 1:

    #!/usr/bin/perl use Data::Dumper; $Data::Dumper::Purity = 1; $old = { name => "bob" }; $young = { name => "bob jr" }; $old->{child} = $young; $young->{parent} = $old; $string = Data::Dumper->Dump([$old],[qw(old)]); undef $old; undef $young; eval "$string"; $check = Data::Dumper->Dump([$old],[qw(old)]); if ($string ne $check) { print "eek!\n"; } print "\n---\n$string---\n$check\n";

    Data::Dumper's man page says:

    The default output of self-referential structures can be `eval'ed, but the nested references to `$VAR'n will be undefined, since a recursive structure cannot be constructed using one Perl statement. You should set the `Purity' flag to 1 to get additional statements that will correctly fill in these references.

    Ciao, Valerio

Re: evaling dumped cyclic structures
by Aristotle (Chancellor) on Nov 27, 2002 at 16:36 UTC
    No, it is because in effect, you execute the following:
    undef $old; $old = { 'child' => { 'parent' => $old, # !!! 'name' => 'bob jr' }, 'name' => 'bob' };

    Obviously, the parent key is assigned the new value of $old, which is, well, undef.

    Update: You want to check the Data::Dumper POD for the $Data::Dumper::Purity setting.

    #!/usr/bin/perl -w use strict; use Data::Dumper; my $old = { name => "bob", child => { name => "bob jr", } }; $old->{child}{parent} = $old; $Data::Dumper::Purity = 1; print Data::Dumper->Dump([$old], ['$old']); __END__ $old = { 'child' => { 'parent' => {}, 'name' => 'bob jr' }, 'name' => 'bob' }; $old->{'child'}{'parent'} = $old;
    Update2: it appears valdez snuck the same point in as I was looking up the POD myself.

    Makeshifts last the longest.

Re: evaling dumped cyclic structures
by demerphq (Chancellor) on Nov 27, 2002 at 17:12 UTC
    While the other respondants are correct that the using
    $check = Data::Dumper->new([$old],[qw(old)])->Purity(1)->Dump
    Will at least give evalable results you should be cautious with self referential structures and D::D. Some structures are fundamentally undumpable with D::D (and most dumpers actually, D::BFDump tries to solve this, but i havent had time to fix some bugs so dont go there right now.)

    But more pertinent (as i think its unlikely that you will be dumping such structures) is that as D::D does not sort the keys of a hash you can not rely on it dumping two (for all intents and purposes) identical hashes in exactly the same way. The keys may be output in any order and thus doing a string compare on the results is not a sufficient test to determine if the two structures are the same.


    --- demerphq
    my friends call me, usually because I'm late....

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://216099]
Approved by broquaint
[1nickt]: If 1nickt is the some guy, that's not what he said.
[GotToBTru]: thepkd might not know about last hour of cb
[1nickt]: my $x = $aohoaoh->[0]->{' foo'}->[0]->{'bar' }; should work
[thepkd]: its not $aohoaoh->[0]. its $aohoaoh[0]->{'foo '}. Tried it. Its working. Thnaks
[thepkd]: Hey 1nickt sorry man. What
[GotToBTru]: a ref too far ;)
[thepkd]: Yes this is my first time in cb. Trying to work my way around.
[JohnCub]: "A ref too far" would make a good title for anyone's perl memoirs
[thepkd]: Anyways it works with $aohoaoh[0]->{'foo '}[0]->{'bar'}

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2016-12-06 13:41 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (105 votes). Check out past polls.