Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
The nicer approach with a ternary:

my @part = ( 'http://example.net/app', ( 'admin' ? $is_admin_link : () ), ( defined $subsite ? $subsite : () ), $mode, ( defined $id ? $id : () ), ( defined $submode ? $submode : () ), ); my $uri = join '/', @part;

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:

my @part = map defined $_->[-1] ? $_->[0] : (), ['http://example.net/app' => 1], ['admin' => $is_admin_link], [$subsite], [$mode => 1], [$id], [$submode];

Here:

  1. the additional visual "encapsulation" given by square parens allowed me to put several params on one line with no risk of confusing them;
  2. I forced a => 1 on elements that you were originally unconditionally inserting: since these were supposedly true, it's completely useless but possibly as a visual reminder that they are, and in turn this is silly for the constant item but may be sensible for $mode.

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:

my @part = map +(ref) ? (defined $_->[-1] ? $_->[0] : ()) : $_, 'http://example.net/app', ['admin' => $is_admin_link], [$subsite], $mode, [$id], [$submode];

Here, the first line is getting long and confusing, and the absence of square parens around $mode also degrades readability.

--
If you can't understand the incipit, then please check the IPB Campaign.

In reply to Re: Secret Perl Operators: the boolean list squash operator, x!! by blazar
in thread Secret Perl Operators: the boolean list squash operator, x!! by Aristotle

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!
  • 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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (7)
    As of 2015-07-06 08:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (70 votes), past polls