Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Smart match in p5

by Juerd (Abbot)
on Mar 14, 2005 at 11:38 UTC ( [id://439341]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Re: Smart match in p5
in thread Smart match in p5

I don't get what this operator is supposed to do, or why it's better than the various operators that exist?

It's one operator to compare anything to anything. It's eq, ==, grep, exists, all in one. That's useful if you want to pass a condition to be applied to a general function. Normally, you'd supply a coderef, but now, you can just supply any value it can be smart matched to. And the great thing is, that you can still pass that coderef.

The "when" operator implicitly smart-matches against $_.

given ($address) { when (@whitelist) { $score -= 30 } when (@blacklist) { $score += 50 } when (/example\.com$/) { $score = 0 } default { ... } }
The smart match operator, in this example invisible because it's used by when internally, avoids a lot of typing. Compare the alternative:
for my $a ($address) { if (grep $_ eq $a, @whitelist) { $score -= 30; last; } if (grep $_ eq $a, @blacklist) { $score += 50; last; } if ($a =~ /example\.com$/) { $score = 0; last; } ... }
Smart match replaces grep and =~ here. But it can also replace eq and == and exists, making it a great tool for making code easier to read, especially when implied with given/when.

Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

PS The default block { } around ... isn't needed, but it's good documentation.

Replies are listed 'Best First'.
Re^3: Smart match in p5
by halley (Prior) on Mar 14, 2005 at 12:59 UTC
    I know that idiom plays a large part of understanding a language, but I feel like neither of those examples are particularly self-documenting or literate.

    Saying "Given X, when A, B, C, do T" does not roll off the tongue. It doesn't express any particular relationship between X and the list. It doesn't help me understand what the when operator does. Are we iterating? Triggering? You said "smart matching," but uh, that still leaves my uninitiated brain lost. Also, "when" connotates time or passive reaction to me, not compliance or active processing.

    "Given X, if it matches any of A, B, C, do T" is a step up. Going from a complicated concept of "does whatever you want" to a more specific concept of "performs this particular comparison" is a shift toward self-documentary or literate programming.

    As you can see, I am very dubious of the value of Perl6, and this sort of divergence from pragmatic programming to ethereal ivory-tower philosophizing just doesn't get me any closer to enjoying the changes in the language. The language should express generic problem-solving succinctly, but not so tersely it cannot be understood concretely.

    --
    [ e d @ h a l l e y . c c ]

      Let's discuss that in a separate thread. Do note that switch/case (just spelled differently in Perl) is much requested and exists in many languages. See the link to S04 to see the table of how matches operate. If you don't know what S04 is, then you should indeed read it before questioning the use of what it describes.

      Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://439341]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.