Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

evaling dumped cyclic structures

by marvell (Pilgrim)
on Nov 27, 2002 at 16:09 UTC ( [id://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.

    HTH

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://216099]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 16:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found