> -----Original Message-----
> From: Lyle [mailto:[email protected]]
> Sent: 27 September 2010 02:25
> To: Eric Robertson
> Subject: Re: Restoring Dumped Values
>
> On 26/09/2010 23:12, Eric Robertson wrote:
> >
> > I've produced a complicated hash that has as its values anonymous
> > hashes and I want to store the hash in a text file and then in another
> > program use this text file to reconstitute the original hash. Using
> > Data::Dumper I've succeeded with the first part and produced the text
> > file with 'print OUT Dumper (%hash)' but using eval with the contents
> > of the file doesn't reproduce the original hash file.
> >
>
> Shouldn't that be
> print OUT Dumper (\%hash)
>
> Data dumper will name it $VAR1 or something like that. You can get it to
> name it the same as the original, but I think it'll still need to be a
> reference.
> Maybe:
>
> Data::Dumper->Dump([\%hash], [qw(hash)]);
>
> Will work (untested). Then if you really didn't want it as a reference:
>
> our ( $hash, %hash );
> require "dumpfile.pl";
> %hash = %$hash;
>
> But that would be silly. Really you should look at data serialization, and
> encode the data structure to YAML or JSON before writing to a file.
> That way it could potentially be read in a decoded by any number of
> languages.
>
>
> Lyle
Thanks for your comments.
Yes, I should have used a reference - not using one gave me a reasonable
text representation of my hash. It was of the following form:
$tempHash1{one}=1;
$tempHash1{two}=2;
$tempHash1{three}=3;
$mainHash{first} = \%tempHash1;
$tempHash2{four}=4;
$tempHash2{five}=5;
$tempHash2{six}=6;
$mainHash{second} = \%tempHash2;
However storing by reference I still couldn't retrieve the hash so I'm going
to use Storable as Bill Leubkert has suggested.
As regards using YAML or JSON the program I'm working on is in two parts -
picking up data from a set of csv files and then popping these values into a
MySQL database. I've finished the first part and rather than carry on
testing the second part in the same program I'm dealing with this separately
since extracting the data takes about 10 seconds and having to go through
this every time would slow down my debugging of the second part in Komodo.
The idea then was to dump the data at the end of the first program and then
retrieve this to start off the testing of the second one. Eventually the two
programs will be joined together so this is just a temporary arrangement and
no one else will need to read the dumped file, which will be disappearing.
I'm familiar with complicated hashes but have never used JSON (as yet) so
it's much easier for me to use hashes - I haven't come across YAML.
Eric
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs