Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by xdg (Monsignor)
on Jul 31, 2006 at 18:18 UTC ( [id://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.

Replies are listed 'Best First'.
Re^2: Secret Perl Operators: the boolean list squash operator, x!!
by QM (Parson) 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

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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-19 05:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found