Hi.

I was just fiddling around with some Perl and DBM, experimenting a bit, when I came across an interesting question. I was writing a subroutine which connects to different largish DBM files depending on the arguments passed. This sub may need to be called several times before the script is through, and may connect to a number of different DBM files, but may also connect to some repeatedly. I am somewhat concerned about performance, so I concocted two possible approaches for this subroutine... however I am not too familiar with DBM performance issues, so there may be a better way than what I'm thinking.

Option A: I keep refs to the tied hashes in a package master hash as I tie() them. Whenever I invoke the subroutine in question, I check that master hash to see if I've already tied to that DBM file. If so, I use the existing ref; if not, I create a new ref and stick it in the hash for next time. Then, I just untie() everything at the end. This approach is best if the tie() function causes a performance hit, it minimizes the number of times it must be called.

Option B: I tie-on-the-fly, simply tying the hash at the beginning of the subroutine, and untie() at the end. This approach assumes that tie() does not cause a significant performance hit, but keeping the ties hashes in memory does.

I know that there's not always a best answer for these things, but I'd like to tap anybody's experience who has some to spare.

As a related question, must one explicitly untie() a hash, or will garbage collection take care of that? I'm sure it's good form to untie(), so I will... I'm just curious.

Thanks,

Hot Pastrami

In reply to Permance regarding tying hashes to DBM by Hot Pastrami

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.