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.

In reply to Re^2: Smart match in p5 by Juerd
in thread Smart match in p5 by xmath

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.