The slurp technique shown is okay if you have small files. I know I have servers where slurping the biggest disk file would make a serious dent on the RAM, enough to start it swapping into oblivion.
The other thing I'm not sure of is whether these patterns occur on the same line, or on different lines. The former case is easy to solve, the latter case involves seeing whether you've seen one after having seen the other:
Assuming you have a variable named $lacking_both that keeps count, your file read loop would look like:
my $saw_alpha = 0;
my $saw_charlie = 0;
while( <DAT> ) {
if( /alpha/ ) {
++$saw_alpha;
last if $saw_charlie;
}
if( /charlie/ ) {
++$saw_charlie;
last if $saw_alpha;
}
}
++$lacking_both unless $saw_alpha and $saw_charlie;
For a once off this will do, but it might be worth generalising this to use a hash instead of discrete scalars if you need to look for an arbitrary number of patterns.
I'd probably also be tempted to
push @lacking_both, $name
So that @lacking_both in a scalar context gives you the number of files lacking both patterns, but also the names of the files themselves, because you are probably going to need that information somewhere down the track anyway.
update: another thing I just thought of: a big strike against slurping the file is that you might slurp the whole file, only to find out that you hit both patterns in the first 10 lines of the file. In which case a short-circuiting last, as shown above, will be a big win. |