Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Secret Perl Operators: the boolean list squash operator, x!!

by radiantmatrix (Parson)
on Aug 02, 2006 at 14:01 UTC ( #565228=note: print w/replies, xml ) Need Help??


in reply to Secret Perl Operators: the boolean list squash operator, x!!

Your x!! approach is nifty, but it's a little hard to read: the double-negation is confusing unless explained, and the x operator seems to be out of place in this context. I have always found one of these easier to read:

my $uri; foreach ( 'http://example.net/app', $is_admin_link && 'admin', $subsite, $mode, $id, $submode, ) { $uri.="$_/" if ( defined $_ && length($_) ) #omit undef and empties }

If you need to preserve the parts seperately:

my @part = ( 'http://example.net/app', $is_admin_link && 'admin', $subsite, $mode, $id, $submode, ); my $uri; foreach (@parts) { $uri.="$_/" if ( defined $_ && length($_) ) #omit undef and empties }

I prefer these because:

  1. The logic for inclusion of parts is clear
  2. It makes it easy to apply a common rule to all components (e.g. URL-ify components before concatenation)
  3. It avoids repetition of code
<radiant.matrix>
A collection of thoughts and links from the minds of geeks
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet

Replies are listed 'Best First'.
Re^2: Secret Perl Operators: the boolean list squash operator, x!!
by Aristotle (Chancellor) on Aug 02, 2006 at 14:06 UTC

    This is basically ioannisresponse, with most repetition removed. All of the points I made in the last paragraph of my response to him apply equally to your suggestion.

    Makeshifts last the longest.

      Actually, most of your objections don't seem to apply.

      1. I already include the slashes through use of $uri.="$_/"
      2. The $is_admin_link && 'admin' is in the right order already

      As for the 0/1 problem with $is_admin_link, that's a fault of my assumption that $is_admin_link would either be true or undefined. I agree, that's a bad assumption, but it's a simple fix. I would probably use a single ternary:

      $is_admin_link ? 'admin' : undef
      <radiant.matrix>
      A collection of thoughts and links from the minds of geeks
      The Code that can be seen is not the true Code
      I haven't found a problem yet that can't be solved by a well-placed trebuchet

        Read closer. I said “the last paragraph.” Here is said paragraph, in order to avoid further confusion.

        Not to mention that all of this misses my point because it’s specific to join, and I’ve needed to pass a variable list to some function or method often enough where really needed the sub-list to be empty depending on the condition – winging it by combining && and the false evaluates to the empty string factoid wouldn’t have done me any good.

        Try to simplify a different example to get closer to my point.

        Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://565228]
help
Chatterbox?
[CPUlidster]: 20yrs since I visited.
LanX is sure Louvre is going to move to Glasgow after brexit
[ambrus]: marto: http://www. irregularwebcomic. net/3498.html and http://www. irregularwebcomic. net/3549.html
[ambrus]: the annotation mentions Brisbane as even worse, and I add Debrecen
[marto]: :)
[ambrus]: robby: dunno, I would like to visit London some time, although there are other places I'd like to visit more
[marto]: "Okay, sure, there are in fact museums and art galleries and other culturally enlightened things to do in Glasgow, but that wouldn't have made a funny comic"

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2017-12-15 11:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (431 votes). Check out past polls.

    Notices?