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


In reply to Re: Writing a perl based web poll. by Tanalis
in thread Writing a perl based web poll. by DigitalKitty

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.