There are two lines of though. One is that you're likely to need the value of the hash key after testing if it exists. The other is that, if you don't need it, but really just wanted to check if it exists, then you're loosing speed, not gaining it. Also, if all you really wanted was a bool, you just created and then destroyed a reference for nothing, which isn't exactly free.

The real solution would be common subexpression optimization, which is significantly more difficult.

You should benchmark exists (as it currently exists) vs. getting the value of a hash value, then consider usage patterns, and if it's worth it. Once you've got some numbers, talk to perl5-porters. Even better, once you've got some numbers and a patch...

You might also want to post to perl6-langauge about this -- since bool and ref context will be differencatable in perl6, you can get the best of both worlds. In fact, it could even return the value, but give "undef-but-true", etc, to give true results when the kv exists, but the value is false.


Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).


In reply to Re: optimization - exists should return a reference by theorbtwo
in thread optimization - exists should return a reference by John M. Dlugosz

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.