perlfaq nodetype
faq_monk
<P>
Using a variable in a regular expression match forces a re-evaluation (and
perhaps recompilation) each time through. The <CODE>/o</CODE> modifier locks in the regexp the first time it's used. This always happens
in a constant regular expression, and in fact, the pattern was compiled
into the internal format at the same time your entire program was.
<P>
Use of <CODE>/o</CODE> is irrelevant unless variable interpolation is used in the pattern, and if
so, the regexp engine will neither know nor care whether the variables
change after the pattern is evaluated the <EM>very
first</EM> time.
<P>
<CODE>/o</CODE> is often used to gain an extra measure of efficiency by not performing
subsequent evaluations when you know it won't matter (because you know the
variables won't change), or more rarely, when you don't want the regexp to
notice if they do.
<P>
For example, here's a ``paragrep'' program:
<P>
<PRE> $/ = ''; # paragraph mode
$pat = shift;
while (<>) {
print if /$pat/o;
}
</PRE>
<P>