What if the element doesn't exist?

In a naive implementation, returning undef, zero, or an empty string from exists will fail dereference, and it would happen at runtime. A reference value that evaluates false, yet returns undef when dereferenced, seems to be required.

That seems to need a tricky sort of special-casing that would affect all dereferences.

Update: BrowserUK points out that dereferencing the return of exists yields undef. That does not pass use strict 'refs';, however. exists currently returns the empty string as false, generating Can't use string ("") as a SCALAR ref while "strict refs" in use at -e line 1..

Update 2: To clarify my point, this idea leads to:

my %foo; my $bar = ${exists $foo{'bar'}};
giving assignment to the value without autovivification. The need to provide a reference which is false in bool context (for campatibility) is the basis for my objection.

After Compline,
Zaxo


In reply to Re: optimization - exists should return a reference by Zaxo
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.