1834571 Members
3328 Online
110069 Solutions
New Discussion

Perl Net::FTP and ls

 
SOLVED
Go to solution
jerry1
Super Advisor

Perl Net::FTP and ls

Does anyone know why "ls" in perl does not
exec the "exec" below when the $file is not
present with "ls". It just passes through.
This is only when connecting to another server
that returns ftp error code 552(vax) not 550
as with hp to hp.

$ftp->ls ($file) or die (exec "$pager $pageid \"No file. \"") ;


So how do you tell perl that code 552 is an
error so die and exec?
5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Perl Net::FTP and ls

The key to what you are trying to do is to examine the status after every command. You also have to note that success in ftp != 0 but is indicated when the first digit (all that is returned by the status method) equals '2'.

I would do it something like this:

use Net::FTP;
use English;
use constant OKEY_DOKEY => 2;

...
...
# Wildcards will work so it's a good idea
# to load an array

my $Pattern = "*.txt";
my @tmp_array = ftp->ls($Pattern);
my $stat = ftp->status;
my $cc = 0;
if ($stat == OKEY_DOKEY)
{
my $i = 0;
while ($i <= $#tmp_array)
{
printf("%s\n",$tmp_array[$i]);
++$i;
}
}
else
{
$cc = ($stat != 0) ? $stat : 243;
printf STDERR
("ls of '%s' failed; (%d)\",$Pattern,
$cc);
}
ftp->quit;
exit($cc);
----------------------------------------
I will be the first to admit that my Perl tends to be a bit wordy but I never have to ask myself 6 months later "Now what does this do?".
If it ain't broke, I can fix that.
jerry1
Super Advisor

Re: Perl Net::FTP and ls

Sorry Clay, your over my head here.

If I understand correctly. There is a
way to get the status(error code) after
the ls that I am doing and check that
code number. The syntax eludes me though.
A. Clay Stephenson
Acclaimed Contributor

Re: Perl Net::FTP and ls

If you had examined FTP.pm you would have found that it uses Net::Cmd. Do a man Net::Cmd and you will find status so that immediately after doing your

@tmp_array = $ftp->ls($file);

you can check the status of that ls by examining $ftp->status
(the most significant digit only) or
by examining $ftp->code (all 3 digits).
Generally all you care oabout is the most significant digit and '2' means that all is well with the most recently executed function (get,put,cd,ls,binary,ascii, ...).


my @tmp_array = $ftp->ls("*.txt");
my $cmd_status = $ftp->status;
if ($cmd_status == 2)
{
# all is well
# do whatever with the elements of @tmp_array
}
else
{
# bad
}
If it ain't broke, I can fix that.
jerry1
Super Advisor

Re: Perl Net::FTP and ls

Got it Clay. I am assuming that any ftp
return code of 5xx is considered some type
of failure and checking with "status" gives
back from return code 552 just 5 is
sufficent to determine that there is some
failure. Although I found that "code"
returns the full code 552.

Thanks again Clay.
James R. Ferguson
Acclaimed Contributor

Re: Perl Net::FTP and ls

Hi Jerry:

Your nterpretation that it is sufficient to only check the first digit of the 'ftp' return code is correct.

The first digit indicates whether the reply is good, bad, or incomplete (e.g. 5=not accepted).

The second digit indicates the functional area (e.g. 5=file system related information).

The third digit simply provides further clarification.

See the man pages for 'ftpd(1M)' for more information.

Regards!

...JRF...