I'm trying to write a Perl program that will iterate through all files in a given directory and identify the number of times a specific string is present in each of the files.

It's combing through DNA sequences looking for the frequency of `ATG` on the forward strand or reverse complement depending on the direction of the sequence I have. I know that all the sequences contain at least one ATG or CAT (reverse complement to ATG) and for some many more but in my output file it's giving me zero or one. Any suggestions?

P.S. Ignore the unnecessary variables I'm editing a previously written script 

Here's my code 

```
#!/usr/bin/perl

my @file=<*.fasta>;
for $file (@file) {

my $get_file = <../[ES]RR*/> or print "Could not find";
$check = substr($file, 0, 9);
$filename = substr ($get_file, 3, 20);

my $pattern_reverse = 'CCATTTTGTCCAA[AC]C';
my $pattern = 'G[GT]TTGGACAAAATGG';
my $forward_start = 'ATG' ;
my $reverse_start = 'CAT' ;

open(DATA,$file) or die ("Couldn't open file.");

my $contig_name;
my $not_found_mark;
my $position;
my $symbol = ">";
my $contig_string;
my $contig_length;

$contig_name = <DATA>;
$not_found_mark = 1;
$contig_string = "";

while ((my $line = <DATA>) && ($not_found_mark)) {

chop($line);

$position = index($line,$symbol);
if ($position < 0) {
        $contig_string .= $line;
        }
else {
        $not_found_mark = 0;
        }
}

print "$filename \n";
$contig_length = length $contig_string;
print "The contig is $contig_length characters. \n";

if ($contig_string =~ /($pattern)/ ) {
        print "Found forward pattern.\n";
        if ( $contig_string =~ /(ATG)/ ) {
            $ATG_count = 0;
            $ATG_count++;
            open ( Match, ">>", ATG_match ) or die "Could not open ATG_match";
            print Match ">$filename $check $ATG_count \n" 
                or die "Could not append.";
            print "$ATG_count \n";

        }
}

elsif ( $contig_string =~ /($pattern_reverse)/ ) {
        print "Found reverse pattern.\n";
        if ( $contig_string =~ /(CAT)/ ) {
            $ATG_count = 0;
            $ATG_count++;
            open ( Match, ">>", ATG_match ) or die "Could not open ATG_match";
            print Match ">$filename $check $ATG_count \n" 
                or die "Could not append.";
            print "$ATG_count \n";
    }
}

else  {
        print "$file \n";
        print "Did not find pattern. \n";
        open ( Nomatch, ">>", no_ATG_match ) or die "Could not open";
        print Nomatch ">$filename $check\n" or die "Could not append";      
        }
}
print ( "There are $ATG_count ATG's \n" );
close ( Match );
close ( Nomatch );
close( DATA );

```