Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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

by xdg (Monsignor)
on Jul 31, 2006 at 18:18 UTC ( #564833=note: print w/ replies, xml ) Need Help??


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

Totally ++. This is both brilliant and scary.

However, it's so unusual that I think it would be hard to follow -- more so than many other idioms. I think I'd put a space into it rather than treat it as an "operator":

( 'admin' ) x !!$is_admin_link,

I think that will be more intelligible to those who aren't familiar with the idiom.

Also, because of the way this works for arrays, you might want to call this the "()x!!" operator -- though a pithy visual metaphor escapes me. Consider:

use strict; use warnings; my $cond = 0; my @foo = 'foo'; my @bar = ( 'bar', @foo x !!$cond, 'baz' ); print join(q{,},@bar), "\n"; @bar = ( 'bar', (@foo) x !!$cond, 'baz' ); print join(q{,},@bar), "\n";

Result:

bar,,baz bar,baz

Teaching people to always use the () may save them from expected results.

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.


Comment on Re: Secret Perl Operators: the boolean list squash operator, x!!
Select or Download Code
Re^2: Secret Perl Operators: the boolean list squash operator, x!!
by Aristotle (Chancellor) on Jul 31, 2006 at 18:35 UTC

    I think that will be more intelligible to those who arenít familiar with the idiom.

    I donít know. Shouldnít that mean it should be written with one more space, x ! !? I think the hard-to-grasp part is actually why/how () x $boolean works; the addition of !! doesnít make it appreciably more difficult to understand, in my opinion.

    Makeshifts last the longest.

      Because !! is a (moderately known) idiom as well (the "boolification" operator), I don't think that needs to be split up. My snap reaction to seeing ('foo') x!! $bar is a "huh?" until my brain parses x!! into two separate things. My snap reaction to ('foo') x !!$bar is immediately "list repeated boolean times".

      Separately, I was thinking of parallels to "boolification" for a name, but it's not true or false -- it's a "there or not there" operator. That made me think first of the Cheshire Cat fading in and out, and the cat idea reminded me of Schrodinger's Cat and other quantum mechanical metaphors. Maybe there's a good name in there somewhere.

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re^2: Secret Perl Operators: the boolean list squash operator, x!!
by QM (Vicar) on Aug 01, 2006 at 01:28 UTC
    Also, because of the way this works for arrays, you might want to call this the "()x!!" operator -- though a pithy visual metaphor escapes me.
    How about the "Enterprise" operator, for it's resemblence to NC1701?
    ()x!!
    Oh, well, it looks better in italics anyway...

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      There it is: proof by its very nature that Perl is an Enterprise-level language!

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (11)
As of 2014-08-22 15:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (159 votes), past polls