BillKSmith has asked for the wisdom of the Perl Monks concerning the following question:
I know that I asked much the same question marshalling data about three years ago. Your answers convinced me that I was not yet ready for this topic. I recently came across my old work and decided to try again.
The book “Intermediate Perl” ISBN 9781449393090 has a section on marshalling data. In that section, it outlines how to create a pair of arrays each of which contains reference to the other, save these arrays to a file with Data::Dumper, and later, restore them by running eval on the contents of the file. The following code is my attempt to implement and test that example. In order to minimize the number of files in my post, I save the data to a memory file and restore to a separate package in the same script. I have two tests. Each tests that the address portion of one of the references is indeed the address of the other array. The pair of tests is run on the original arrays to validate the tests. The same pair of test is then run on the restored arrays. One of theses tests fails. This convinces me that the arrays are not restored correctly. My question is “why?”. I am not yet interested in any other way to save those arrays. (That is covered later in the same chapter.)
use strict; use warnings; package main{ use Data::Dumper; our $marshall_data = \do{my $file_simulator}; my @data1 = qw(one won); my @data2 = qw(two too to); push @data2, \@data1; # Add recursive elemen +ts push @data1, \@data2; open my $FH, '>', $marshall_data; print {$FH} Data::Dumper->Dump( # Marshall the data [ \@data1, \@data2 ], [qw(*data1 *data2)] ); close $FH; Testing::recursive_data(\@data1, \@data2); # Both tests pass } package Other{ my @data1; my @data2; open my $FH, '<', $main::marshall_data; my $string = do {local $/ = undef; <$FH>}; # Slurp the string close $FH; # print $string, "\n"; eval $string; # Restore content of a +rrays die "Marshalling Failed: $@\n" if $@; Testing::recursive_data(\@data1, \@data2); # Second test fails } package Testing{ use Scalar::Util qw( refaddr ); use Test::Simple tests => 4; sub recursive_data{ my ($data1_ref, $data2_ref) = @_; ok( refaddr($data1_ref) == refaddr($data2_ref->[3]), '$data2[3] refers to @data1' ); ok( refaddr($data2_ref) == refaddr($data1_ref->[2]), '$data1[2] refers to @data2' ); } }
Note: Contents of the file are exactly as specified in the book.
@data1 = ( 'one', 'won', [ 'two', 'too', 'to', \@data1 ] ); @data2 = @{$data1[2]};
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Marshalling Data
by choroba (Cardinal) on May 21, 2016 at 19:14 UTC | |
by BillKSmith (Monsignor) on May 21, 2016 at 22:10 UTC | |
|
Re: Marshalling Data
by Aldebaran (Curate) on May 22, 2016 at 06:14 UTC |