while ( <$file_h> ) { if (my @matches = m/$regex/g) { ++$matched{$_} for @matches; print "$file $_"; } }