In the "mod_perl_traps" page, when it refers to regexes only being compiled once, it is
specifically referring to regexes that use the /o modifier:

    my $x =~ /$somevar/o;

In regular Perl (and mod_perl), that causes the pattern to only be compiled once.  If 
the value of
$somevar changes, the regular expression will still try to match against the old 
pattern.  This is
a common problem.  In the mod_perl environment, the problem is that when using the /o 
modifier,
the regex is still only being compiled once and subsequent requests to your script 
will still use
the first regex pattern encountered, regardless of what you specify.  The 
mod_perl_traps page
offers strategies to avoid this.  Since you are not using the /o modifier, this 
shouldn't apply to
you.

I ran your script from the command line and it works fine.  However, I did notice that 
you weren't
using strict and this may be a source of some problems.  I am guessing that since you 
didn't use
it, your %content hash has old data hanging around in subsequent invocations of the 
script. 
However, while this would be a memory leak, it shouldn't cause a problem.

My suspician is that your "splits" may be an issue:

    foreach my $line (split /^/, $textstr) {

Since the caret "^" in the first position of a regex is an anchor to the beginning of 
the string,
you are attempting to split on the beginning of the string.  If you must use the caret 
as a
delimeter, try escaping it in the regex:

    foreach my $line (split /\^/, $textstr) {

Here's an example of the problem (sorry, I'm on a Win32 system so my command line perl 
looks
funky):

C:\>perl -e "$x=q/a^b^c/;@x=split/^/,$x;print $x[0];"
a^b^c

Notice that it wasn't split.  By escaping the caret, the split works fine:

C:\>perl -e "$x=q/a^b^c/;@x=split/\^/,$x;print $x[0];"
a

If you have a caret in your params, this will cause your script to fail.

Hope this helps!

Cheers,
Curtis Poe

PS:  Here's a corrected version of your script with "strict" added.

#!/usr/bin/perl -w
# search for each of a number of strings in a number of web pages
use strict;
use CGI;
require LWP::UserAgent;

my $q = new CGI;

my $textstr = $q->param('STRINGS');
my $pages = $q->param('PAGES');
my @strings;
my $i = 0;
my $ua = new LWP::UserAgent;
my %content;

print $q->header(-expires=>'-1d');
print <<EOH;
<html>
<title>Search results</title>
<body bgcolor=ffffff>
<h1>Search results</h1>
EOH

foreach my $line (split /\^/, $textstr) {
    chomp $line;
    $strings[$i] = $line;
    $i++;
}

foreach my $line (split /\^/, $pages) {
    chomp $line;
    my $request = new HTTP::Request(GET => $line);
    print "Loading $line<br>";
    my $response = $ua->request($request);
    if ($response->is_success) {
                $content{$line} = $response->content;
    } else {
                print "<b>Error: $line".$response->status_line."</b><br>";
    }
}

print "<br>Searching<br>";

foreach my $page (keys %content) {
    print $page."<br>";
    for ($i=0; $i <= $#strings; $i++) {
                # \Q deals with () in pattern
                if ($content{$page} =~ /\Q$strings[$i]/) {
                    print '<blockquote>'.$strings[$i].' found</blockquote>';
                }       
    } 
} 

print <<EOF;
</body>
</html>
EOF



=====
Senior Programmer
Onsite! Technology (http://www.onsitetech.com/)
"Ovid" on http://www.perlmonks.org/

__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/

Reply via email to