This is my first Perl script, so far it works fine, except after running for a long time, it begins to throw an odd error: "Use of uninitialzed value at string ne
", and it refers to a line in the isdir
function in Net::FTP::File.
sub Net::FTP::isdir {
my $ftp = shift;
local $_fatal;
my $c = $ftp->pwd();
my $r = $ftp->cwd(@_);
my $d = $ftp->cwd($c);
my $e = $ftp->pwd();
$setmsg->( $ftp, "Could not CWD into original directory $c" ) if $c ne $e || !$d;
return undef if $_fatal;
return $r ? 1 : 0;
}
And here is my code:
#!/usr/local/bin/perl
use Net::FTP;
use Net::FTP::File;
$hostname = 'ftp.census.gov';
$username = 'anonymous';
$password = '-anonymous@';
$base_dir = '/geo/tiger';
$root_dir = 'TIGER2010';
$ftp = Net::FTP->new($hostname)
or die "Failed to connect to ", $hostname;
$ftp->login($username, $password)
or die "Failed to login ", $ftp->message;
$ftp->cwd($base_dir);
$ftp->binary;
recursive_get($root_dir);
$ftp->quit;
sub recursive_get {
$ftp->cwd($_[0]);
print "Adding directory ".$_[0].".\n";
mkdir($_[0], 0777) unless -d $_[0];
chdir($_[0]);
my @directory_listing = $ftp->ls;
foreach(@directory_listing) {
next if ($_ =~ /^\./);
print "Processing ".$_.".\n";
recursive_get($_) if $ftp->isdir($_);
if($ftp->isfile($_)) {
print "Getting $_ \n";
$ftp->get($_);
print "Extracting ".$_."\n";
system("unzip -u $_");
unlink($_);
}
}
$ftp->cdup;
chdir("..");
}
Thanks in advance, and any general comments about the code would be appreciated too.