in reply to Comparing strings case insensitive

The first is clearer and less error prone (woe unto you if $str2 ever contains metacharacters as is), but presuming a recent perl both should be equally speedy.

Update: Wow, I must have heard wrong about the re speedups or something; I thought that they supposedly had gotten optimized so fixed strings were on the same order as a vanilla comparison but as the benchmark below shows that's not the case. So yeah, definitely option one.

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^2: Comparing strings case insensitive
by LesleyB (Friar) on Sep 12, 2008 at 21:26 UTC
    So the lower cased string comparison is more robust than the regexp?

      Yes, because lc "alligatorz" eq lc "a.*z" will properly compare as unequal, whereas "alligatorz" =~ /^a.*z$/i incorrectly (for what you're trying to do) will match. That's what I mentioned about robustness, but again with the proper application of \Q that wouldn't be an issue.

      That being said, I'd still use the lc $x eq lc $y version vice the \Q-ified regexp match as it's more explicit that you're doing a case insensitive comparison between fixed strings.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

      So the lower cased string comparison is more robust than the regexp?

      Yes.