in reply to Flock Feedback
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:
Update #2: Quickly fixed the || typo and the magic numbers before running off to write v0.03.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Flock Feedback
by chipmunk (Parson) on Feb 26, 2001 at 10:02 UTC | |
|
Re (tilly) 2: Flock Feedback
by tilly (Archbishop) on Feb 26, 2001 at 09:50 UTC | |
|
Re: Re: Flock Feedback
by dws (Chancellor) on Feb 26, 2001 at 10:17 UTC | |
|
Re: Re: Flock Feedback
by tomhukins (Curate) on Feb 26, 2001 at 15:01 UTC | |
by chipmunk (Parson) on Feb 26, 2001 at 20:04 UTC | |
by tye (Sage) on Feb 26, 2001 at 20:57 UTC |