using the Net::FTP module in Perl correctly
The easiest way to implemente an FTP client in Perl is to use the Net::FTP module. Here is an example to retrieve files and sub-directories in the root directory:use Net::FTP;
$ftp = new Net::FTP($host, $port) or die “cann't connect to FTP server $host:$port“;
$ftp->login($user, $password) or die “cann't log into FTP server $host:$port“;
@list = $ftp->dir;
$ftp->quit;
foreach (@list) {
if($_ =~ /^d/i and $_ =~ s/^([^\s]+\s+){8}//) {
push(@subdirs, $_);
next;
} elsif($_ =~ /^-/i and $_ =~ s/^([^\s]+\s+){8}//) {
push(@files, $_);
next;
}
}
But there is still some uncertainty in line 5, because the validity of the data returned from the dir function is not guaranteed. In order to make sure that the variable @list is complete and correct, we must check the return value from the status function in the Net::CMD module and compare it with the constant-like function CMD_OK after.
页:
[1]