Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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;


Comment on Re: Smartmatch alternatives
Download Code
Re^2: Smartmatch alternatives
by oiskuu (Pilgrim) 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1067468]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2014-09-30 10:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (363 votes), past polls