Re: Secret Perl Operators: the boolean list squash operator, x!!by blazar (Canon)
|on Jul 17, 2008 at 22:32 UTC||Need Help??|
The nicer approach with a ternary:
Only now did I get at this very nice node of yours, brought here from some other recent one, and of course it's unusual to reply to a years old node, nor am I discussing the "boolean list squash operator" which is actually a good technique. (And I think I must have used it every now and again, even if not that regularly, and... I wouldn't have thought of giving it a name.)
I'm here just to discuss, for our beloved TIMTOWTDIness sake, how I would have probably done the damned thing in a reasonably real world situation. To be precise, I'm assuming here that it's isolated enough as a task not to warrant a separate sub, and that performance is not an issue. (Even if it were, you'll see I'm playing dereferentiation tricks, but I doubt that they could become that relevant wrt other factors...)
Said all this, probably I wouldn't have thought of the boolean list squash operator. I would have tried to "encapsulate" the component data and map it through some suitable code. Of course the problem is: which code? Of course, the answer is that there are tons of ways that work, but I also want something which is visually clear at a glance, and unobtrosive. I considered some possibilities involving nested ternaries which would e.g. distinguish refs from non-refs, and then arrayrefs with two elements from ones with one. Then I considered as an alternative chained map()s achieving the same effect for clarity at the expense of multiple looping where a single one would suffice. But then I wouldn't want two or three "header" lines of code only to suck up say other six. I really want at most one, and simple enough. Eventually, the best alternative I could think of is:
As a sort of counter-example, if I wanted the unconditionally inserted elements to be included in the list without the burden of the square parens, then it's obvious how to do it:
Here, the first line is getting long and confusing, and the absence of square parens around $mode also degrades readability.