Reputation: 143
I have designed a program for retreiving files from a remote sftp folder, and it was working fine.
And now I am having a weird issue with this program. Please find below the code fragment with which I am having this issue:
my @ls = $sftp->ls("$config->[$i]->{'SOURCE'}") or do {
$logger->error("Could not retrieve directory listing, exiting from processing");
next;
};
or statement for $sftp->ls is being executed, which is expcted only when $sftp->ls fails. But I don't have any issue while accessing it manually. Status Code which I have received after $sftp->ls statement is 0, which means Successful.
I am not sure why it is still redirected to the or fragment, please advise. Also there is no issues when the remote folder is not empty.
SFTP used:
use Net::SFTP::Foreign::Compat ':supplant';
Upvotes: 1
Views: 57
Reputation: 85887
Your statement my @ls = ... or ...
checks the result of the =
operator. Because its LHS is an array, this is list assignment. List assignment in scalar context returns the number of elements on the RHS. In other words, if a non-empty list was returned, everything is OK; an empty list is treated as an error.
You didn't tell us what $sftp
is. I'm going to assume it's a Net::SFTP object.
According to the documentation, $sftp->ls
returns a list of directory entries. Therefore your code treats an empty directory as an error. You didn't say this explicitly either, but you did say "there is no issues when the remote folder is not empty", implying there's a problem with empty directories.
The issue (as far as I can see) is with your code. It does exactly what you told it to.
Update: I've looked at the source code. This is not documented, but you can check for errors separately by calling the method in scalar context:
my @ls = do {
my $ls_ref = $sftp->ls($config->[$i]{SOURCE}) or do {
$logger->error("Could not retrieve directory listing, exiting from processing");
next;
};
@$ls_ref
};
In scalar context, ls
returns either a reference to an array of results or undef
on failure.
Upvotes: 2