in reply to Re: lookbehind
in thread lookbehind

I'm a little confused... I thought the "o" modifier was used only when you have no variables to interpolate. Doesn't it tell Perl to compile the regex one time instead of every time it's evalutated, thus speeding up the program? Please clarify the role of "o" because I must have missed something important.

Replies are listed 'Best First'.
RE: RE: Re: lookbehind
by mdillon (Priest) on May 07, 2000 at 23:28 UTC
    since a regular expression without variable interpolation doesn't have the potential to change between uses, Perl only ever compiles it once. however, when a regular expression contains a variable to be interpolated, the value of the variable can potentially be different every time, so Perl compiles the regular expression with the current value of the variable every time it is used. the 'o' modifier tells Perl that the values of interpolated variables should be treated as constant, so that Perl will only compile the regular expression 'once' no matter how many times you use it.

    i'm not sure whether or not using 'o' on a regexp without any variable interpolation adversely affects performance, but it is simply unnecessary.

      Great explanation, thanks! I'm sure this point has confused others in the past, because I've seen more experienced Perl programmers than me make the same mistake (probably where I picked it up from).