in reply to Why is /o not listed as a modifer

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!

Replies are listed 'Best First'.
Re^2: Why is /o not listed as a modifer
by ikegami (Patriarch) on Dec 18, 2008 at 21:58 UTC

    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)