in reply to creating a directory remotely implicitly

Another option, besides running mkdir -p $targetdir, is to use SFTP:
my $sftp = $ssh->sftp; $sftp->put($sourcefile, $remotefile);