As of at least perl 5.6.0 you can use qr to precompile regexps... so something like...
$re = qr/$regex/; $_ =~ $re;
granted that is not what you asked... but it's much easier and more straightforward if you didn't already know about it.

Update actually since 5.005 it seems

Update2 I tried this...

use Benchmark; sub make_grep { my $p = $_[0]; eval "sub { grep /$p/o, @_ }"; } sub make_grep2 { my $p = $_[0]; sub { grep /$p/o, @_ }; } $re = '(ob|[tTs])'; $a = make_grep($re); $b = make_grep2($re); $pat = qr/$re/; timethese(100000, { a => sub { $a->(qw(this is a test bob and jobe)) }, b => sub { $b->(qw(this is a test bob and jobe)) }, c => sub { grep $pat, qw(this is a test bob and jobe) } });
...and got this...
Benchmark: timing 100000 iterations of a, b, c... a: 7 wallclock secs ( 5.92 usr + 0.00 sys = 5.92 CPU) @ 16 +891.89/s ( n=100000) b: 5 wallclock secs ( 4.57 usr + 0.00 sys = 4.57 CPU) @ 21 +881.84/s ( n=100000) c: 0 wallclock secs ( 0.62 usr + 0.00 sys = 0.62 CPU) @ 16 +1290.32/s (n=100000)
putting the compiled re to be much faster... maybe I did something wrong...

                - Ant


In reply to Re: coderef assignment with and without eval by suaveant
in thread coderef assignment with and without eval by sacked

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.