According to perlop
The effect that the 'o' modifier has does not apply, limited to those templates that explicitly use it.
So if you write
my $str = 'x'; my $re = qr/$str/o; ... if (s/$re//) { ... }
Perl will still check if the value of $re has changed when s/// executed. /o acts as a promise that the value of $str used in compiling $re will not change, so if you qr// again, you will get the same result, even if $str has changed. This can be seen with use re 'debug' :
use strict; use warnings; use re 'debug'; foreach my $i (0 .. 2) { my $s = '123'; print STDERR "Setting \$re\n"; my $re = qr/$i/o; print STDERR "Performing s///\n"; $s =~ s/$re//; }
With the /o modifier, you will only see "Compiling REx ..." after "Installing $ re" for the first time through the loop. Without it, you will see every iteration.
The conclusion is that if you want to change the pattern at run time, you should not use /o . This will not affect s/// , and it will not allow you to recompile $re when you need to.
Michael carman
source share