It is still accepted, for reasons of backwards compatibility if nothing else.

As noted by J A Faucett and brian d foy, the 'o' modifier is still documented, if you find the right places to look (one of which is not the perlre documentation). It is mentioned in the perlop pages. It is also found in the perlreref pages.

As noted by Alan M in the accepted answer, the better modern technique is usually to use the qr// (quoted regex) operator.

6 Answers
6

I'm sure it's still supported, but it's pretty much obsolete. If you want the regex to be compiled only once, you're better off using a regex object, like so:

my $reg = qr/foo$bar/;

The interpolation of $bar is done when the variable is initialized, so you will always be using the cached, compiled regex from then on within the enclosing scope. But sometimes you want the regex to be recompiled, because you want it to use the variable's new value. Here's the example Friedl used in The Book:

The /o modifier is in the perlop documentation instead of the perlre documentation since it is a quote-like modifier rather than a regex modifier. That has always seemed odd to me, but that's how it is. Since Perl 5.20, it's now listed in perlre simply to note that you probably shouldn't use it.

Before Perl 5.6, Perl would recompile the regex even if the variable had not changed. You don't need to do that anymore. You could use /o to compile the regex once despite further changes to the variable, but as the other answers noted, qr// is better for that.

This is an optimization in the case that the regex includes a variable reference. It indicates that the regex does not change even though it has a variable within it. This allows for optimizations that would not be possible otherwise.

Welcome to Stack Overflow. Please read the About page. Thank you for running these timings. The first three results are likely to be essentially the same; the second three might also be 'the same', or the might be some gradation in the timings; the last result seems to be separate. Can you include the code you used in your tests so I can understand more exactly what you measured? If there was a data file, you shouldn't include it, but it would help to identify how big the file was — the number of lines and total number of bytes in it.
–
Jonathan LefflerDec 26 '14 at 18:02