duncs has asked for the wisdom of the Perl Monks concerning the following question:
So, I open the file to get a filehandle, lock it, then use the store_fd and retrieve_fd on the filehandle (to prevent possible race conditions between using lock_store and lock_retrieve from the other processes), but have discovered it doesn't work a I expected. Is this a problem with Storable or my understanding?
Update: Fixed thanks to zwon - was missing a truncate and seek before rewriting the file
#!/usr/bin/perl use strict; use warnings; use Storable qw(retrieve store retrieve_fd store_fd); use Fcntl qw(:flock); my $file='/tmp/storable_test'; my %data = ( item => 'Here I am', ); store(\%data, $file); #===== # open & lock file open(my $fh, '+<', $file); flock($fh, LOCK_EX); # get data, amend, put back my $data = retrieve_fd(\*$fh); print 'item: ', $data->{item},$/; delete($data->{item}); print 'gone (error as expected): ', $data->{item},$/; truncate $fh, 0; # <- *** WAS MISSING THIS *** seek $fh, 0, 0; # <- *** WAS MISSING THIS *** store_fd($data, \*$fh); close($fh); #====== # now reload from the file my $check_data = retrieve($file); print 'why is this here? ', $check_data->{item},$/;
I am guessing its probably my understanding but cannot see why currently.
Thanks
Duncs
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Storable or user bug?
by zwon (Abbot) on Oct 15, 2009 at 19:49 UTC | |
by duncs (Beadle) on Oct 16, 2009 at 08:24 UTC | |
|
Re: Storable or user bug?
by Khen1950fx (Canon) on Oct 15, 2009 at 21:18 UTC |