Ok, yet another meditation that would be better fit for p6l, but I like to see it discussed here and possibly reported there by someone who's actively following the list or dismissed altogether.

I was reviewing the stuff I have inadvertently accumulated in my temp dir and I found a tiny script, most probably originating from some discussion here, that featured the following lines of code:

my $fileno=fileno $fh; print defined $fileno ? $fileno : 'undef';

Thinking of how often one does this kind of things, he/she can't appreciate more the introduction of the C<//> operator, which will soon be in 5 official realms too: not only would it allow to write the second line as

print $fileno // 'undef';

but it would remove the need to create an ad hoc temporary variable for the sole purpose of not having to run the same code, i.e. fileno $fh twice. (In other examples one may need it anyway, and this particular one is somewhat artificial, but it exposes the point and many real world situations are precisely like this.) Thus the whole two lines may be rephrased like:

print fileno($fh) // 'undef';

Clearer, ain't it?

Now, the point is that the original snippet is just a particular case of more general situation in which one has a construct like

print func($thing) ? $thing : 'undef';

where func() is not necessarily a single function or sub but possibly a complex expression involving $thing: granted, by far the most common situation is with defined, but there are other ones and in this case, here's my humble proposal - a shortcutting ternary operator that will accept on the lhs an item, and on the right code and another item; will pass the lh term to the code and it will return either the lht or the rht depending on whether the code returns a true or false value respectively. I have no idea as for the symbol, but perhaps C<|||> would do, reasoning on the base that:

Thus one may have e.g.

say %foo<thing> ||| { /foo/ and .foo('bar') }, 'defoolt';

If all this is possible and does make sense, then probably a C<&&&> should be introduced for symmetry and consistency. I don't see a particular need for low precedency equivalents nor can imagine how could they be called, but I'm open to any suggestion and comment.

I suppose that if this doesn't meet wide popular consent, someone may suggest me that I could write the code to do so myself, in Perl 6: however while I know very well that operators are nothing more than subs in it and one can easily create new ones, I'm not really sure that one can create shortcutting ones as well. Perhaps in terms of macros, ain't it?


In reply to [Perl 6] Generalized shortcutting C<||>? by blazar

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.