| Oskar Pearson on Thu, 20 Feb 2003 16:12:24 +0200 (SAST) |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
| [Linux dev] Re: Perl and matching several regex's |
Hi
In the "Object Oriented Perl" book, there's mention of this;
the problem with doing it the easier way is that the perl system
checks the syntax of the regex every time it's used, is re-parsed,
and converted into perl's internal table-driven representation.
He suggests using the qr operator (man perlop)
> I was more thinking in terms of (but not perl syntactically correct yet)
>
> for $i in keys %RegexHash
> {
> if ($_ =~ $RegexHash[$i])
> then
> &process($i,$_);
> break
> fi
> }
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
my %regexes = (
qr /this is a test/ => \&one,
qr /this is another test/ => \&two,
);
print Dumper(\%regexes);
while (<>) {
foreach my $regex (keys %regexes) {
if ($_ =~ $regex) {
&{ $regexes{$regex} };
}
}
}
sub one {
print "case 1\n";
}
sub two {
print "case 2\n";
}
--
Oskar Pearson <oskar@xxxxxxxxxxx>
Qualica Technologies (Pty) Ltd
web: http://www.qualica.com/