![]() |
|
Syntactic Confectionery Delight | |
PerlMonks |
Use a Serialized Hash... When It Might Not Exist?by redapplesonly (Sexton) |
on Nov 11, 2022 at 22:39 UTC ( #11148149=perlquestion: print w/replies, xml ) | Need Help?? |
redapplesonly has asked for the wisdom of the Perl Monks concerning the following question: Hola Perl Monks, I am working on a Perl (v5.30.0) script that needs to use stateful information from the previous run of the script. In my imagination, I can see the script basically working like this: >> STEP ONE: Loads stateful data from an external file >> STEP TWO: Crunches stateful data >> STEP THREE: Saves stateful data to the external file for the next iteration I'm pretty confident that I can use a serialized hash to store the stateful information. I've read up on Perl's Storable module, plus the store() and retrieve() functions. Seems doable. But here's the problem: What about the first time the script runs? There would be no external file. So the code would have to be smart enough to check for the existence of the file, and create it if necessary. I didn't think this would be a hard problem, but its got me vexed. Can you take a look? Here's my test code:
This is pretty bulky, and I'm pretty sure there's got to be a smarter (or more compact) way to do all of the above. But there's a problem which concerns me: Here's the command line output when I delete the external file, then run my script twice in a row:
Okay, so on Iteration 1, the code realized that the external file was missing, and created it. So that's good. But I worry about this part of Iteration 1:
Line 33 is printf "Hash has key2! (Value is: \'%s\')\n", $hashref->{'key2'}; in the if(DoIHaveThisData($hashref, 'key2')) block of code. DoIHaveThisData() believes that key2 is in the hash, when the hash has yet to be populated. So why does the function return a false positive? Is there a logic bug? Or is my hash populated with a key2 --> nothing key/value pair by default? Any advice/criticism is welcome. I have a funny feeling that the design of my code is unnecessarily complicated, so if you have any design suggestions, I'll happily lap them up. Thanks in advance for your consideration.
Back to
Seekers of Perl Wisdom
|
|