The stupid question is the question not asked | |
PerlMonks |
Re: Creating a persistent state for File::Monitorby tobyink (Canon) |
on Jan 17, 2013 at 16:13 UTC ( [id://1013806]=note: print w/replies, xml ) | Need Help?? |
Here's an example module for saved state between script runs. Save it as STATE.pm in one of your Perl library directories...
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:
... 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'
In Section
Seekers of Perl Wisdom
|
|