Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How to save and reload my hash

by lepetitalbert (Monsignor)
on Nov 20, 2005 at 06:12 UTC ( #510202=perlquestion: print w/ replies, xml ) Need Help??
lepetitalbert has asked for the wisdom of the Perl Monks concerning the following question:

Hello dear Monks,

I have the following data structure.

I'd like to :
- store this hash in a file.
- reload the file in a hash.

I read about Storable , YAML , Data::Dumper , MLDBM , ...

but am lost (again!) with these 'freeze' , 'thaw' , 'bless' , ...

The only thing I was able to do is save the hash to a file with :

print FILE Dumper (\%host_info);

But is it a way to reload this in my %host_info ?

How can I save and reload my hash ?

Thanks.

Have a nice day.

"There is only one good, namely knowledge, and only one evil, namely ignorance." Socrates

Comment on How to save and reload my hash
Download Code
Re: How to save and reload my hash
by ivancho (Hermit) on Nov 20, 2005 at 06:20 UTC
    first 3 lines of Storable's synopsis:
    use Storable; store \%table, 'file'; $hashref = retrieve('file');

    second line stores to 'file', third line restores from 'file' ( possibly in another script )
    if you wanted a hash instead of a hash ref, wrap the "retrieve" call in %{} - ie,
    my %host_info = %{retrieve('file')};

      Storable works great for me! Thanks to a five year old post.

      perl -e ' use Storable; my $hashfile="file.hash"; store {}, $hashfile unless -r $hashfile; my $hash=retrieve($hashfile); print join(",", %$hash), "\n"; $hash->{"index"}++; store $hash, $hashfile; '
Re: How to save and reload my hash
by neosamuri (Friar) on Nov 20, 2005 at 07:17 UTC

    For using data dumper all you havev to do in order to get the data back is use eval to recreate the data structure. Also of note you should also set $Data::Dumper::Purity to 1 if you have nested references

    Here is a example:

    #Save use Data::Dumper; $Data::Dumper::Purity = 1; open FILE, ">$outfile" or die "Can't open '$outfile':$!"; print FILE Data::Dumper->Dump([$main], ['*main']); close FILE; #restore open FILE, $infile; undef $/; eval <FILE>; close FILE;
Re: How to save and reload my hash
by GrandFather (Cardinal) on Nov 20, 2005 at 08:05 UTC

    Take a look at XML::Simple. It does exactly what you want:

    use XML::Simple; my $options = XMLin ('teams.badDB') if -e 'teams.badDB'; # do stuff with $options $options->{'time'} = 15 * 60 if ! exists $options->{'time'}; $options->{'width'} = 200 if ! exists $options->{'width'}; # ... open outFile, '>', 'teams.badDB'; print outFile XMLout ($options); close outFile;

    DWIM is Perl's answer to Gödel
Re: How to save and reload my hash
by planetscape (Canon) on Nov 20, 2005 at 09:04 UTC

    You might also wish to have a look at this, which provides an overview of some of the options for serializing data structures.

    HTH,

    planetscape
Re: How to save and reload my hash
by davidrw (Prior) on Nov 20, 2005 at 12:30 UTC
    Also have a look at Cache::FileCache:
    my $hostname = 'foo.bar.example.com' use Cache::FileCache; my $cache = new Cache::FileCache( { 'namespace' => 'HostInfo', 'default_expires_in' => 600 } ); my $cacheKey = $hostname; my $host_info = $cache->get( $cacheKey ); if ( not defined $host_info ) { $customer = get_host_info_as_hashref( $hostname ); $cache->set( $cacheKey, $host_info, "10 minutes" ); } return $host_info;

      Hello dear Monks,

      first thanks for you answers.

      I first tried : (cos it's the shortest)

      use Storable; # fill my hash store \%host_info, 'file'; %host_info = (); %host_info = %{retrieve('file')}; # print my hash

      This worked. Don't know what I did yesterday, when I used

      store \%host_info, 'file';

      (probably not exactly that) It had a file with just

      pst01234(and some unprintable chars).

      Maybe I should not code until 6am.

      Thank you all.

      Have a nice day !

      "There is only one good, namely knowledge, and only one evil, namely ignorance." Socrates
        The output that Storable produces is not meant to be human-readable. So it's probably true that whatever you were doing worked just fine. You just have to sort of trust (or test it) that when you read it back in using retrieve it'll come out ok.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2014-09-22 00:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (176 votes), past polls