Unless performance is a concern, go with what's most readable.
For a lone string test, use lc() and eq. But if you've already used a couple of regex's to test $foo, it may be more readable to throw in another one, rather than switching gears and using lc() and string compare.
Consider adding a test below this fragment:
Now which reads better if ( $foo =~ /^$bar$/i ) { ... } or if ( lc($foo) eq lc($bar) ) { ... } I'll argue that it's at best a toss-up. As a reader of fragments like this, I see the pattern of a variable being tested against a sequence of regex's, and my brian goes into regex scanning mode. Mixing in a string comparision might be technically correct, but it breaks up the reading flow, at least in examples like this.next if $foo =~ /^#/; if ( $foo =~ /^(?:this|that|or|something|else)$/ { ... }
</counteropinion>
In reply to (dws)Re: Regexes Are Not For String Comparison
by dws
in thread Regexes Are Not For String Comparison
by japhy
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |