Perl sFTP:如何检查远程文件不存在
Perl sFTP : how to check remote file doesnt exist
我刚接触 Perl 1 天,正在学习 API doc here,基本问题很少
$sftp = Net::SFTP::Foreign->new($host, autodie => 1);
my $ls = $sftp->ls("/bar");
# dies as: "Couldn't open remote dir '/bar': No such file"
问题
- 使用 autodie 连接会自动关闭吗?
- 我们在上面的例子中看到了如何使用文件夹,类似的语法也适用于文件?
或者像这样更有意义??
my $sftp = Net::SFTP::Foreign->new($host, autodie => 1);
$sftp->find("/sdfjkalshfl", # nonexistent directory
on_error => sub { print "foo!\n";sftp->disconnect();exit; });
我试图 运行 在我的 windows 机器上执行代码
use Net::SFTP::Foreign;
my $host = "demo.wftpserver.com";
my $sftp = Net::SFTP::Foreign->new($host ,ssh_cmd => 'plink',autodie => 1);
my $ls = $sftp->ls("/bar");
但我收到错误
'plink' is not recognized as an internal or external command ,
然而,当我从 windows 命令行 运行 plink 时,它工作正常!!
with autodie will the connection be auto closed ?
是的。当程序结束时,一切都被销毁,连接也被关闭。当 $sftp
变量超出范围时也是如此。像这样的模块通常实现 a DESTROY
sub。当对象(在 Perl 中只是一个引用)超出范围时调用它们。该子程序中可以进行一些清理。另一个例子是 DBI,当然还有词法文件句柄(比如来自 open
调用的 $fh
)。
we see in above example how to use folder , similar syntax also works for file ?
No. The docs 说ls
是一个目录:
Fetches a listing of the remote directory $remote. If $remote is not given, the current remote working directory is listed.
但是你可以只对你想要的文件所在的目录做ls
,然后使用wanted
选项。
my $ls = $sftp->ls( '/home/foo', wanted => qr/^filename.txt$/ );
虽然 autodie
应该会死掉,但如果您不希望它真的死在这里,您应该将它包装在 Try::Tiny 调用或 eval
中。
use Try::Tiny
# ...
my $ls = try {
return $sftp->ls( '/home/foo', wanted => qr/^filename.txt$/ );
} catch {
return; # will return undef
};
say 'Found file "filename.txt" on remote server' if $ls;
至于找不到 plink
,可能是 Windows PATH
与您的 Perl 看到的不同。
我刚接触 Perl 1 天,正在学习 API doc here,基本问题很少
$sftp = Net::SFTP::Foreign->new($host, autodie => 1);
my $ls = $sftp->ls("/bar");
# dies as: "Couldn't open remote dir '/bar': No such file"
问题
- 使用 autodie 连接会自动关闭吗?
- 我们在上面的例子中看到了如何使用文件夹,类似的语法也适用于文件?
或者像这样更有意义??
my $sftp = Net::SFTP::Foreign->new($host, autodie => 1);
$sftp->find("/sdfjkalshfl", # nonexistent directory
on_error => sub { print "foo!\n";sftp->disconnect();exit; });
我试图 运行 在我的 windows 机器上执行代码
use Net::SFTP::Foreign;
my $host = "demo.wftpserver.com";
my $sftp = Net::SFTP::Foreign->new($host ,ssh_cmd => 'plink',autodie => 1);
my $ls = $sftp->ls("/bar");
但我收到错误
'plink' is not recognized as an internal or external command ,
然而,当我从 windows 命令行 运行 plink 时,它工作正常!!
with autodie will the connection be auto closed ?
是的。当程序结束时,一切都被销毁,连接也被关闭。当 $sftp
变量超出范围时也是如此。像这样的模块通常实现 a DESTROY
sub。当对象(在 Perl 中只是一个引用)超出范围时调用它们。该子程序中可以进行一些清理。另一个例子是 DBI,当然还有词法文件句柄(比如来自 open
调用的 $fh
)。
we see in above example how to use folder , similar syntax also works for file ?
No. The docs 说ls
是一个目录:
Fetches a listing of the remote directory $remote. If $remote is not given, the current remote working directory is listed.
但是你可以只对你想要的文件所在的目录做ls
,然后使用wanted
选项。
my $ls = $sftp->ls( '/home/foo', wanted => qr/^filename.txt$/ );
虽然 autodie
应该会死掉,但如果您不希望它真的死在这里,您应该将它包装在 Try::Tiny 调用或 eval
中。
use Try::Tiny
# ...
my $ls = try {
return $sftp->ls( '/home/foo', wanted => qr/^filename.txt$/ );
} catch {
return; # will return undef
};
say 'Found file "filename.txt" on remote server' if $ls;
至于找不到 plink
,可能是 Windows PATH
与您的 Perl 看到的不同。