http://www.perlmonks.org?node_id=565228


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.