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

Re: Smartmatch alternatives

by dcmertens (Scribe)
on Dec 17, 2013 at 13:43 UTC ( [id://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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 22:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found