get the six different reading frames from the nucleotides, decide on which ones to remove because they got any one of the three stop codons, do that first, then translate the rest of the sequences into proteins and do the blasting. that would save you from using the * to mark where a stop codon is...I know I am not able to give you a solution but I am just suggesting something that could land you anywhere near it...
I will really advice you to use the * only in Typeglob notations in Perl.. I had a feeling that you used blastp on a protein sequence cuz I did not read your code in a clear way. Be a bit more neat instead of repeating the same if statement over and over, your program could be a way shorter and effective too...
Excellence is an Endeavor of Persistence.
Chance Favors a Prepared Mind