Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Use a Serialized Hash... When It Might Not Exist?

by kcott (Archbishop)
on Nov 12, 2022 at 02:33 UTC ( [id://11148154]=note: print w/replies, xml ) Need Help??


in reply to Use a Serialized Hash... When It Might Not Exist?

G'day redapplesonly,

"I am working on a Perl (v5.30.0) script that needs to use stateful information from the previous run of the script."

All of your steps, and other design notes, seem spot-on to me. :-)

"But here's the problem: What about the first time the script runs?"

That's a very valid point. I don't use Storable very often, but when I do I encounter the same issue. I usually handle this by dealing with it before runtime execution starts in an INIT block.

Here's a rough example of how I might have written your code (pm_11148149_storable_first_run_0.pl):

#!/usr/bin/env perl use 5.030; use warnings; use Storable; my ($hashfile, $serial_data_for); INIT { $hashfile = 'pm_11148149_storable_first_run.sto'; $serial_data_for = -e $hashfile ? retrieve($hashfile) : {}; } my @keys = qw{key1 key2 key3}; my @vals = qw{data1 data2 data3}; $serial_data_for->@{@keys} = @vals; store($serial_data_for, $hashfile);

I added some extra statements for reporting/demo purposes (pm_11148149_storable_first_run_1.pl):

#!/usr/bin/env perl use 5.030; use warnings; use Storable; use Data::Dump; warn "Perl version: $^V\n"; my ($hashfile, $serial_data_for); INIT { $hashfile = 'pm_11148149_storable_first_run.sto'; $serial_data_for = -e $hashfile ? retrieve($hashfile) : {}; } say 'Serialised data:'; dd $serial_data_for; my @keys = qw{key1 key2 key3}; my @vals = qw{data1 data2 data3}; $serial_data_for->@{@keys} = @vals; store($serial_data_for, $hashfile);

Here's some equivalent output to your sample run:

ken@titan ~/tmp $ rm pm_11148149_storable_first_run.sto ken@titan ~/tmp $ ls -l pm_11148149_storable_first_run.sto ls: cannot access 'pm_11148149_storable_first_run.sto': No such file o +r directory ken@titan ~/tmp $ ./pm_11148149_storable_first_run_1.pl Perl version: v5.30.0 Serialised data: {} ken@titan ~/tmp $ ls -l pm_11148149_storable_first_run.sto -rw-r--r-- 1 ken None 69 Nov 12 13:00 pm_11148149_storable_first_run.s +to ken@titan ~/tmp $ ./pm_11148149_storable_first_run_1.pl Perl version: v5.30.0 Serialised data: { key1 => "data1", key2 => "data2", key3 => "data3" } ken@titan ~/tmp $

Notes:

  • I concur with ++choroba's suggestion to use the builtin exists() function directly, rather than rolling your own DoIHaveThisData(). While abstracting functions into subroutines for reuse is often a very good idea, I don't see any benefit in this instance. If you did consider it to be necessary, I'd keep it very simple along the lines of:
    sub DoIHaveThisData { my ($hash, $key) = @_; return exists $hash->{$key}; }
  • You may need to look at "perlref: Postfix Reference Slicing", and perhaps even "perldata: Slices", if you're unfamiliar with how I've populated the hashref.
  • A .sto extension, to identify Storable files, is widely used. It's not a requirement; it might qualify as a convention.

— Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-24 22:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found