I had a site that was running way too slow. I determined that the problem was that the same search and replace regexes were being compiled over and over again. I was doing stream parsing of html with HTML::PARSE, so I could not change the program flow. I decided to compile the regexes and then use them over and over and found out that is was not as simple as it seemed. See the code below.
#!/usr/bin/perl -w use strict; my $lhs = "abc"; my ($string1, $string2,$string3, $string4); $string1 = "qrsabcwty"; $string2 = "qrsabcwty"; $string3 = "qrsabcwty"; $string4 = "qrsabcwty"; my %lhsCompiled; #lets say the regex we want is $string1 =~ s/($lhs)/XXX$1/; # Then I compile it. my $rhsCompiled = qr{XXX$1}; $lhsCompiled{$lhs} = qr{($lhs)}; # This regex will not work $string2 =~ s/$lhsCompiled{$lhs}/$rhsCompiled/; # The left hand side works, but the right had side comes # out as (?-xism:XXXabc) # it returns qrs(?-xism:XXXabc)wty # instead I make the right hand side into a code block # using the e modifier which identifies the right hand side # as code block or a subroutine call. sub lhsSub {return "XXX".$1} $string3 =~ s/$lhsCompiled{$lhs}/lhsSub/e; $string4 =~ s/$lhsCompiled{$lhs}/"XXX".$1/e; # I suspect using a subroutine is faster. It will be compiled, # I am not certain if the code will be compiled print "string1 $string1\n"; print "string2 $string2\n"; print "string3 $string3\n"; print "string4 $string4\n"; #The results are # string1 qrsXXXabcwty # string2 qrs(?-xism:XXXabc)wty # string3 qrsXXXabcwty # string4 qrsXXXabcwty
You can see the code at work at Truespel Converter

In reply to Compiling Search/Replace Regexs by InterGuru

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.