Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: How to save and reload my hash

by ivancho (Hermit)
on Nov 20, 2005 at 06:20 UTC ( [id://510204]=note: print w/replies, xml ) Need Help??


in reply to How to save and reload my hash

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')};

Replies are listed 'Best First'.
Re^2: How to save and reload my hash
by mrdvt92 (Acolyte) on Apr 23, 2010 at 14:18 UTC

    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; '

      Update: in reply to a four year-old reply to a five (well, nine) year-old post!

      Just be aware that nstore is safer than store and nothing is bombproof with Storable except the same particular build and version. Storable is a specialized format, not a standard. If you want a serialization that cannot break on updates, can be used across languages, is usually as fast and sometimes faster than Storable, and happens to be human readable as well, I encourage you to check out JSON(::XS <- for the speed).

        I appreciate the feedback. I did notice something in all the examples I've been able to find regarding Storable and JSON. It has to do with the load from file back to the hash itself. Unless I'm missing something, and that's very possible, there seems to be a missing component of the hash reconstruction from the anonymous hash example %{} illustrated in responding comments. PERL seems to be handling the illusion of the reconstructed hash, until you try and use it in a foreach/keys loop. The assignment becomes a hashref which doesn't seem to fully reconstitute the hash from the file, because I could not get it to work with the foreach/keys loop. It wasn't until I learned about %$ to correct that. Do you see any issues with %$ ? The PERLDOC for Storables warned about the retrieve, would cause some sort of serialization pitfall, thus, causing the foreach/keys not to work. Until %$. The code examples used in the response, served my purpose for a daily PERL script I run at least 20 times a day, and may offer some relief to others trying to reconstitute the hash from a file, so it works as it did before the store.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://510204]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-03-29 00:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found