I'm not convinced that so called "compiled regular expressions" are significantly faster. Here's a benchmark, matching IP addresses. One using the 'variable' approach, and one using compiled regular expressions:
#!/usr/bin/perl use strict; use warnings 'all'; use Benchmark; use vars qw /$ip_v $ip_re @data/; my $quad_v = q '(?:25[0-5]|2[0-4]\d|1\d\d|\d\d?)'; my $quad_re = qr '(?:25[0-5]|2[0-4]\d|1\d\d|\d\d?)'; my $sep_v = q '\.'; my $sep_re = qr '\.'; $ip_v = qq "$quad_v$sep_v$quad_v$sep_v$quad_v$sep_v$quad_v"; + $ip_re = qr "$quad_re$sep_re$quad_re$sep_re$quad_re$sep_re$quad_ +re"; @data = map {join "." => map {int rand 1000} 1 .. 4} 1 .. 1_000; timethese -5 => { var => 'for (@data) {/$ip_v/}', re => 'for (@data) {/$ip_re/}', }; __END__
Running this results in:
Benchmark: running re, var for at least 5 CPU seconds...
        re:  5 wallclock secs ( 5.25 usr +  0.00 sys =  5.25 CPU) @ 46.10/s (n=242)
       var:  5 wallclock secs ( 5.16 usr +  0.00 sys =  5.16 CPU) @ 45.93/s (n=237)
Not what I call a significant win for compiled regular expressions. Perhaps you have examples where the gain is large - I've yet to encounter them.

Abigail


In reply to Re: RegEx Perl Code by Abigail-II
in thread RegEx Perl Code by Jaspersan

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.