Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Writing a perl based web poll.

by Tanalis (Curate)
on Jul 15, 2003 at 09:46 UTC ( [id://274332]=note: print w/replies, xml ) Need Help??


in reply to Writing a perl based web poll.

As has already been pointed out by Skeeve, you're not reading in the data there before you start incrementing the results.

It's also a very good idea to lock the results file before you write to it - especially with web applications - otherwise you could find that the file gets clobbered and you lose the data in it.

I've always done this using Fcntl's flock method, which can be set to only allow one process read/write access to a file at a time.

It seems most effective (from experience) if you use flock on a lock file, and lock that, rather than the results file directly before you write any data. For example:

use Fcntl qw/:flock/; open LOCK, ">", "results.dat.lck" or die; flock LOCK, LOCK_EX; open OUT, ">", "results.dat" or die; # print data close OUT; close LOCK; # releases the lockfile, lets other processes write
I'm sure this is documented somewhere in a much better way than I can explain it, and with the reasons behind the use of the external lock file rather than direct locking, but I can't find it at the minute. I'll keep looking .. *grin*

Hope that helps.

-- Foxcub
#include www.liquidfusion.org.uk

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-19 23:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found