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


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

Instead of using the x operator, we could have used the the && operator. The advantage here is that it codes simpler, and the disadvantage is it has no movable parts (since it is not an array).
no strict; no warnings; my $part = 'http://example.net/app' . ($admin && $is_admin_link) . (defined $subsite && $subsite) . $mode . (defined $id && $id) . (defined $submode && $submode) ; print $part;

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

    Where did the slashes go? That would assemble URIs such as http://example.net/appadminjoebobdoc rather than http://example.net/app/admin/joebob/doc.

    You also broke down on my ornery $is_admin_link requirements. First of all the clause is the wrong way around, it would have to be ( $is_admin_link && $admin ) – but since $is_admin_link is always 0 or 1, that would leave spurious zeroes in the output, so you need to boolify: ( !! $is_admin_link and $admin ).

    All things considered, we get this:

    my $part = ( 'http://example.net/app' . ( !! $is_admin_link && '/' . $admin ) . ( defined $subsite && '/' . $subsite ) . '/' . $mode . ( defined $id && '/' . $id ) . ( defined $submode && '/' . $submode ) );

    I definitely feel like a broken record now.

    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.

    Makeshifts last the longest.

      Indeed, the substring related to $is_admin_link should have been written to reflect both boolean states, like this:

      (($is_admin_link||'') && $admin)

      Whether this form is preferable than using the trenary operator, the issue props our individual taste.

      ( The missing slashes were already prepended on my variables -- you had no way of knowing this; which in turn reminds us of the better clarity of join(), another subject and one more issue to consider.)