"If exists is to return a reference, it should always return a reference."

I don't know about that. Let's say we're looking at exists($foo{'bar'}). So, if $foo{'bar'} exists, exists returns a reference to it. Otherwise, it returns undef or an empty string, or 0 or whatever the hell. Something that's false in a boolean context. One potential problem, as you point out, is if someone tries to dereference the result without checking to see if it's true first. (I'll assume for the time being that it's out of the question to change the way references evaluate in boolean context :o)

I would say that checking for the existance of the value returned by exists is pretty natural. For instance the example put forward by BrowserUK:

use strict 'refs'; print ${exists $foo{'bar'}};

would yield Can't use an undefined value as a SCALAR reference or something of the like. This would be an example of bad coding: using a value without being sure that there is a value. It's really no different from: The same mistake is made (and the same error potentially generated) as if the following were written:

use strict 'refs'; print ${$foo{'bar'}};

This would yield the same error. Whereas one 'right' way to do the same thing would be:
One 'right' way to use the reference from the proposed exists would be:

use strict 'refs'; print ${exists $foo{'bar'} || \"" };

...perhaps substituting a reference to a default value or somesuch for the \"". What I'm saying is that this puts responsibility on the programmer to make sure she doesn't try to dereference anything undefined or whatever. If she wants to be able to do stuff like print ${exists $foo{'bar'}} she's not going to use strict. For the rest of us, we get added functionality for exists that doesn't break existing code.

Update: The crux, in my mind, of why print ${exists $foo{'bar'}} doesn't make sense just finally came to me in actual words: it's like writing print ( if ($foo{'bar'}) ), or print ( $foo > $bar ). It's a boolean test, which is not designed to stand alone. Conceptually, exists exists to choose between two options.

Update: Aristotle pointed out some ambiguity in the way I phrased some stuff. Also, I noticed that in my first paragraph I had written exists $foo instead of exists $foo{'bar'}.


LAI
:eof

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