A series of comments:
  1. I would not assume that the data file had a name ending in .dat. That is the kind of assumption that can change, and then people will be unhappy because their data file got wiped out.
  2. You need to set the LOCK_NB flag as well as LOCK_EX or else your first try will hang until you get the lock.
  3. In a CGI environment, waiting 1 second per retry is probably a bit much...
  4. While you have control of the file, you don't need to be cautious about truncates and seeks. Just open for read, read, close, open for write, and write as usual.
  5. Explicitly closing the data file is a good idea.
  6. However I like calling local on *FH if I will use FH as a filehandle. Just a good practice...

UPDATE
Others have already said everything that I would have to say other than the fact that my fifth point is that you actually did. I offered you code earlier where the lock went away just by going out of scope. If you use that then you still want to be explicit about closing the date file. I was just saying that it was subtle but important point.


In reply to Re (tilly) 2: Flock Feedback by tilly
in thread Flock Feedback by footpad

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.