As you can read here I like to qr// my patterns. In my case I gained nothing from it (other than not biting myself), but you'd gain a great deal by qr//:ing the @patterns. And while we're talking about qr//, I'd prefer to see a qr// object instead of an o modifier in the if expression.

The o modifier is evil and as of qr//'s introduction we no longer need it. But we'd be fine even without both qr// and the o modifier in this particular case. That is because perl is friendly enough to remember the last pattern used for every match op. If the patterns are identical (stringwise) the last compiled regex for that match op is used. Since you only use $pat once no recompilation is done.

Demonstration:
my @patterns1 = ('foo') x 2; my @patterns2 = ('bar') x 2; use re 'debug'; while (@patterns1) { 'a' =~ shift @patterns1; 'b' =~ shift @patterns2; } __END__ Compiling REx `foo' size 3 first at 1 1: EXACT <foo>(3) 3: END(0) anchored `foo' at 0 (checking anchored isall) minlen 3 Compiling REx `bar' size 3 first at 1 1: EXACT <bar>(3) 3: END(0) anchored `bar' at 0 (checking anchored isall) minlen 3 Freeing REx: `foo' Freeing REx: `bar'
As you can see, the foo pattern and the bar pattern are only compiled once. But there's no harm in using qr// here, so I still suggest it.

Cheers,
-Anomo

In reply to Re: Re: Re: First Pattern Matching by Anonymous Monk
in thread First Pattern Matching by artist

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.