Are you really doing script : echo $?? I'm not familiar with the details of unix shell scripting, but maybe $? gets interpolated too soon. Try breaking it down into two commands.
All the trouble and confusion was caused by bugs in the Net::SFTP::Foreign module.
I've upgraded to the latest version and now it works. I don't fully understand why it failed yet, but seems that some die inside the module could be the cause.
Thank you all
Xavi