Please find below the full output.
#ssh-rsa AAAAB3NzaC1yc2 root@HOSTA#gI" /tmp/test.txt']] at ./key.pl li
If that is really, really the (start of the) output of your script, then the hostname (maybe) contains a newline or a carriage return or some other whitespace. Have you really, really checked that your variables contain what you think they do?
If the file contains Windows newlines for example, the output will look as above, as the carriage return left over from the \r\n sequence will move the cursor to the start of the line again.