From: Mike Taylor Date: Tue, 9 Dec 2014 12:17:51 +0000 (+0000) Subject: Implement next() and delete() X-Git-Tag: v0.02~17 X-Git-Url: http://sru.miketaylor.org.uk/cgi-bin?a=commitdiff_plain;h=63eac4bb35205a2b9d29c99c7c15bf3b4278e02b;p=perl-indexdata-utils.git Implement next() and delete() --- 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; }