[Melbourne-pm] case insensitive REs

Tim Connors tconnors at astro.swin.edu.au
Tue May 6 21:11:23 PDT 2008


G'day.

I want the user to be able to supply a -i flag to my program to make 
global case insensitive searching.

Except that when I actually go to perform the RE operation in perl, it 
only takes /i as a modifier.  I can't simply say, where $case contains 
either "i" or "":

  while (/($re)/g$case) {
     ...
  }

since perl complains that I am not allowed to put a variable there:

Scalar found where operator expected at /home/ssi/tconnors/bin/phrasegrep 
line 131, near "/($re)/g$case"
        (Missing operator before $case?)

I would have expected perhaps a global variable in perhaps perlvar(1) 
telling me I could force a global case insensitive match.

The only way around this that I can see is the butt ugly:

if ($case) {
  while (/($re)/gi) {
     ...
  }
} else 
  while (/($re)/g) {
     ...
  }
}

(or perhaps an eval -- ick?)

And obviously, this would be thouroughly stupid.  So what am I doing 
wrong?

-- 
Tim Connors



More information about the Melbourne-pm mailing list