Re: best way to keep a simple count?
by rob_au (Abbot) on Apr 11, 2005 at 10:54 UTC
|
For a proven wheel for this solution, take a look at File::CounterFile - It's not just for web counters! :-)
perl -le "print unpack'N', pack'B32', '00000000000000000000001000000000'"
| [reply] |
Re: best way to keep a simple count?
by tlm (Prior) on Apr 11, 2005 at 13:51 UTC
|
| [reply] |
Re: best way to keep a simple count?
by monkfan (Curate) on Apr 11, 2005 at 12:44 UTC
|
Advices I got from experts:
open I, "<counter.txt"; my $n = <I>; chomp($n); close(I); $n++; open O
+, ">counter.txt"; print O $n; close(O);
or
Storable::nstore("file", \$count);
or using File::Slurp
perl -MFile::Slurp -le 'write_file "tmp42", $n = 1 + eval{read_file "t
+mp42"}; print $n'
| [reply] [d/l] [select] |
|
Some experts gave you bad advice. You need to read and update the file as one step, otherwise another process comes along and tries to do the same thing while you are in the middle. Either the count doesn't get incremented properly (both processes read the same value before either one wrote a new one), the count gets reset (one process reads the file just after it was truncated), or the count disappears.
--
brian d foy <brian@stonehenge.com>
| [reply] |
Re: best way to keep a simple count?
by willyyam (Priest) on Apr 11, 2005 at 12:47 UTC
|
I'm a sucker for log files, so I would probably capture the date and time of the program's run, perhaps its arguments (if any) on one line of a log file. Then, I could just run
wc -l /logfile for a count, but then I can capture other information as well.
| [reply] [d/l] |
Re: best way to keep a simple count?
by Anonymous Monk on Apr 11, 2005 at 12:03 UTC
|
Been there, done that, did it in many ways. I've used a database, I've used locked files, I've used plain unlocked files open for read/write and used seek, and I've echoed new numbers to files.
What is "best" purely depends on what you want, and what your wishes are. All solutions are simple, requiring just a few lines of code. | [reply] |
Re: best way to keep a simple count?
by mattr (Curate) on Apr 11, 2005 at 14:36 UTC
|
I second the recommendation of File::CounterFile, it just works. I've used it a number of times in the past.
Matt | [reply] |
Re: best way to keep a simple count?
by salva (Canon) on Apr 11, 2005 at 14:47 UTC
|
use a Config::* module that allows writting:
#!/usr/bin/perl
use warnings;
use strict;
use Config::Properties::Simple;
my $cfg=Config::Properties::Simple->new(scope=>'user', optional=>1);
my $count=$cfg->getProperty(counter => 0);
print "counter: $count\n";
$cfg->setProperty(counter => ++$count);
$cfg->save;
You should also use some lock to ensure that only one process reads and increments the counter. | [reply] [d/l] |
Re: best way to keep a simple count?
by davidrw (Prior) on Apr 11, 2005 at 15:55 UTC
|
yet another way (not necessarily the best) would be Cache::FileCache (useful for many other things such as caching database query results). One downside (or upside) is you don't (readily--i think it throws it under /tmp/ somewhere) have the filename, so can't do "cat counter.txt" to check it.
use Cache::FileCache;
my $cache = new Cache::FileCache( { namespace=>"my_program", auto_pu
+rge_on_get=>1 } );
my $cacheKey = 'ct';
my $value = $cache->get( $cacheKey ) || 0;
$value++;
$cache->set( $cacheKey, $value, $EXPIRES_NEVER );
| [reply] [d/l] |
Re: best way to keep a simple count?
by Anonymous Monk on Apr 11, 2005 at 13:46 UTC
|
echo $((`cat counter` + 1)) > counter
| [reply] [d/l] |
|
| [reply] [d/l] |
|
I never claimed it was. And it wasn't the first suggestion in this thread that might fail if multiple instances of the program are running at the same time. (Well, technically, having multiple instances of the program running at the same time isn't the problem - the problem is them executing the one line of code at the same time).
Code Pendants question was vague. He just wanted to know what other people used. He didn't state anything specific about his situation. Other than the implicite suggestion the program is started once a day (so, no multiple occurences).
Here's another one-liner you can list faults for:
perl -pi -le '$_++' counter
| [reply] [d/l] |
Re: best way to keep a simple count?
by Spidy (Chaplain) on Apr 12, 2005 at 20:36 UTC
|
Personally, I would just use dbmopen(); because I like it, and it's easy enough.
| [reply] |