Monks, etc, please help: I am using version 0.611 of the Storable module on a win32 platform. This is the most current version that ActiveState's PPM installation will give me, though a much newer version is available on CPAN. I have a need to use file locking when storing data structures. The latest version of Storable on CPAN (which I am having problems installing, but that aside for now, please) implements a lock_store and lock_retrieve function. But for now, I have tried writing my own storable file lockin wrapper around the old version, and am having problems, in essence, using store_fd instead of store.

I have code written that uses the 'store' function, which accepts a filename to save as. However, if I change it to use store_fd, which accepts a file handle/descriptor instead, I would think I could use flock to do the locking for me.

This is not a question on how to use flock (it fails whether I lock or not), but rather, why isn't store_fd working for me when I use sysopen to get a file handle? It doesn't update the storable file. If I get the file description with perl's 'open' for writing instead, it will update the file, but does not write in a format that retrieve is happy with. I will post my code. Any ideas?? Hopefully I am just making some simple mistake. Thanks!
# # lock a file handle # # arg 1: reference to file handle # # call like this: &lockFile(\*MYFILEHANDLE); # # locking/unlocking should be done in situations when a file handle is # opened for WRITING, in this sequence: # # open (FILE, ">some_file.txt"); # &lockFile(\*FILE); # . # . (process your info here) # . # &unlockFile(\*FILE); # close (FILE); # # # # sub lockFile { my ($fh); $fh = shift; # # win 9x and some really old perls won't support flock # this will determine if we have flock capabilities # and write to constant HAS_FLOCK # use constant HAS_FLOCK => eval { flock STDOUT, 0; 1 }; if (HAS_FLOCK) { flock ($fh, 2); } } # # unlock a filehandle # # arg 1: reference to file handle # # call like this: &unlockFile(\*MYFILEHANDLE); # sub unlockFile { my ($fh); $fh = shift; if (HAS_FLOCK) # sub lockFile already determined if 'flock' suppor +ted { flock ($fh, 8) } } # # this implements the storable module's store functionality with locki +ng # arg 1: reference to perl data object # arg 2: filename of storable data file # # the latest Storable module supports store_lock, which does this, but + it is # currently not available via ppm installation. So we will use this wr +apper # instead. The method is described in the perl cookbook, so it should +be # trustworthy. # # # and since we use our own lockFile and unlockFile functions, locking +will # gracefully NOT be used when it's not supported. # sub storeWithLocking { my ($reference, $fn); $reference = $_[0]; $fn = $_[1]; use Storable qw(store_fd); sysopen(DF, "$fn", O_RDWR|O_CREAT, 0666) or die "can't open ${fn}: $!"; &lockFile(\*DF); eval ' store_fd($reference, \*DF); '; if ($@) { print "Error in store_fd call: \"$@\"<BR>\n"; } truncate(DF, tell(DF)); &unlockFile(\*DF); close(DF); }

Edit kudra, 2002-08-05 Added a READMORE tag


In reply to File Locking using (older) Storable Module by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.