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 $_.
The smart match operator, in this example invisible because it's used by when internally, avoids a lot of typing. Compare the alternative:given ($address) { when (@whitelist) { $score -= 30 } when (@blacklist) { $score += 50 } when (/example\.com$/) { $score = 0 } default { ... } }
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.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; } ... }
Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }
In reply to Re^2: Smart match in p5
by Juerd
in thread Smart match in p5
by xmath
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |