You are encountering repeated substitutions on one variable due to a false match. Consider the following:
$str = 'B305;
$str =~ s/B3/B304/; # $str is now B30405
$str =~ s/B3/B304/; # $str is now B3040405
See the repeat_formula() section of the documentation. In particular the part starting from "You should also be careful to avoid false matches". This suggests how to avoid this error.
John.
--