http://www.perlmonks.org?node_id=659556


in reply to Net::SFTP does not catch errors

I've noticed a similar "feature" when using 'get' with Net::SFTP. Allow me to expand on your comment a bit, as I suspect 'get' and 'put' operate identically in this regard. Here is some code very similar to yours, using 'get':
eval { $sftp->get($remote_file, $local_file) or die "GET FAILED"; }; print "eval says: $@" if $@;
The problem is that with the 'die' statement present in the get command, the 'die' phrase is passed to $@ whether or not the get command succeeds. As best I can determine, this is a flaw, or at least a limitation, in Net::SFTP. In addition, even when not using the eval statement to enclose the sftp->get command, a die statement is executed even when the 'get' command succeeds. This kills the script right after completing the 'get'.

Removing the 'die' statement but keeping the eval in place results in:

1) No $@ generated if the get succeeds -- this is fine;

2) $@ is created if the get fails and the problem is with the local file path;

3) No $@ generated if the get fails and the problem is in the remote file path -- the get failure remains undetected by eval and so it is not stored in $@.

It appears to me that:

a) die should not be used with $sftp->get commands because even on a successful 'get';

.... i) if eval is used, an unwanted $@ is created from the 'die' statement;

.... ii) if eval is not used the 'die' kills the feed script (although the get succeeds);

b) Eval should be used to generate $@ so that get 'failures' related to the local file path are detected;

c) Using eval does not fully verify the get succeeded because it will ignore get failures due to problems with the remote file path.

If anyone knows a workaround to this limitation with Net::SFTP, please post it here.