and if I was more careful about analyzing error output I would have seen that. Damn
tye, you rock.
So fd_retrieve doesn't work on an empty file, it only works on storable data. I would think it should return an empty hashref - but hey, it doesn't and I can deal with that now that I know it.
The working revised code:
#!/usr/bin/perl -w
# start with no existing giant.db file
use strict;
use Fcntl qw(:DEFAULT :flock);
use Storable qw(store_fd fd_retrieve retrieve);
use Data::Dumper;
$Data::Dumper::Deepcopy=1;
$Data::Dumper::Purity=1;
$Data::Dumper::Sortkeys=1;
my %data = ( 1 => 'FEE', 2 => 'FYE',
3 => 'FOH', 4 => 'FUM', );
my $exists = (-e "giant.db"?1:0);
sysopen(*DB, "giant.db", O_RDWR|O_CREAT, 0666) or die("sysopen: $!\n")
+;
flock(*DB, LOCK_EX) or die("flock: $!\n");
my $hashref = $exists?fd_retrieve(*DB):{};
# modify the data, do work, etc..
$hashref->{$_} = $data{$_} for sort keys %data;
store_fd($hashref, *DB) or die("store_fd: $!\n");
truncate(*DB, tell(*DB));
close(*DB);
and you can check the resultant giant.db file on the command line with:
perl -MStorable -MData::Dumper -e '{print Dumper(retrieve("giant.db"))
+}'
Thanks!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.