Support for password authentication was added to Net::SFTP::Foreign seven years ago. Maybe it is time to update your scripts to use that feature.
The problem with using Expect for authentication the way you are doing is that the data will be transferred over the pseudo TTY and that is not always reliable.