This is my first post to this web site, and I'm very excited to be here. I'm hoping that my problem will be so elusive that it will take weeks even for the experts to solve!
I use the MLDBM format to store some data in a file. I am writing a web application using Catalyst that needs to read and modify this data, so I started out by writing a proper Model class. In the class, I have a few lines of code that initializes the tied hash. It looks like this :
sub hash_init { 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; }
I call this function in my other subroutines that manipulate the hash, deleting or adding a items. For instance, my deleteItem subroutine looks like this:
my %hash = hash_init(); print Dumper(%hash); delete( $hash{$del_id} ); print Dumper(%hash); untie (%hash);
In the above code, I can see that the item has been deleted between the two Dumper calls. And yet when I list the content at a later time, the entry is still there! When I just copy the same initialization code in the delete subroutine, everything works perfectly. But I don't want to keep copying the same lines over and over again, in case I want to change them later. Plus I want to find out how to best deal with these "tie" beasts.
I suspect the problem has to do with the reference count on the hash. I figured the changes weren't being committed to the file because there were some other variables pointing there that weren't destroyed. I am not sure whether the main %hash reference in hash_init() is still existent after the subroutine returns. However, I couldn't find a way to solve the problem. I tried just returning a pointer to the hash from hash_init() instead of the actual variable, but I couldn't get that to work either.
Any ideas?
In reply to Proper way of passing around tie'd variable by graffitici
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |