X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=lib%2FIndexData%2FUtils%2FPersistentCounter.pm;fp=lib%2FIndexData%2FUtils%2FPersistentCounter.pm;h=3f2ae663f6b5d4fa225aa6f6fdb30ee83be0ff22;hb=63eac4bb35205a2b9d29c99c7c15bf3b4278e02b;hp=95e18bc01ebb0972d58f19d43f5217bdaf4fc654;hpb=c67610c032f288379dfc8e4365b59c9e9d1f3bb5;p=perl-indexdata-utils.git diff --git a/lib/IndexData/Utils/PersistentCounter.pm b/lib/IndexData/Utils/PersistentCounter.pm index 95e18bc..3f2ae66 100644 --- a/lib/IndexData/Utils/PersistentCounter.pm +++ b/lib/IndexData/Utils/PersistentCounter.pm @@ -30,11 +30,23 @@ sub new { sub next { my $this = shift(); + + my $fh = new IO::File('+<' . $this->{file}) || return undef; + flock($fh, 2) || die "can't lock file"; + my $n = <$fh>; + $fh->seek(0, 0); + sleep(int(rand(2))); + $fh->print($n+1, "\n"); + $fh->close() or return undef; + return $n+0; } sub delete { my $this = shift(); + + unlink $this->{file} or return 0; + return 1; }