my $cntr = 0 my $stuff = "" LINE: while() { if($cntr == 0 && $_ !~ /$myregex/) { next LINE; } last LINE if $cntr >= 5; $stuff .= $_; $cntr++; }