compiled regex problem

compiled regex problem

Post by David L Rubi » Sat, 25 Sep 1999 04:00:00

I'm confused about the "compiled regular expreseion problem."


My Apache server is running a mod_perl script which does the following:
I use the url<site> which invokes my script. The
script retrieves
content from <site>, processes the content there using some regexs, and returns
some content to me.

1. There is a global variable %regex_tab which contains regular expressions for
each site I might

2. There is a function process() which looks as follows

sub process

        my $re = $regex_tab{$site};  
        while ($content =~ /$re/og) {



So, my question is, do I have to worry about the compiled regular expression
I'm resetting $re to something (apparantly) different upon each invocation not
to mention
the fact that $re is lexically scoped within a function. But, if this does not
an instance of "the problem," what does?


David L Rubin                         f 1.973.581.6665 v 1.973.386.8598
Lucent Technologies, NJ0117 14J-211, 67 Whippany Rd, Whippany, NJ 07981


compiled regex problem

Post by lt lindle » Sat, 25 Sep 1999 04:00:00

:>My Apache server is running a mod_perl script which does the following:

:>   my $re = $regex_tab{$site};  
:>   while ($content =~ /$re/og) {

Doesn't that tell Perl to only compile your regexp once -- that $re
is not going to change?  But $re can be different each time the
subroutine is entered.

If you used qr{} to build your regular expressions that you stored
in %regex_tab, then they will only be compiled once anyway.

To be honest, I was not sure that this was true from reading the
documentation.  It looks like "qr" is as much a quoting operator
as anything else.  So I resorted to trying it.  :-)

Run the following:
#!/usr/lib/lprgs/perl -w
use re 'debugcolor';
use strict;
my %r;
$r{xxx} = qq{xxx};
$r{yyy} = qq{yyy};

for ('xxx', 'yyy', 'xxx', 'yyy') {
        print "$_ true\n" if /$r{$_}/;


As strings are encountered in the match operator each time through
the loop, they become a freshly recompiled expression.

Then change "qq" to "qr" in the above two assignments to %r and try
it again.

When the objects that the match operator encounters were built with
"qr", they don't get recompiled, even though there isn't any /o
modifier.  I guess that's why they are called compiled regular

Now put a /o modifier on the match operator.  You get the behavior
your program is getting.


// Lee.Lindley   /// Programmer shortage?  What programmer shortage?

////////////////////    50 cent beers are in short supply too.