Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Read the Dumper data back to a hash.

by rachitmohta11 (Novice)
on Jun 21, 2010 at 06:34 UTC ( #845672=perlquestion: print w/ replies, xml ) Need Help??
rachitmohta11 has asked for the wisdom of the Perl Monks concerning the following question:

I have a hash output stored in a Dumper file. The content looks like this. Its actually a dynamic hash.
$VAR1 = 'ASD'; $VAR2 = {}; $VAR3 = 'asdf'; $VAR4 = { 'slkfdh' => { 'aslkfd' => {} } };
How can i read this data into a hash in my script?

Comment on Read the Dumper data back to a hash.
Download Code
Re: Read the Dumper data back to a hash.
by moritz (Cardinal) on Jun 21, 2010 at 06:38 UTC

    Don't dump the hash as

    print Dumper %hash;

    But rather use

    print Dumper \%hash; # ^

    Then you can simply use eval to obtain a hash reference, so for example

    my %copied_hash = %{ eval $string };
    Perl 6 - links to (nearly) everything that is Perl 6.
      What is $string in eval $string? Is it the file handle?

        The string returned by Dumper.

Re: Read the Dumper data back to a hash.
by CountZero (Bishop) on Jun 21, 2010 at 07:18 UTC
    Has the age-old art of reading the documentation indeed finally been lost?

    From the docs of Data::Dumper:

    Given a list of scalars or reference variables, writes out their contents in perl syntax. The references can also be objects. The content of each variable is output in a single Perl statement. Handles self-referential structures correctly.

    The return value can be evaled to get back an identical copy of the original reference structure.

    And see also the example in the Synopsis.


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Read the Dumper data back to a hash.
by kejohm (Hermit) on Jun 21, 2010 at 08:46 UTC

    I prefer to use the Data::Dump module, since it has a simpler interface and it doesn't try to set any variables, which makes it more convenient to use in most cases. Here is a sample of writing a structure to a file and reading it back again using Data::Dump:

    #!perl use strict; use warnings; use Data::Dump qw(dump); # Create a complex structure my %hash = ( number => 42, string => 'This is a string', array => [ 1 .. 10 ], hash => { apple => 'red', banana => 'yellow' }, ); # See what it looks like print "Here is the structure before dumping to file:\n"; dump \%hash; # Print structure to file open my $out, '>', 'dump_struct' or die $!; print {$out} dump \%hash; close $out; # Read structure back in again open my $in, '<', 'dump_struct' or die $!; my $data; { local $/; # slurp mode $data = eval <$in>; } close $in; # See what the structure read in looks like print "Here is the structure after reading from file:\n"; dump $data; __END__
Re: Read the Dumper data back to a hash. (safe undumper )
by Anonymous Monk on Jun 04, 2013 at 23:41 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://845672]
Approved by moritz
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (13)
As of 2014-10-01 12:05 GMT
Find Nodes?
    Voting Booth?

    What is your favourite meta-syntactic variable name?

    Results (10 votes), past polls