in reply to Re: Smartmatch alternatives
in thread Smartmatch alternatives

Or, avoiding full grep:

my $contains = sub { $_ eq $_[1] && return 1 for @{$_[0]}; 0 }; for (qw( Lisa Bart Fred LanX )) { [qw( Fred Wilma Barney Betty )]->$contains($_) and print; }

Though efficiency probably shouldn't enter the discussion here. Performing a key search on array, where you could have used a hash—bad decision. Another reason to dislike the smartmatch: it promotes bad design.

Replies are listed 'Best First'.
Re^3: Smartmatch alternatives
by tobyink (Canon) on Dec 17, 2013 at 19:13 UTC

    It makes sense to use hash keys if all the items being searched through are strings. If some of them are regexps, coderefs, objects with overloading, etc., then a hash is not an option.

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name