use strict; my $fileglob = shift || '*.pl'; my $pattern = shift || 'hello'; my $keeplines = shift || 3; for my $file (<${fileglob}>) { unless(open(IN, $file)) { warn "Can't read from $file: $!"; next; } my @lines; while() { push @lines, $_; shift @lines if(@lines > $keeplines); if(/$pattern/i) { print "--- From $file:---\n@lines\n" } } }