When Perl encounters a variable in a regex it can't be sure that the variable will never change and so it compiles it each time the regex is used.
That used to be true, long time ago, but it is no longer so. Even if your regexp contains variables and you don't use /o, perl may still not compile it again... simply because these days, perl contains a check to see if the scalar has changed since last time the regexp got compiled...

Benchmarking will show you that, in case the scalar doesn't change, not using /o is comparable in speed, or might even be appear slightly faster (difference < 1%) than when using /o — but likely that's just inaccuracies in the benchmark.

/o does prevent recompiling the regexp, even if the scalar changes. Likely this is not what you want.

Therefore, use of /o in modern perl is largely obsolete.


In reply to Re: Re: regex "o" modifier by bart
in thread regex "o" modifier by shemp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.