qr// is a regular expression quote, and as such does, in a sense, compile regular expressions. Unfortunately, you're using the regular expression as a hash key, at which point it's turned back into a string. As you process the Apache log file, $rule is just a string. When you use it as a regular expression, it has to be compiled again - each time through the loop.
If I were writing your code, I would store the regular expression rules/filehandles in an array. Here's a sketch of what it might look like:
my @rules; # not %rules.
...
# Process input file of processing rules
while(<INFILE>) {
...
push @rules, {
regex => qr/$string/,
file_handle => $fh
};
}
...
# Read Apache log file and print to various other files
while (my $line = <STDIN>) {
for my $rule_ref (@rules){
my $regex = $rule_ref->{regex};
my $fh = $rule_ref->{file_handle};
if ($line =~ $regex) {
print $fh $line;
}
}
}
Hope this helps.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|