You can have a global counter variable on the top intialized to 0
before your for
loop, say:
my $counter = 0;
# go through each reference file
for my $file (@reference_files)
# ... Rest of your code ...
Then, you can increment $counter
inside of the if
statement where $matching_genes
is assigned:
if (my $matching_genes = $genes{$scaffold})
{
$counter++;
say join "\t", $gene_name, $_ for values %$matching_genes;
}