in reply to best way to keep a simple count?

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'
Regards,
Edward

Replies are listed 'Best First'.
Re^2: best way to keep a simple count?
by brian_d_foy (Abbot) on Apr 11, 2005 at 21:27 UTC

    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>