http://qs1969.pair.com?node_id=11148149

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:

#!/usr/bin/perl use warnings; use strict; use Storable; my $HASHFILE='file'; package main; sub DoIHaveThisData { my ($myhash, $key) = @_; if (exists $myhash->{$key}) { return 1; } return -1; } unless(-e $HASHFILE){ printf "Hash file \'%s\' doesn't exist, creating...\n", $HASHF +ILE; my %emptyHash = (); # Serialize emptyHash, store it in $HASHFILE: store \%emptyHash, $HASHFILE; # If we reach here, there should now be an empty hash, seriali +zed in file $HASHFILE } # Deserialize the hash, now access it as $hashref: my $hashref = retrieve($HASHFILE); # Check to see if key2 is there: if(DoIHaveThisData($hashref, 'key2')){ printf "Hash has key2! (Value is: \'%s\')\n", $hashref->{'key +2'}; } # Add/Overwrite some data: $hashref->{'key1'} = 'data1'; $hashref->{'key2'} = 'data2'; $hashref->{'key3'} = 'data3'; # Save the hash for the next time this script runs: store $hashref, $HASHFILE; # END OF PROGRAM

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:

me@ubuntu01$ rm file me@ubuntu01$ me@ubuntu01$ me@ubuntu01$ ./SerialHashTest.perl Hash file 'file' doesn't exist, creating... Use of uninitialized value in printf at ./SerHash2.perl line 33. Hash has key2! (Value is: '') me@ubuntu01$ me@ubuntu01$ me@ubuntu01$ ./SerialHashTest.perl Hash has key2! (Value is: 'data2') me@ubuntu01$ me@ubuntu01$

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:

Use of uninitialized value in printf at ./SerHash2.perl line 33. Hash has key2! (Value is: '')

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.