in reply to Re: Proper way of passing around tie'd variable
in thread Proper way of passing around tie'd variable
Thank you both for your replies. I had actually tried passing back a reference first, but I run into a very odd problem. So I changed my hash_init function to something like this:
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; #Dumper(%hash); return \%hash; }
And then I call it from deleteItem as follows:
sub deleteItem { my ($self, $del_id) = @_; my $hash = hash_init2(); print Dumper($hash); # check if the item actually exists in the database if ( exists($hash->{$del_id}) ) { delete( $hash->{$del_id} ); } untie($hash); }
The odd thing is, the print Dumper call in deleteItem normally doesn't print anything (so the reference points to a null hash, and the delete fails). However, when I uncomment the line Dumper(%hash) in hash_init2, then everything works. As you see, I am not even printing anything, but just reading the values.
Any ideas why this might be happening? Does one need to read from the tied hash in the same scope or something?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Proper way of passing around tie'd variable
by kyle (Abbot) on Jan 07, 2008 at 15:37 UTC | |
by graffitici (Novice) on Jan 07, 2008 at 18:04 UTC | |
by kyle (Abbot) on Jan 07, 2008 at 18:20 UTC |