bless$self=>perlmonks; has asked for the wisdom of the Perl Monks concerning the following question:

I have a bunch of regular expressions in a text file that is frequently read, but infrequently changed. I want to compile all of the regexes, and store them in another file. I tried doing this like so:
$re = qr/$foo/; print FILE $re, "\n"; # ... and at a later time ... $re = <FILE> chomp $re; if($line =~ /$re/) { # blah blah blah....
and this appears to work. My only question is this: Am I truely saving CPU time by doing this, or does perl still have to do something special to turn that string I got from a file into a regular expression.

Replies are listed 'Best First'.
Re: precompiling regexes
by japhy (Canon) on Aug 31, 2001 at 05:52 UTC
    The magic is only there for qr//'ed regexes. If you read it from the file, it's just a string. You'll have to re-qr// it for any gains. But I think you're wasting effort on this. Maybe not.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;