At the risk of belaboring the points, here's v0.02:
sub getNewCounter # --------------------------------------------------------------- # Reads and incremements the counter value. Implementation based # on wisdom gained from perlmonks.org and the Perl Cookbook. # --------------------------------------------------------------- { my( $datafile ) = @_; my( $lockfile ); # Determine the name of the lock file $_ = $datafile; s/\.dat/\.lck/; $lockfile = $_; # The idea here is to wait up to ten seconds to gain access to # the lock file. If that fails, well, try again later. my( $retries ) = 10; open( LOCK, "$lockfile" ) or die "Cannot open lock file for readin +g. $!"; while ( $retries ) { if ( flock( LOCK, LOCK_EX | LOCK_NB ) ) # if Fcntl isn't availa +ble (it should be), try 2 | 4. { last; } else { $retries--; sleep( 1 ) ; } } # How did we get out of that loop? If we failed to flock, then fle +e unless ( $retries ) { die "Cannot lock counter for reading. $!"; } # Okay, we're assuming here... open( FILE, "+< $datafile" ) or die "Can't open data file for readi +ng. $!"; my( $countval ) = <FILE>; seek( FILE, 0, 0 ) or die "Can't reset data file for writ +ing. $!"; truncate( FILE, 0 ) or die "Can't clear data file for writ +ing. $!"; print FILE ++$countval; close( FILE ) or die "Cannot close counter after wri +ting. $!"; close( LOCK ) or die "Cannot close lock file. $!"; return( $countval ); }

Yes, it assumes that the sentinel file already exists.

Better? (I ask because I may not be able to get the SecuroTroopers to install File::CounterFile.)

--f

P.S. Thanks to jptxs for picking up on the other question. Also, tilly's newest sub looks interesting and that will go into 0.03, should anything else?

Update: Thanks, tilly for your extreme patience in this. Point by Point:

  1. .dat is fine for this script, but good advice for future ones.
  2. Added LOCK_NB, per advice; see above.
  3. I was under the impression that one second was the finest granularity available for sleep()?
  4. You mean, like the 0.01 did?
  5. *blush*
  6. /me hies himself to a Monastery library...

Update #2: Quickly fixed the || typo and the magic numbers before running off to write v0.03.


In reply to Re: Flock Feedback by footpad
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.