Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

petdance (2468)

petdance
  andy@petdance.com
http://www.perlbuzz.com/
AOL IM: petdance (Add Buddy, Send Message)
Yahoo! ID: petdance (Add User, Send Message)
Jabber: petdance@gmail.com

I'm Andy Lester, and I like to test stuff. I also write for the Perl Journal, and do tech edits on books. Sometimes I write code, too.

Journal of petdance (2468)

Tuesday February 22, 2005
11:12 AM

Not everything is a regex

[ #23306 ]
We love the regexes in Perl, but they don't always make sense. If you're doing

if ( $str =~ /^foo$/i )

then write it as

if ( lc $str eq "foo" )

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • My pet peeve is the opposite: people who use a string as the first argument to split(). Not only is it intended to be a regex, but some strings have special behaviour (" " for instance).

    -Dom

  • Actually that's lc $str eq "foo" or lc $str eq "foo\n".
    • Except that that distinction is irrelevant in this case. It's just trying to match a value coming out of a hash. But point taken.
      --

      --
      xoa

  • Is there a fast way to force a variable within regex tags to not follow regex escapes?
    $filesearch = "Financials [1994]";

    foreach ... {
      ...
      if ( $file =~ m/$filesearch/ ) ...
    I'm currently doing this first:
    $filesearch =~ s/([][])/\\$1/g;
    $filesearch =~ s/([()])/\\$1/g;
    But it seems so messy.
  • When the string is in $_, I'll often use a regex for a string comparison so I can avoid spelling out the variable. This is particularly often the case when grep is involved.

    It still annoys me though.

    And abuse of regexen for string comparisons tends to crop up a lot in badly written scripts by people who don't really know Perl well (such as most of the stuff that circulates on “free CGI scripts!” sites.)

  • The trouble is that this type of advice is incorrect in the modern unicode world.

    I think youll find that the equivelency you post doesnt hold for certain unicode characters like the greek sigma chars. Lowercase/Uppercase arent safe forms to do case insensitive comparisons in unicode instead they have a special form called "foldcased" which is intended for this purpose and involves prenormalizing the string.

    Anyway, sorry this is with my pedant hat on.