There probably isn't anything wrong with your Perl code. Can you ssh via the command line? There are a lot reason why a ssh connection could fail. OpenSSH has excellent documentation here:
http://www.openssh.org/manual.html. You might want to try executing ssh from the command line and add verbosity with the
-vvvv switch.