EyeOpener has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to learn about the /o regex modifier. The documentation I've found all says pretty much the same thing: /o prevents recompilations of the regex pattern. Perldoc and the Camel book also say specifically "...mentioning /o constitutes a promise that you won't change the variables in the pattern. If you change them, Perl won't even notice."

I want to test this behavior, but I can't seem to make /o ignore changes to variables in the regex. I thought this meant that variables would be interpolated and the regex would be compiled only once (the first time it's encountered), and that if a variable within the regex has changed the next time the pattern is evaluated, that change will be ignored. This doesn't seem to be the case.

Here's the code I'm testing with:

while (1) { print "Enter regex: "; chomp ($regex = <>); print "Enter string: "; chomp ($str = <>); print "Matches!\n" if $str =~ /$regex/o; }

I would expect that in the second iteration (and all future iterations) through the loop, the input into $regex would be effectively ignored, but that doesn't seem to be the case. Have I misinterpreted /o, or is my code bogus? Thanks!

Peter

Replies are listed 'Best First'.
ActiveState /o regex modifier bug
by EyeOpener (Scribe) on Jul 18, 2002 at 19:39 UTC
    Just an update for future SoPW: turns out this is an ActivePerl bug. The code above should work fine on most systems.

    -Peter

Re: Use of /o regex modifier?
by kvale (Monsignor) on Jul 18, 2002 at 18:55 UTC
    It gives the expected behavior for me with 5.6.1:
    Enter regex: a Enter string: abc Matches! Enter regex: d Enter string: abc Matches!
    -Mark
      Interesting, I'm also running 5.6.1 (ActiveState build 626), but I get different output:

      Enter regex: a Enter string: abc Matches! Enter regex: d Enter string: abc Enter regex: d Enter string: abcd Matches! Enter regex: \d Enter string: 3 Matches!

      So maybe I understood the /o modifier correctly after all. Now I just need to know why it ain't working! Thoughts, anyone?

      -Peter

Re: Use of /o regex modifier?
by PodMaster (Abbot) on Jul 18, 2002 at 21:32 UTC
      Thanks for the node. I had done lots of reading on hits from "regex modifier" and "/o modifier" searches, but this one didn't turn up for me. No, really! Time to start using super search...

      -Peter