Implement next() and delete()
[perl-indexdata-utils.git] / lib / IndexData / Utils / PersistentCounter.pm
index 95e18bc..3f2ae66 100644 (file)
@@ -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;
 }