develooper Front page | perl.perl5.porters | Postings from April 2007


Kees Jan Hermans
April 12, 2007 00:54
Message ID:
Dear perl5 porter people,

What I miss in the Storable module (which I love, otherwise), is to be
able to change something about a stored reference atomically; an
extension to lock_store and lock_retrieve, something that can be found
inside Sleepycat's transactions subsystem, for example.  So I wrote a
little function that does this.  It loads a reference, calls a callback
function with it (and an argument of your choosing) and stores it again,
all while having the file descriptor locked:

sub lock_retrieve_change_store {
  my ($path, $func, $arg) = @_;
  if (open(STORE, "+< $path")) {
    flock(STORE, LOCK_EX);
    my $ref = fd_retrieve(\*STORE);
    if (!&$func($ref, $arg)) {
      close STORE;
      return undef;
    truncate(STORE, 0);
    sysseek(STORE, 0, 0);
    store_fd($ref, \*STORE);
    return $ref;
  return undef;

Does it have a place inside the Storable module, do you think ?  Or has
this been proposed a million times already, and found its way in the
other thousands of perl persistence libraries ?


KJ Hermans Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About