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.
PS The default block { } around ... isn't needed, but it's good documentation.