while (my $line = ) { if ($line =~ /$_/) { ... last; # breaking while loop } else { ... last; # breaking while loop } }