The problem here is that you're getting your $exon variable confused with $gtf[2]. You're using $exon to hold the counter which you increment on matching lines, but then you clobber it by assigning $gtf[2] to it after that, but then you don't use it after that. Also, if you're going to use a counter, you don't need to use a regex to increment the digit part of the field. Also, there's no need for an elsif when there are only two possibilities (eq and ne).
if ( $gtf[9] ne $previous ){
$exon = 1; # reset counter
} else {
$exon++; # increment counter
}
$gtf[2] = "exon$exon"; # put counter back in field
Aaron B.
Available for small or large Perl jobs; see my home node.