BillKSmith has asked for the wisdom of the Perl Monks concerning the following question:
I am reading the book "Intermediate Perl" and I am having trouble with the section call marshalling Data (page 78). The idea is to save a data structure with Data::Dumper->Dump and later recover that data structure in another program by executing the saved string with eval.
The example in the book creates two hashes that reference each other. It then dumps both of them to a string. My code below produces the exact string that the book says to expect. For the sake of demonstrating my problem, I create a second package (Other) and attemp to reconstruct the hashes from the string in that package. The resulting hashes contain the right data, but they do not reference each other. I demonstrate this by showing that two references which should be equal are equal in the main package, but not in the Other package.
use warnings; use Data::Dumper; package main{ our @data1 = qw(one won); our @data2 = qw(two too to); push @data2, \@data1; push @data1, \@data2; open my $FH, '>', \$marshall_data; print {$FH} Data::Dumper->Dump( [ \@data1, \@data2 ], [qw(*data1 *data2)] ); close $FH; print "NO " if \@data2 != $data1[2]; print "CIRCULAR DATA STRUCTURE in package main\n"; } package Other{ open my $FH, '<', \$main::marshall_data; my $string = do {local $/ = undef; <$FH>}; close $FH; eval $string; die "$@\n" if $@; print "NO " if \@data2 != $data1[2]; print "CIRCULAR DATA STRUCTURE in package other\n"; print \@data2, ' ', $data1[2], "\n\n"; }
What Am I missing? Could there be an error in the book? or perl?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: marshalling data
by tobyink (Canon) on Mar 12, 2013 at 07:14 UTC | |
by BillKSmith (Monsignor) on Mar 12, 2013 at 12:30 UTC | |
by tobyink (Canon) on Mar 12, 2013 at 14:30 UTC | |
by BillKSmith (Monsignor) on Mar 12, 2013 at 20:22 UTC | |
Re: marshalling data
by thomas895 (Deacon) on Mar 12, 2013 at 03:36 UTC | |
by tye (Sage) on Mar 12, 2013 at 03:58 UTC | |
by thomas895 (Deacon) on Mar 13, 2013 at 07:30 UTC | |
by Anonymous Monk on Mar 12, 2013 at 03:55 UTC | |
by BillKSmith (Monsignor) on Mar 12, 2013 at 03:58 UTC | |
by tobyink (Canon) on Mar 12, 2013 at 06:59 UTC | |
Re: marshalling data
by Anonymous Monk on Mar 12, 2013 at 03:53 UTC |