Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

by ioannis (Priest)
on Aug 01, 2006 at 07:39 UTC ( #564940=note: print w/ replies, xml ) Need Help??


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;


Comment on Re: Secret Perl Operators: the boolean list squash operator, x!!
Download Code
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.)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://564940]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2014-08-28 10:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (259 votes), past polls