My problem with smartmatch is that it tries to replace several more explicit constructs. I prefer to just use the more explicit one, leaving less to heuristics. grep would be fine except for one problem; it does an exhaustive search. There's no (sane) way to tell it to stop searching after the first element meets the search criteria. I don't know if smartmatch stops after the first satisfying condition.
List::Util is a core Perl module. We should consider the functions it offers to be almost as much "first class citizens" as grep. ...except that for documentation we have to look at the module's POD rather than perldoc -f. One of its functions is any. This function stops searching immediately, and returns a true value as soon as one item meets the search criteria. And its name conveys exactly what it does; it's not searching for a whole bunch of things (like grep), it's just going to tell us whether any of the elements meet the criteria.
my $found = any { $_ eq 'something' } @array_of_strings; my $found = any { /\bsomething\b/ } @array_of_strings; my $found = any { $_ == 42 } @array_of_numbers; my $found = any { $_->isa('obj_type') } @array; my $found = any { $_->can('tupitar') } @array;
It's not as succinct as $something ~~ @array, but I can look at it and immediately know the semantics under which $something is being tested, whereas with smartmatch I have to open up perlop and check that my intuition isn't playing tricks on me.
Dave
In reply to Re: Smartmatch alternatives
by davido
in thread Smartmatch alternatives
by cavac
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |