Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Smartmatch alternatives

by dcmertens (Beadle)
on Dec 17, 2013 at 13:43 UTC ( #1067468=note: print w/replies, xml ) Need Help??

in reply to Smartmatch alternatives

I believe the answer to your question is "no". At least, I can't think of a clean looking way using normal Perl syntax to do it, unless you are willing to use a function call. On the other hand, if you are willing to use a function call, your code becomes self-documenting. In an effort to aggravate BrowserUK, I present both a normal function and a pipelining function ref for your consideration:

use strict; use warnings; use Test::More; my @data = ('1.0', 'hello', 0); sub is_string_in { my $number_to_find = shift; return 0 < (grep { $_ eq $number_to_find } @_); } my $is_string_in = \&is_string_in; ok('hello'->$is_string_in(@data)); ok(is_string_in('hello' => @data)); ok(not 1->$is_string_in(@data)); ok(not is_string_in(1 => @data)); ok(0->$is_string_in(@data)); ok(is_string_in(0 => @data)); use Scalar::Util qw(looks_like_number); sub is_number_in { my $number_to_find = shift; return unless looks_like_number($number_to_find); return 0 < (grep { looks_like_number($_) and $_ == $number_to_find + } @_); } my $is_number_in = \&is_number_in; ok(not 'hello'->$is_number_in(@data)); ok(not is_number_in('hello', @data)); ok(1->$is_number_in(@data)); ok(is_number_in(1, @data)); ok(0->$is_number_in(@data)); ok(is_number_in(0, @data)); done_testing;

Replies are listed 'Best First'.
Re^2: Smartmatch alternatives
by oiskuu (Hermit) on Dec 17, 2013 at 17:20 UTC

    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.

      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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1067468]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2017-08-20 10:40 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (315 votes). Check out past polls.