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?

Bill

In reply to marshalling data by BillKSmith

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.