In order to guide/help you without repeating steps you've already tried, whomsoever does reply would prefer to have a look see at what you've tried, by way of (skeletal) implementation, thus far i.e. in what way(s) did you try use alarm that didn't work.
A user level that continues to overstate my experience :-))
#!/usr/bin/perl -w
use strict;
my $FH;
local $SIG{ALRM} = sub {
die "Timeout no data received\n";
};
open ($FH, '<', "file1") or die "cant open file to read file$!.\n";
alarm 5;
while (<$FH>){
if ($_ =~ m/any string/i){
print "$_\n";
alarm 0;
}
}
close ($FH);