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 | |
by tobyink (Canon) on Dec 17, 2013 at 19:13 UTC |
In Section
Seekers of Perl Wisdom