I think it is possible that your input file has at some point acquired Windows CR-LF line endings, but you are running it on a Unixish system that expects LF line endings. So the chomp() is stripping the LF but leaving the CR, which then gets included in the joined string that you output.
Without changing the input file, one option to fix that would be to replace the chomp to remove all trailing whitespace:
while (<>) { s{\s+$}{}; ...
It is common that a string with a lone CR ("carriage return") is displayed to the terminal by "returning the carriage" as a typewriter would to the first column without starting a new line (which is what LF, "line feed", does). This could explain the output you are seeing. Piping the output to a hex dump program such as od -x would help to confirm that.
In reply to Re: Why is my code producing weird output?
by hv
in thread Why is my code producing weird output?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |