Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: (Mis)Understanding grep...eq $_ (versus ex-bang-bang)

by grinder (Bishop)
on Feb 24, 2009 at 07:50 UTC ( #745926=note: print w/ replies, xml ) Need Help??


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

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 = ( 'http://example.net/app', ( 'admin' ) x!! $is_admin_link, ( $subsite ) x!! defined $subsite, $mode, ( $id ) x!! defined $id, ( $submode ) x!! defined $submode, );

we would now have

my @part = ( 'http://example.net/app', 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


Comment on Re^3: (Mis)Understanding grep...eq $_ (versus ex-bang-bang)
Select or Download Code
Re^4: (Mis)Understanding grep...eq $_ (versus ex-bang-bang)
by parv (Priest) on Feb 24, 2009 at 08:08 UTC
    What is the difference?

      None.

      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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2014-10-22 07:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (114 votes), past polls