#!/usr/bin/perl
use strict;
use warnings;
my @buffer;
my @pattern = qw(abc def ghi jkl mno);
while (<DATA>) {
push @buffer, $_;
next if @buffer != @pattern;
While your scheme is terse and elegant in that it fills in the buffer
as needed, but even if I'm not one of those paranoids about
efficiency, it bothers me a little that the check is done for all
lines, including the majority of them that would not require it. I'd
pre-load it instead.
my $matches = grep /1/,
map $buffer[$_] =~ /$pattern[$_]/,
0 .. $#pattern;
Well, I'm a big fan of map() and grep(), but in this case it seems to
me an overhead to use them. What about a simple counter instead? Also,
another source of inefficiency is in the fact that all patterns are
tried even if some of them fail, thus...
print @buffer if $matches == @pattern;
shift @buffer;
}
....all in all I'd rewrite it as
#!/usr/bin/perl
use strict;
use warnings;
my @pattern = qw(abc def ghi jkl mno);
my @buffer = (0, map scalar <>, 1..$#pattern);
LINE: while (<>) {
shift @buffer; push @buffer, $_;
$buffer[$_] =~ /$pattern[$_]/ or
next LINE for 0..$#pattern;
print @buffer;
}
__END__
Michele