Hello,

I wrote some code that needs to open a storable file and keep a lock on it until I say to let go (because I am modifying the data while I have it open). I thought the best way to do this would be to write a routine to open and lock a filehandle, which would then pass that filehandle into the routine that would read it through storable. Unfortunately I seemed to have coded myself into a circle and can't get this to quite work right. Any ideas? Many thanks!

#!/usr/bin/perl -w use strict; use Fcntl qw(:DEFAULT :flock); use Storable qw(store_fd fd_retrieve); use Data::Dumper; $Data::Dumper::Deepcopy=1; $Data::Dumper::Purity=1; $Data::Dumper::Sortkeys=1; sub db_load { my $fh = $_[0]; return fd_retrieve($fh); } sub db_save { my ($fh, $db_hashref) = @_; store_fd($db_hashref, $fh); return $fh; } sub fileopenandlock { my ($fh, $file) = @_; sysopen($fh, $file, O_RDWR|O_CREAT, 0777) or die("I can't open the filehandle $fh for file $file in order to +lock it\n"); flock($fh, LOCK_EX) or die("I can't get an exclusive lock LOCK_EX on the file handle $f +h\n"); return $fh; } sub filecloseandunlock { my $fh = $_[0]; flock($fh, LOCK_UN) or croak("I can't unlock LOCK_UN the file handle $fh\n"); close($fh) or croak("I can't close the filehandle $fh\n"); return 1; } my %data = ( 1 => 'FEE', 2 => 'FYE', 3 => 'FOH', 4 => 'FUM', ); my $db_file = "./giant.db"; # open and read db my $db_fh = do { local *DB }; my $db = db_load(fileopenandlock($db_fh,$db_file)); print "First read dump:\n"; print Dumper($db) . "\n\n"; # should dump empty # fill it up with data $db->{$_} = $data{$_} for sort keys %data; print "Dump before write:\n"; print Dumper($db) . "\n\n"; # should dump full # save db filecloseandunlock(db_save($db_fh,$db)); # test read - did it work? my $db_fhnew = do { local *DBNEW }; my $dbnew = db_load(fileopenandlock($db_fhnew,$db_file)); print "Second read dump:\n"; print Dumper($dbnew) . "\n\n"; # should dump full filecloseandunlock($dbnew);


Thanks

In reply to Storable and passed filehandles by blahblah

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.