Hi
bisimen,
your error is in this line:
$mass .= "$mass_values{$codon}\n";
This line concatenates all the masses (interleaved with a new line), whereas you want to sum them up. So, in the end, you get a large string with all the masses, each on its own line.
Just replace concatenation with addition:
$mass += $mass_values{$codon};
and your first program should do what you want, without the need for the second one.
There are other improvements that could be made: use strict, don't use no warnings 'uninitialized'; just to silence the warnings, but try to remove the warnings by understanding where they come from (possibly a new line character at the end of your string in the file), chomp the line you read from the input file, use split to get the individual letters of the protein string (see choroba's example above).