0

如果它们是文件或目录,我想测试我用 ftp 下载的文件和目录。使用下面的代码,我总是得到 else 语句 ( 2,4,6 ) 曾经$x是什么 (文件或目录)。这段代码有什么问题?

use Net::FTP;

my $host = "whatever";
my $user = "whatever";
my $password = "whatever";

my $f = Net::FTP->new($host) or die "Can't open $host\n";
$f->login($user, $password) or die "Can't log $user in\n";

# grep all folder of top level
my @content = $f->ls;

# remove . and ..
@content = grep ! /^\.+$/, @content;

foreach my $x (@content) {

    print "\n$x: ";
    if ( -f $x ) {print " ----> (1)";} else {print " ----> (2)";}
    if ( -d $x ) {print " ----> (3)";} else {print " ----> (4)";}
    if ( -e $x ) {print " ----> (5)";} else {print " ----> (6)";}
}
4

1 回答 1

4

根据我在 OP 中的评论,ls只返回实体名称列表,它不会将它们提取到本地机器。对于get文件和目录,您需要使用Net::FTP::Recursive. 这是一个例子:

use warnings;
use strict;

use Net::FTP::Recursive;

my $host = 'localhost';
my $user = 'xx';
my $password = 'xx';

my $f = Net::FTP::Recursive->new($host) or die "Can't open $host\n";
$f->login($user, $password) or die "Can't log $user in\n";

my @content = $f->ls;

@content = grep ! /^\.+$/, @content;

# note the rget() below, not just get. It's recursive

$f->rget($_) for @content;

foreach my $x (@content) {

    print "\n$x: ";
    if ( -f $x ) {print " ----> (1)";} else {print " ----> (2)";}
    if ( -d $x ) {print " ----> (3)";} else {print " ----> (4)";}
    if ( -e $x ) {print " ----> (5)";} else {print " ----> (6)";}
}

示例输出:

a.txt:  ----> (1) ----> (4) ----> (5)
dir_a:  ----> (2) ----> (3) ----> (5)
于 2016-01-19T18:24:55.230 回答