Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Patterns without interpolation are compiled when their quoting operator (m//, s///, qr//) is compiled. /o shouldn't matter one bit for those, so I won't discuss them.

Patterns with interpolation are compiled when their quoting operator (m//, s///, qr//) is executed.

Perl caches compiled regex patterns to avoid needless recompilation in situations like the following:

# Compiles each pattern once since m// realises # you're using the same pattern twice in a row. for my $re (qw( foo bar )) { for (1..2) { /$re/ } }

A match or substitution operator can only resuse the last regex is compiled, so the following isn't efficient:

# Compiles each pattern twice for (1..2) { for my $re (qw( foo bar )) { /$re/ } }

You can use qr// to precompile a pattern.

# Compiles each pattern once my @res = map qr/$_/, qw( foo bar ); for (1..2) { for my $re (@res) { /$re/ } }

Note that Perl currently flattens and recompiles compiled patterns interpolated into another pattern.

# Doesn't recompile $re if it's a qr//. /$re/ # Stringifies and recompiles $re if it's a qr//, # but it should be subject to the caching mentioned above. /x$re/

This should be optimised in the future.

In reply to Re: Regexp optimization - /o option better than precompiled regexp? by ikegami
in thread Regexp optimization - /o option better than precompiled regexp? by Hessu

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (2)
    As of 2018-01-21 06:43 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (227 votes). Check out past polls.