Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: best way to keep a simple count?

by monkfan (Curate)
on Apr 11, 2005 at 12:44 UTC ( [id://446582]=note: print w/replies, xml ) Need Help??


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>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://446582]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-26 07:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found