|Do you know where your variables are?|
RFC: Perl5 Language Extension: Definedness-Triggered Shortcut Operatorsby martin (Friar)
|on Mar 17, 2012 at 09:20 UTC||Need Help??|
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.
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.