doran has asked for the wisdom of the Perl Monks concerning the following question:
Frequently I need to check if a value matches any number of values. A common solution, cited in the camel book is to use a regex, seperating the search strings with a vertical bar:
While that's very easy, it's not as fast as using a comparison operator such as 'eq' or '=='. However using these often leads to redundant and cumbersome looking code:if ($foo=~/$bar|$baz|$boo/){ print "yep!\n"; }
or the much malined:if (($foo eq $bar) || ($foo eq $baz) || ($foo eq $boo)){ print "yep!\n"; }
Is there way of using a single comparison operation against several values, grouping them together much as you can do with a single regex statement, without the overhead of the regex?if ($foo eq $bar){ print "yep!\n"; } elsif ($foo eq $baz){ print "yep!\n"; } elsif ($foo eq $boo){ print "yep!\n"; }
|
---|