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

Why does the list of regexp modifiers not list /o and just off-the-cuff mention it elsewhere in perlretut?

Replies are listed 'Best First'.
Re: Why is /o not listed as a modifer
by moritz (Cardinal) on Dec 18, 2008 at 15:18 UTC
    In addition to what others already wrote: /o caused much confusion, and these days its performance advantage is pretty small (if it exists at all), because regex compilation is cached in newer perl versions.

    As far as I know it's not officially deprecated because too much legacy code uses it, but most people discourage its usage anyway.

    If you want to achieve the old behavior, constructing a regex with qr is the recommended way.

Re: Why is /o not listed as a modifer
by jdporter (Paladin) on Dec 18, 2008 at 15:12 UTC

    Probably because /o doesn't modify the semantics of the regex at all. Its only possible impact is on performance.

Re: Why is /o not listed as a modifer
by hbm (Hermit) on Dec 18, 2008 at 16:49 UTC

    I just learned a tough lesson with /o:

    I had a subroutine that loops through a given list of files, slurping each file and splitting it with a RE/o; and processing each chunk.

    Later I had to support files of a different format. In a test, the above routine worked fine, I just had to pass it a different RE.

    But when I put the two subroutine calls together, the second one failed...

    My mistake was thinking that RE/o was compiled once in its narrowest scope - that the subroutine could process 1000 files with one fixed RE, and then process another 1000 files with a different RE.

    I hope I explained that well; it took me a while to figure it out!

      My mistake was thinking that RE/o was compiled once in its narrowest scope - that the subroutine could process 1000 files with one fixed RE, and then process another 1000 files with a different RE.

      Perl does that without the 'o' modifier.

      for my $re (qw( a a a b b a )) { '' =~ /$re/; }
      >perl -Dr 731397.pl 2>&1 | find "Compiling REx" Compiling REx `a' Compiling REx `b' Compiling REx `a'

      Notice how the regexp is only compiled whenever the interpolated variable changes. (Tested 5.6.*, 5.8.*, 5.10.0)

Re: Why is /o not listed as a modifer
by Narveson (Chaplain) on Dec 18, 2008 at 15:10 UTC

    Because the page you refer to lists

    Modifiers that relate to the interpretation of the regular expression inside
Re: Why is /o not listed as a modifer
by ikegami (Patriarch) on Dec 18, 2008 at 21:43 UTC

    There are regexp pattern modifiers (m, s, i and x).
    There are regexp quote operator (qr//) modifiers (m, s, i, x, p and o).
    There are match operator (m//) modifiers (m, s, i, x, p, o, g and c).
    There are substitution operator (s///) modifiers (m, s, i, x, p, o, g, c and e).

    perlre discusses regexp patterns, so it only mentioned regexp pattern modifiers. perlop mentions the operator modifiers along with the operators they modify.

    By the way, you should have no reason to use the 'o' modifier now that qr// exists.

    Update: It used to be that only the 'm', 's', 'i' and 'x' modifiers were listed in perlop. Now I see some of the operator modifiers are there too. 'o' was surely omitted because it's obsoleted by qr//, like I previously mentioned.

    perlre for 5.6.0: msix
    ...
    perlre for 5.6.2: msix
    perlre for 5.8.0: msix
    ...
    perlre for 5.8.8: msix
    perlre for 5.8.9: msixpgc
    perlre for 5.10.0: msixpgc