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?


In reply to Re^2: Proper way of passing around tie'd variable by graffitici
in thread 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.