Hi monks,

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.