Given that it appears you are logging in via SSH anyway, can you use
Net::SSH::Perl instead of using
Expect directly? It handles command execution on its own; maybe it is clever enough to work around what ever is causing your prompt problem.
In any event, it would clear out the login and command management code from your script.