If you happen to be working in a linux environment, you could approach this a bit differently by using the fuse kernel module with the lftpfs driver to let the OS manage the network connection (including the sftp protocol). Then the problem looks more like doing a recursive copy.
in reply to recursive SFTP
Fuse has also been ported to FreeBSD and OS X, but I don't know if the lftpfs driver works on those platforms.
Apropos of nothing, the lftpfs fuse driver is written in Perl.