Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Update: You appear to be updating your reply significantly without noting the updates so this reply does not yet reflect any of these updates. I may choose to update or reply again after your node content appears to have settled down. I've now quoted your entire original response as seen when I started replying to it:

OK, so my example only used single-element lists, so it was an insufficiently ornery problem to show that only the ternary and push are generically appropriate. Here’s an example to disqualify grep:
my $name = join '-', @prefix, ( defined $path and length $path ) ? ( s +plit /:/, $path ) : (), $suffix;

Not at all. Where did you get the idea that grep can't handle more than one item in a list?

To repeat myself, the following three constructs are interchangeable:

( LIST ) x!! COND ( grep COND, LIST ) COND ? ( LIST ) : ()

except that only the middle one doesn't require an extra set of parens in some rare cases and only the last one short-circuits evaluation of LIST if COND is false (and perhaps some other more subtle differences, I suspect, though none occur to me at the moment).

(Update: And the second may evaluate COND more than once.)

So, I don't see much recommending the first choice over the second (two keystrokes is hardly a worthy deciding factor) and find the less-than-typical use of grep to be less confusing than the invention of an "x!!" "operator". So I'd use grep rather than x!! if I had reason to not use the third option or even the clearer conditional use of push or some other solution (such as filtering out undefined values as already demonstrated).

So your new example can be rewritten as:

my $name = join '-', @prefix, ( grep { defined $path and length $path } split /:/, $path ), $suffix;

to demonstrate the use of the second option on a list that might contain more than one item.

Trying to rewrite that using the first option will likely cause you new problems because of your use of "and" (which I wouldn't use in a such a simple Boolean expression, since "&&" is better suited for that task). Perhaps your new idiom needs to be renamed "( ()x!!() )" to not make it error-prone? (:

And note that $path being undefined would result in a warning (if enabled) in both my grep construct and your x!! construct. So I doubt I'd use either in that particular case.

Since split on an empty string returns an empty list, for this particular case I'd likely instead use:

( split /:/, $path || '' ),

but replacing "||" with "//" (or a suitable replacement) if $path might be "0".

Update: And I find it telling that when your update added an actual use of "x!!", you went and removed your use of "and" to avoid demonstrating this weakness in your idiom. :)

- tye        

In reply to Re^3: Secret Perl Operators: the boolean list squash operator, x!! (grep disqualified?) by tye
in thread Secret Perl Operators: the boolean list squash operator, x!! by Aristotle

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (4)
    As of 2018-06-24 05:49 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (126 votes). Check out past polls.