No, all regex get compiled into regex opcodes. If they contain no variables, they are compiled once at Perl compile-time. If they contain variables, then the /o flag controls whether the compilation happens the first time it is seen at runtime, or every time it is seen at runtime. But the compilation always happens. It must happen... you can't interpret a regex "directly".

Also note that some modern innovations make /o somewhat less nasty. The regex engine maintains a one-level cache, so if the regex to be compiled hasn't changed and was the most recent one compiled, the cached compilation is reused. That means you don't need /o for this:

my @winners = grep /prefix$part/, @list;
In older Perls, that regex would have been recompiled for each element of @list. Not so, now. Even works for stuff like this:
while (<>) { next unless /prefix$part/; ... }
as long as the rest of the loop doesn't use any other recompile-on-demand regex. But the moment it does, you've got two regexes both being compiled on each iteration, and you get molasses at that point.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.


In reply to •Re: Re: Re: •Re: RegEx re-compilation by merlyn
in thread RegEx re-compilation by habit_forming

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.