The /o is hardly an optimization, and potentially dangerous. /o prevents recompilation of the regexp. However, since the regexp doesn't change, as it's not interpolating a variable, perl already knows the regexp hasn't changed, so no recompilation happens.

Where it does make a difference is in situations like this:

for my $word ("foo", "bar", "baz") { if ($line =~ /quux $word/o) {yada yada yada} }
Now using the /o modifier prevents the regexp from being recompiled. It's faster. It's unlikely to be correct though.

The only use for /o is the slight optimization where you have a regexp with an interpolating variable and where the variable doesn't change. Without /o perl takes the time to check whether the interpolated string has changed, and recompiles only when it has. With /o, perl skips this check. Typically, the check is dwarved by the execution of the regexp, but there are a few cases where it's a tiny optimization. Given the dangers of /o, I prefer to use qr instead.

IMO, the use of /o should be a mandatory warning, asking the programmer if he really, really wants this. Because for every correct use of it I have seen, I've seen 100 incorrect uses, and 1000 programmers who don't know what /o does.


In reply to Re^4: stripping whitespace gives an 'unitialized value' error by JavaFan
in thread stripping whitespace gives an 'unitialized value' error by stevemayes

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.