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

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-19 14:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found