The defined-or operator, available since perl5.10, is an example of shortcut behaviour triggered on definedness: If the left hand side is defined, leave it at that, otherwise evaluate the right hand side.

Shortcut behaviour in general can help to make code more expressive, in that it combines control flow and expression evaluation in a very intuitive way. Without it, more ifs and elses and variables to store intermediate results would clutter code that has to deal with conditional evaluations.

Defined-or, therefore, demonstrates a useful concept. It could be taken much further, though, with a little bit of generalization. In light of that, we might also give the choice of its symbol a second thought.

I suggest a new family of logical shortcut operators looking at definedness.

I'll briefly explain syntax and semantics of each of those.

A :|| B
A dor B
(Defined-or) Evaluate A. If the result is defined, return it. Otherwise, evaluate B and return its result. :|| has the same precedence as || and dor has the same precedence as or. There is also an assignment operator variant :||= in analogy to ||=.
A :&& B
A dand B
(Defined-and) Evaluate A. If the result is undef, return undef. Otherwise, evaluate B and return its result. :&& has the same precedence as && and dand has the same precedence as and. There is also an assignment operator variant :&&= in analogy to &&=.
A :-> B
(Defined-dereference) Evaluate A. If the result is undef, return undef. Otherwise, evaluate B and return what would have been returned if the symbol was an ordinary arrow. B can be a method call as well as an arrayref index or hashref index or a coderef parameter list. There is no assignment variant like there is none with a normal arrow.

As to the symbol for defined-or, I consider the current choice somewhat unfortunate in that slash and double-slash already had enough other meanings and don't easily lead to generalizations either. It could stay in the language like other redundancies but gradually fade into deprecation.

Note that defined-dereference is a bit less trivial to implement than it might seem at first glance, since there is no other operator yet with shortcut behaviour whose shortcut-condition only has to be adapted. I included it anyway as I consider it useful in similar situations as defined-and, where undef may indicate some sort of failure we want to take care of early in an expression. I sometimes wished to have such an operator to chain method calls returning their object on success.

Clearly, there are situations where traditional "and" and "defined-and" would amount to the same effect. Being able to distinguish definedness from boolean truth, however, has its applications, or nobody would have cared to implement defined-or. I am now suggesting to act on that insight a bit more consequently.

I may even have a go at the patch myself if the perl5 porters gang neither veto the idea nor want to take it up from here. It may take me a while though, as I'd hate to make a blunder of my first patch that is more than a bugfix.

Fellow monks, what is your opinion on (a) the concept, (b) the proposed syntax?

Update:Put RFC: in the title.


In reply to RFC: Perl5 Language Extension: Definedness-Triggered Shortcut Operators by martin

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.