From 63eac4bb35205a2b9d29c99c7c15bf3b4278e02b Mon Sep 17 00:00:00 2001 From: Mike Taylor Date: Tue, 9 Dec 2014 12:17:51 +0000 Subject: [PATCH] Implement next() and delete() --- lib/IndexData/Utils/PersistentCounter.pm | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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; } -- 1.7.10.4