Thursday, November 16, 2006

Net::FTP::Recursive

I had some issues trying to use Net::FTP::Recursive yesterday. Turns out that it only understands UNIX style ls listings so when trying to rget from an FTP site with DOS formatted then it chokes silently. Lots of various questions on this but no answers that I found while googling. The pod does mention something about ParseSub but you have to pay attention to what they're talking about. ParseSub argument lets you add your own formatting input to allow the module to know how to parse the output. It's all very straightforward once you read the source....

If you're looking for why rget isn't working, check to see if your FTP site isn't using DOS style listings. If it is, feel free to use this:




$ftp->rget(ParseSub => &somesub);



sub somesub {

my @returnedobjs = ();

foreach my $line (@_) {

#print "file: $line";

my (@fields, $file);

$line =~ /^

d{1,2}-d{1,2}-d{1,2}s+ #date

d{1,2}:d{1,2}wws+ #time of day nn:nn{AM|PM}

(|d*)s+ #either DIR or filesize

(S*) #name of entry

$

/x;

$fields[6] = $2;


if (isDat($fields[6])) { $datfile = $fields[6]; }

next if $fields[6] =~ /^.{1,2}$/;

if ($1 eq '') {


$file = Net::FTP::Recursive::File->new(IsDirectory=>
1,
IsPlainFile => 0,

IsSymlink => 0,

OriginalLine => $line,

Fields => [@fields]);

}

else {

$file = Net::FTP::Recursive::File->new(IsDirectory => 0,

IsPlainFile => 1,

IsSymlink => 0,

OriginalLine => $line,

Fields => [@fields]);

}

push(@returnedobjs, $file);

}

return(@returnedobjs);

}