while (<FH>) { foreach $pat (@patterns) { if ( /$pat/ ) { # do something } } }
Instead, you either need to use one of the experimental Regexp extension modules from CPAN (which might well be overkill for you purposes), or else put together something like this, inspired from a routine in Jeffrey Friedl's book:
sub _bm_build { my $condition = shift; my @regex = @_; # this MUST not be local(); need my() my $expr = join $condition => map { "m/\$regex[$_]/o" } (0..$#regex); my $match_func = eval "sub { $expr }"; die if $@; # propagate $@; this shouldn't happen! return $match_func; }
sub bm_and { _bm_build('&&', @_) } sub bm_or { _bm_build('||', @_) }
$f1 = bm_and qw{ xterm (?i)window };
$f2 = bm_or qw{ \b[Ff]ree\b \bBSD\B (?i)sys(tem)?\s*[V5]\b };
# feed me /etc/termcap, prolly while ( <> ) { print "1: $_" if &$f1; print "2: $_" if &$f2; }