in reply to simple IP counter..
You do use flock, which is good, but if the file is opened while another instance is in the for()-loop, the counter will in effect only be increased once. To avoid this race condition, you have to write to the file without closeing it after reading. This can be done using a read+write mode.
There are also CPAN modules that do this, but I think modules are overkill for a ++.sub counter { my ($file) = @_; local *COUNTER; local $/ = undef; open COUNTER, '+<', $file or open COUNTER, '>', $file or return undef; flock COUNTER, 2; seek COUNTER, 0, 0; my $counter = <COUNTER>; seek COUNTER, 0, 0; truncate COUNTER, 0; print COUNTER ++$counter; close COUNTER; return $counter; }
- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.
|
|---|