Sorry. I meant those suggestions to be pretty much straighforward
modifications or additions to your code, not anything entirely
different.
use Fcntl qw/:DEFAULT :flock/;
sub counter {
my $file = shift;
sysopen(COUNTER, $file, O_RDWR|O_CREAT) || warn $! && return;
flock(COUNTER,LOCK_EX) || warn $! && return;
my $count = <COUNTER> || 0;
seek(COUNTER,0,0) || warn $! && return;
truncate(COUNTER,0) || warn $! && return;
print(COUNTER $count + 1, "\n") || warn $! && return;
close(COUNTER) || warn $! && return;
return $count + 1;
}
|