dbs has asked for the wisdom of the Perl Monks concerning the following question:
Hi!
Is there a module that will allow me to keep a variable
at a certain value across different runtimes? Whats the name?
So I run my script at 2PM, a value gets set to 5 then
I run the same script at 4PM the value remains at 5.
I run it again at 430PM this time setting the value to 6.
The next day (no reboot) I run it at 8AM and the value is still at 6.
thx!
Re: memory persistance
by chromatic (Archbishop) on Aug 25, 2011 at 20:20 UTC
|
I use DBD::SQLite for this. My cron jobs launch shell scripts which set up paths to driver programs which connect to databases, perform some work, and then exit.
The initial setup and learning curve may seem daunting (and it may be daunting for your purposes), but I've found it easier on my projects than starting with flat files and reinventing file handling and the such.
With that said, if you use something like YAML::XS to abstract away the file loading, parsing, and saving, flat files aren't too bad.
| [reply] |
|
cool thanks! I am looking at IPC-ShareLite-0.17 and IPC-SharedCache-1.3. Thx!
| [reply] |
Re: memory persistance
by ww (Archbishop) on Aug 25, 2011 at 20:51 UTC
|
#!/usr/bin/perl
use strict;
use warnings;
use 5.012;
BEGIN: recover_var();
# main here
# ... insert your existing code for getting the value here
# Since you didn't tell us the variable name, we'll use $var and assum
+e you've used it
my $var;
say "(\t\t Simulating execution of existing...";
say "\t\t code to obtain a (new) value for \$var";
say "\t\t and NOW simulating that said execution is complete. \t)\n";
$var += int(rand(113));
say "\n\tTake a pencil and write the current value of \$var, $var, on
+your yellow sticky.";
say "\t Otherwise, the value of \$var will disappear when the RAM used
+ by \n\t this script is returned to the OS on termination of the scri
+pt./n";
say "\n\t\t\tTerminating...";
exit;
sub recover_var {
if ( !$var ) {
print "\n\t Find your yellow sticky (you did write down the old va
+lue, didn't you?) \n\t and enter the last \$var found on the sheet\n\
+t or '0' if no sticky is found: ";
chomp ($var = <>);
say "\n The last known value of \$var was $var.\n";
return;
}
}
Tested, but may not be suitable for purchaser's application. No warranty of fitness implied.
Despite the actual syntax error (the begin block that isn't), this passed perl -c ... with flying colors. Expanding on the note above, this is NOT suitable for OP's need, but taken litterally, OP asked for a way to keep a variable in memory after script termination... so here's a way, albeit with "dead tree RAM".
:-) | [reply] [d/l] [select] |
|
| [reply] |
|
...and if user selects a recording implement other than a pencil?
| [reply] |
Re: memory persistance
by dwm042 (Priest) on Aug 25, 2011 at 20:57 UTC
|
Do you see any particular advantage to keeping such a value in memory rather than reading and writing such a value to a file or database?
If speed were required, keep the file on a ramdisk.
| [reply] |
Re: memory persistance
by sundialsvc4 (Abbot) on Aug 26, 2011 at 13:56 UTC
|
To my way of thinking, “IPC sharing” modules are intended, as the acronym implies, for Inter-Process Communication. In other words, two or more processes are running at the same time and they need to communicate with one another reliably in real-time. Which is not the case here.
What you seem to be describing is a persistence mechanism, for preserving data between runs that occur many hours apart. “Storing the data in memory” would, in my view, be entirely unsuitable.
SQLite is an excellent suggestion because, even though it is “a disk file,” it is also a database in that you can keep any number of tables inside the thing, and you can run queries against it, all without involving a database server. (If you do have a database server at-hand, and a friendly DBA, you might also consider using a database on that server for this and any similar purposes.) You might even consider using this storage, not merely for “the parameters needed for the next run,” but for a log about previous runs that have been made, and/or for anything that you might need to support re-runs.
If you do use SQLite, here’s one important caveat from experience: use transactions. If you don’t, SQLite will obediently verify every single disk-write by re-reading the data, as it has been programmed to do, thus dropping performance to one-half disk drive speeds. Wrap even small requests in a transaction so that SQLite will know that it can buffer the data in-memory for a while and do “lazy writes,” with a dramatic improvement in performance. Also note that SQLite (IMHO...) really isn’t architected with “many simultaneous writers” in mind.
| |
Re: memory persistance
by spadacciniweb (Curate) on Aug 29, 2011 at 11:20 UTC
|
| [reply] |
|
| [reply] |
|
You can also store data in network order to allow
easy sharing across multiple platforms, or when storing
on a socket known to be remotely connected. The routines
to call have an initial n prefix for network, as in nstore
and nstore_fd . At retrieval time, your data will be
correctly restored so you don't have to know whether you're
restoring from native or network ordered data. Double
values are stored stringified to ensure portability as
well, at the slight risk of loosing some precision in the
last decimals.
and
This version of Storable will defer croaking until it encounters a dat
+a type in the file that it does not recognize. This means that it wil
+l continue to read files generated by newer Storable modules which ar
+e careful in what they write out, making it easier to upgrade Storabl
+e modules in a mixed environment.
Mariano
(($_="Mzz ojjdloobnf jt uvy5502383")=~y~b-zg2-5c96-81~a-z0-9~s)=~s~~~s; print
| [reply] [d/l] [select] |
|
|