develooper Front page | perl.beginners | Postings from March 2002

Re: Data::Dumper documentation ?

Thread Previous | Thread Next
Chas Owens
March 19, 2002 04:24
Re: Data::Dumper documentation ?
Message ID:
On Tue, 2002-03-19 at 05:44, wrote:
> heh - that was exactly half of what i was looking for - how to dump stuff! great :)
> mayby you can help me out with the second part too? :
> if i have a datastructure (a hash) dumped to a file using Data::Dumper,
> how can i recreate the datastructure/hash in another program?
> if i do something like this:
> open SOURCE, source.file;
> undef $/;
> eval SOURCE;
> die $@ if $@;
> close IN;

This should look something like (note the code below has not been tested
on animals, in fact the code below has not been tested at all <G>)

my $hashref;
{ #block to control scope
	local ($/) = undef; #make this change local so as not to screw
	                    #up anyone else
	open SOURCE, 'source.file';
	$_ = <SOURCE>;
	s/^\$.*? /\$hashref/; #make sure the variable is named $hashref
	die $@ if $@;
	close SOURCE;

> then eval will turn the datastructure into a scalar? but i want it as a hash!
> or how does it work ?
> :o)

The scalar is a reference to a hash.  You can access the keys like this:

print $hashref->{key1};

or you can say

my %hash = %$hashref;

Please note that this only does a shallow copy, but that should be
enough if you have no plans to use the data in $hashref (ie you are only
using it as a temporary storage location for the variable before shoving
it into a hash variable).  You may also be interested in the Storable
and FreezeThaw modules.

> martin
> ps, love the missile address line!
Today is Pungenday the 5th day of Discord in the YOLD 3168
Today is also Mojoday
Missile Address: 33:48:3.521N  84:23:34.786W

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About