my @matching_file; foreach $file (@file_list) { open (my FH, '<', $file); while() { if(//) { push(@matching_file,$file); #Store files in which pattern is present. last; #Once the pattern is found no need to iterate further the same file. } } }