Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this?Last hourOther CB clients
    Other Users?
    Others imbibing at the Monastery: (4)
    As of 2024-07-24 21:14 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found

      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.