my @words = (
'foo',
'bar',
);
foreach (@array) {
foreach my $word (@words) {
if (/\Q$word/) { # BAD!! $word always changes.
print;
last;
}
}
}
####
my @words = (
'foo',
'bar',
);
foreach my $word (@words) {
foreach (@array) {
if (/\Q$word/) { # GOOD!! regexp only recompiled when needed.
print;
last;
}
}
}
####
my @words = (
'foo',
'bar',
);
while () {
foreach my $word (@words) {
if (/\Q$word/) { # BAD!! $word always changes.
print;
last;
}
}
}
####
my @words = (
'foo',
'bar',
);
# Precompile the regexps.
my @regexps = map { qr/\Q$_/ } @words;
while () {
foreach my $regexp (@regexps) {
if (/$regexps/) { # GOOD!! $regexp is a compiled regexp.
#if ($_ =~ $regexp) { # GOOD!! Alternate syntax.
print;
last;
}
}
}
####
use Regexp::List ();
my @words = (
'foo',
'bar',
);
my $regexp = Regexp::List->new()->list2re(@words);
while () {
print if /$regexp/;
#print if $_ =~ $regexp; # Alternate syntax.
}
####
my @patterns = qw(
create
drop
delete
update
insert
);
my $regexp;
$regexp = Regexp::List->new(modifiers => 'i')->list2re(@patterns);
$regexp = qr/\b(?:$regexp)\b/;
...
while ($data = $sth->fetchrow_arrayref()) {
# index is faster than regexps on constant strings.
next if index(lc($data->[10]), 'tempdb') >= 0;
if ($data->[13] =~ $regexp) {
print "$data->[3] $data->[9] $data->[10] $data->[13]\n";
last;
}
}