Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Use a Serialized Hash... When It Might Not Exist? by redapplesonly

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found