Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Using Data::Dumper to dump an array of hashes to a file

by wishartz (Beadle)
on Aug 14, 2008 at 16:10 UTC ( #704380=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks, I'm having problems trying to dump an array of hashes into a file, so I can use the data structures in another program. Could you please help? My structure looks like this:
disk_stats[array_index]{key}=value
I can populate the array of hashes, but how do I dump it? I have tried the following, but it doesn't work:
open (FILE, "> diskstats.perldata") or die "can't open diskstats: $!"; print FILE Data::Dumper->Dump([\@disk_stats]); close FILE or die "can't close diskxstats: $!";
How would I read it back into a program as well? I have read the documentation for Data::Dumper, but still cannot figure out how to do it? Thanks

Replies are listed 'Best First'.
Re: Using Data::Dumper to dump an array of hashes to a file
by kyle (Abbot) on Aug 14, 2008 at 16:26 UTC

    Here's a little demo:

    use strict; use warnings; use Data::Dumper; my @aoh = ( { a => 1 } ); my $file = 'diskstats.perldata'; out( $file, \@aoh ); undef @aoh; @aoh = in( $file ); sub out { my ( $file, $aoh_ref ) = @_; open my $fh, '>', $file or die "Can't write '$file': $!"; local $Data::Dumper::Terse = 1; # no '$VAR1 = ' local $Data::Dumper::Useqq = 1; # double quoted strings print $fh Dumper $aoh_ref; close $fh or die "Can't close '$file': $!"; } sub in { my ( $file ) = @_; open my $fh, '<', $file or die "Can't read '$file': $!"; local $/ = undef; # read whole file my $dumped = <$fh>; close $fh or die "Can't close '$file': $!"; return @{ eval $dumped }; }

    Notes:

    • I don't check here whether the eval succeeded, but that would be a good idea.
    • These are bad sub names.
    • These are not general dump/save subs. They're meant just for arrays.
    • Consider using YAML or Storable instead since it doesn't have the possibility of executing things if someone hostile gets a hold of the dump file.
      Thanks for your reply.

      One thing that I don't understand, is why do I have to include the subroutine 'in' in my program for it to dump out the contents of the array? I thought the subroutine 'in' was what I was just using in my other program to read the file?

      And it only seems to dump it, if I press <crtl+c> and kill the program? Unless, it is just taking a long time to write the contents to disk?

        Sorry, my mistake. I had put the subroutines in the middle of my code, when they needed to be at the end, when I had finished with the array.

        Thanks again

      sub in { my ( $file ) = @_; open my $fh, '<', $file or die "Can't read '$file': $!"; local $/ = undef; # read whole file my $dumped = <$fh>; close $fh or die "Can't close '$file': $!"; return @{ eval $dumped }; }

      Wouldn't do or require accomplish just the same task, but more succinctly?

        Mostly, yes.

        I wouldn't use require because it would decline to load the same file twice (unless you monkey with %INC). Also, I don't know if it would return the value returned by the file (I think it doesn't).

        Using do is a better choice, but the error handling is different. Here's some code I wrote a while back after examining the docs for a while:

        if ( ! defined do $rc_file ) { die "Error in '$rc_file': $@" if $@; die "Can't read '$rc_file': $!" if $!; warn "'$rc_file' returned undef as last value"; }

        It's not as easy as just "return @{ do $file }", but I admit I like it better. Using do has some other differences noted by the documentation, but nothing I'd expect to trip us up in this case.

Re: Using Data::Dumper to dump an array of hashes to a file
by Your Mother (Archbishop) on Aug 14, 2008 at 18:42 UTC

    What kyle said: "Consider using YAML or Storable instead since it doesn't have the possibility of executing things if someone hostile gets a hold of the dump file."

    The YAML is more terse too. Storable is very fast and nice but it has some caveats and isn't always portable (though nstore can help) or compatible with version changes.

    use strict; use warnings; use YAML qw( DumpFile LoadFile ); my @aoh = ( { a => 1 } ); my $file = 'diskstats.perldata'; DumpFile( $file, \@aoh ); my $reconstituted = LoadFile($file); # check it out in Data::Dumper style- use Data::Dumper; print Dumper $reconstituted;
Re: Using Data::Dumper to dump an array of hashes to a file
by planetscape (Chancellor) on Aug 15, 2008 at 05:07 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://704380]
Approved by kyle
Front-paged by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2021-05-08 11:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (96 votes). Check out past polls.

    Notices?