in reply to Re^11: Send password in Net::SSH::Expect
in thread Send password in Net::SSH::Expect

Bingo! That was it. Thanks, oh venerable monks! Removed it by adding:
$hostname =~ s/\r//g;
Now output shows:
Launching [[ssh -q user@host 'sed -i "s#.*HOSTA#ssh-rsa AAAAB3NzaC1yc +root@HOSTA#gI" /tmp/test.txt']] at ./key.pl line 81.
And the command is doing what it should. To learn is to struggle.