EchoAngel has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I cannot find an example of saving a hash to file and retrieving the hash back ONLY using the data dumper module. I know the storable has a way but it's binary like. I want data dumper since it saves stuff as text (readable) Thanks
  • Comment on Need Example of Saving and Retrieving Hash from File

Replies are listed 'Best First'.
Re: Need Example of Saving and Retrieving Hash from File
by Joost (Canon) on Mar 29, 2005 at 00:04 UTC
    Personally, I'd use YAML's LoadFile and DumpFile functions, unless you need to store objects.

    For Data::Dumper, something like

    # dump to file local $Data::Dumper::Purity = 1; open DATA,">","some/file" or die $!; print DATA Dumper( $hashref ); close DATA; # read from file open DATA,"<","some/file" or die $!; local $/; my $hashref = eval(<DATA>) or die $@; close DATA; # or even my $hashref = do("some/file") or die $!;
    should work
Re: Need Example of Saving and Retrieving Hash from File
by cowboy (Friar) on Mar 29, 2005 at 00:34 UTC
    While you mentioned only using Data::Dumper, your reasoning for choosing it was it saves in a readable format. You might consider XML::Dumper instead. You gain the benefit of not having to eval{} the data back in, which could be dangerous, and it is still readable. (well, readable enough at any rate, not quite as nice as Data::Dumper)
Re: Need Example of Saving and Retrieving Hash from File
by tlm (Prior) on Mar 29, 2005 at 00:10 UTC

    Here's a crude example:

    use Data::Dumper; my %hash = ( foo => 1, bar => 2, baz => 3 ); open my $out, '>', 'save' or die $!; print $out Dumper(\%hash); close $out; open my $in, 'save' or die $!; my $h = eval do { local $/; <$in> }; close $in; print "$_ => $h->{$_}\n" for keys %$h;
    But look at the contents of 'save' to see what eval is actually evaluating.

    the lowliest monk

      As I said earlier, thawing the data was the hard part. Because yours fails if you use strict. By putting in a "print $@" right after the eval, and enabling strict, you'll see:

      Global symbol "$VAR1" requires explicit package name at (eval 1) line +1, <$in> line 1.
      Not good. The trick? Change your eval line to:
      my $h = eval 'my ' . do { local $/; <$in> };
      We get a lexical "$VAR1" which allows the whole thing to compile. Things don't work so well if you're dumping multiple variables, though. With multiple variables, you either pre-declare all variables using my (or our, or use var), or you split on ;, and put a "my " in front of each variable. The former is dangerous in that you may fail to predeclare some variable, the latter in that a semicolon may show up elsewhere in the data. So it's best to count on a single variable anyway.

        I'm aware of the situation with $VAR1, that's why I cautioned the OP to look at the contents of the file to see what perl is being asked to evaluate. That's one of the reasons why I use Storable instead of Data::Dumper; if I'm stuck with having to use D::D for some reason then I use the two-argument version of Dump so that I can control the names of the LHS variables in the dumped string, and then when I restore I rely on the dumped assignment. E.g., in the storing code:

        print $out Data::Dumper->Dump( [ \%hash ], [ '*dict' ] );
        and in the retrieving code:
        my %dict; eval $dumped_string # initializes %dict or die $@;

        the lowliest monk

Re: Need Example of Saving and Retrieving Hash from File
by Tanktalus (Canon) on Mar 29, 2005 at 00:06 UTC
    I'm sure someone will have some advice for me, too, here, but here is a snippet of some code I have for saving meta-information (basically, a hash in my situation). The _thaw method was the hardest to figure out (using both strict and warnings).
Re: Need Example of Saving and Retrieving Hash from File
by ambs (Pilgrim) on Mar 29, 2005 at 11:10 UTC
    To use a readable format is, in fact, a good way to go. In any case, if you are working with a big Hash, storable would be faster (a lot). Just to let you know.

    Alberto Simões

      Hi, But I felt like people who are not aware of the Storable module tell what is this data ie; not human readable. There are people who want the data even not to be in any encrypted form
Re: Need Example of Saving and Retrieving Hash from File
by lidden (Curate) on Mar 29, 2005 at 09:34 UTC
    You could use do like this:
    my %hash = ('foo' => 3, 'bar' => 'baz'); open my $out, '>', 'A_tmp_file' or die "Damn: $!"; print $out Dumper(\%hash); close $out or die $!; my $ref = do 'A_tmp_file' or die "Noo: $!"; print "$ref->{'bar'}\n";
    See "perldoc -f do"
      thanks a lot!