projects
/
perl-indexdata-utils.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
| inline |
side by side
(parent:
c67610c
)
Implement next() and delete()
author
Mike Taylor
<mike@indexdata.com>
Tue, 9 Dec 2014 12:17:51 +0000
(12:17 +0000)
committer
Mike Taylor
<mike@indexdata.com>
Tue, 9 Dec 2014 12:17:51 +0000
(12:17 +0000)
lib/IndexData/Utils/PersistentCounter.pm
patch
|
blob
|
history
diff --git
a/lib/IndexData/Utils/PersistentCounter.pm
b/lib/IndexData/Utils/PersistentCounter.pm
index
95e18bc
..
3f2ae66
100644
(file)
--- 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;
}