in reply to Re^2: Proper way of passing around tie'd variable
in thread Proper way of passing around tie'd variable
I'm guessing that the code you posted isn't the code you're using. To get it to run, I had to make some changes. Here's what I have:
use Data::Dumper; use MLDBM qw( DB_File ); use Fcntl qw( :DEFAULT :flock ); sub hash_init2 { #### DBM Configuration ####################### my $dbm_file = "file.db"; my %hash; local *DBM; my $db = tie %hash, 'MLDBM', $dbm_file, O_CREAT | O_RDWR, 0644 or die "Couldn't not tie to $dbm_file: $!"; my $fd = $db->fd; open DBM, "+<&=$fd" or die "Could not dup DBM for lock : $!"; flock DBM, LOCK_EX; undef $db; return \%hash; } my $h = hash_init2(); print Dumper $h;
I notice that things work as I expect if I throw out all the stuff to do with file locking (everything between the tie line and the return line). My advice is to pick another file to lock (say, "file.db.lock"), and do not much with your "file.db" directly at all. Let MLDBM own that. There's then no reason to catch the value, $db, and no reason to undef it (which might be another problem).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Proper way of passing around tie'd variable
by graffitici (Novice) on Jan 07, 2008 at 18:04 UTC | |
by kyle (Abbot) on Jan 07, 2008 at 18:20 UTC |