Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re^2: (Mis)Understanding grep...eq $_

by ysth (Canon)
on Feb 24, 2009 at 07:21 UTC ( #745922=note: print w/replies, xml ) Need Help??

in reply to Re: (Mis)Understanding <c>grep...eq $_<c>
in thread (Mis)Understanding grep...eq $_

It makes no sense to use it on scalars.
Hey, that's not such a bad idea. For conditional inclusion in lists, instead of something like:
@foo = ( ... ( $include_bar ? 'bar' : () ) ... );
you'd have
@foo = ( ... grep( $include_bar, 'bar' ) ... );

Replies are listed 'Best First'.
Re^3: (Mis)Understanding grep...eq $_ (versus ex-bang-bang)
by grinder (Bishop) on Feb 24, 2009 at 07:50 UTC

    Ooh, nice! That's eminently readable. Much more likely to survive a code review than aristotle's ex-bang-bang operator.

    Rather than

    my @part = ( '', ( 'admin' ) x!! $is_admin_link, ( $subsite ) x!! defined $subsite, $mode, ( $id ) x!! defined $id, ( $submode ) x!! defined $submode, );

    we would now have

    my @part = ( '', grep( $is_admin_link. 'admin' ), grep( defined($subsite), $subsite ), $mode, grep( defined($id), $id ), grep( defined($submode), $submode ), );

    I like it.

    • another intruder with the mooring in the heart of the Perl

      What is the difference?


        I dare say the ex-bang-bang approach is faster, but then I would be rightly accused of premature optimisation. The grep approach, while slower (but at a cost approaching zero) is more easily figured out from first principles.

        The effect of x!! is not easily discerned; one has to write a harness to explore its behaviour. Either because one has encountered a similar idiom in a different language and wants to see if Perl matches expectations based on prior experience, or else they have not the faintest idea of what it does (and once one sees what it does, can they determine why?)

        So in summary, there is a difference. One is tricksy, the other is merely degenerate.

        • another intruder with the mooring in the heart of the Perl

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://745922]
[LanX]: yeah too far for someone who can't even make it to Amsterdam
LanX looking for a good magit tutorial
LanX ... and git in general
[erix]: I've been to Amsterdam before you were born
[LanX]: 1899?
[chacham]: Just paint the white house black again.
dbander snorts
LanX Gesundheit! :)
talexb wonders about dividing developers in those born before the Unix timestamp of zero .. and after. Spoiler: I'm before.
chacham remebers I am a mad scientist

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (13)
As of 2017-08-18 13:22 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (302 votes). Check out past polls.