Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Creating a persistent state for File::Monitor

by bfnpmsz (Initiate)
on Jan 17, 2013 at 15:12 UTC ( #1013799=perlquestion: print w/replies, xml ) Need Help??
bfnpmsz has asked for the wisdom of the Perl Monks concerning the following question:

Good day all, I am attempting to create a state file for File::Monitor so I can load the last state of the monitors and monitor file changes at set intervals. I do not want to have a constant script running on the CPU with a sleep. I want to run it upon demand. Do you have any suggestions on a way of doing this. Any input would be helpful.
  • Comment on Creating a persistent state for File::Monitor

Replies are listed 'Best First'.
Re: Creating a persistent state for File::Monitor
by tobyink (Abbot) on Jan 17, 2013 at 16:13 UTC

    Here's an example module for saved state between script runs. Save it as in one of your Perl library directories...

    { package STATE; use strict; use warnings; use Storable qw( retrieve store ); use constant FILENAME => sprintf '%s.state', $0; sub import { no strict 'refs'; my $p = caller; *{"$p\::STATE"} = (${^STATE}{$p}||={}); } sub BEGIN { %{^STATE} = %{ -f FILENAME ? retrieve FILENAME : {} } } sub END { store \%{^STATE} => FILENAME; } } 1;

    Now you can load that module with use STATE. Once loaded, there will be a global hash called %{^STATE} which all modules have access to. Also, within any packages that call use STATE there will also be a hash called %STATE which acts as an alias for %{${^STATE}{(__PACKAGE__)}}.

    So you can write a script like this:

    #!/usr/bin/perl use STATE; print $STATE{counter}++, "\n";

    ... and it will retain state between runs, the counter being incremented each time.

    It does this in a rather simplistic way, using a state file with the same name as the script, just with ".state" tacked onto the end of the filename.

    This is not necessarily how you'd want to do things in a major project (often the permissions of the directory where you keep the script would preclude you from storing the state there!) but serves as a good simple example I think.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1013799]
Approved by Corion
Corion is mildly surprised as some different toy projects coagulate into a useful application. I've been toying with writing a Meetup wrapper and also have been toying with synchronizing events between (CalDAV) calendars, and now that means I can ...
[Corion]: ... easily* synchronize my personal calendar with Meetup and import stuff from Meetup there
[Corion]: * Code not yet written, some restrictions may apply

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (11)
As of 2017-02-23 09:35 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (343 votes). Check out past polls.