#! perl -slw use strict; my $pid = open TAIL, '-|', 'tail -f theLog' or die $!; my @matches; while( @matched <3 and defined( $_ = ) ) { push @matches, $_ if /the search term/; } kill 2, $pid; ## do something with the 3 lines in @matches.