It used to be considered beneficial to include the ‘o’ modifier at the end of Perl regular expressions. The current Perl documentation does not even seem to list it, certainly not at the modifiers section of perlre.
Does it provide any benefit now?
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.
/ois deprecated. The simplest way to make sure a regex is compiled only once is to use use a regex object, like so:The interpolation of
$baris done when the variable$regis initialized, and the cached, compiled regex will be used 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:Within the scope of the function, the value of $today_regex stays the same. But the next time the function is called, the regex will be recompiled with the new value of
$today. If he had just used:…the regex would never be updated. So, with the object form you have the efficiency of /o without sacrificing flexibility.