use warnings; use strict; my $fileText = <>> OP version\n"; open my $infile, "<", \$fileText; while (my $line = <$infile>) { next if $line !~ $comb; for my $target(@argList) { next if $line !~ $target; print "Matched '$target' in: $line"; } } print "\n>>> Grandfather version\n"; open $infile, "<", \$fileText; while (my $line = <$infile>) { print "Matched '$1' in: $line" while $line =~ /($comb)/g; }