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

Storable

From:
Kees Jan Hermans
Date:
April 12, 2007 00:54
Subject:
Storable
Message ID:
1176319890.3785.10.camel@cookie
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);
    close(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 ?

Sincerely,

KJ Hermans




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About