Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: How to save and reload my hash

by mrdvt92 (Acolyte)
on Apr 23, 2010 at 14:18 UTC ( [id://836529]=note: print w/replies, xml ) Need Help??


in reply to Re: How to save and reload my hash
in thread How to save and reload my hash

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

Replies are listed 'Best First'.
Re^3: How to save and reload my hash
by Your Mother (Archbishop) on Sep 23, 2014 at 03:23 UTC

    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.

        If Storable is doing it for you, don’t worry. It’s only something to worry about in certain circumstances or if you like to be able to eye-ball the data. Storable and JSON both serialize and marshal (deserialize) references. It’s fully reconstituted, it just has to be accessed with dereferencing semantics (%$hash or $hash->{key}); you might want to look at perlref. Sounds like you have it working now though, so ++

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-28 13:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found