This is obviously a problem with Windows files used under Unix (I have been through that several times, I know now where it comes from, but the first time it took me a while to figure out). Under Unix, the chomp function removes only the "\n" part of the line end, and the "\r" also present in Windows line ends stay there and put the subsequent characters to the start of the line (overwriting whatever is there). Use this:
$line =~ s/\r//g;
$line =~ s/[\r\n]//g;
Corrected two typos (a silly colon before the g regex modifier and a missing /) in the second regex. Thanks to poj
for having pointed out them to me.