costas has asked for the wisdom of the Perl Monks concerning the following question:

I am looking for a way to capture any errors caused when using Net::FTP. At the moment I hard code any errors using the or die function but what I really want is to capture the proper error message.

I dont seem to see any error handling in the NEt::FTP docs and I have so far been unsuccesful in using the Net:Cmd module.

Can anybody help?
#Example scenario #Move to destination directory FROM root $ftp->cwd(/home/costas) or die "cannot find root directory";
Would prefer something like
$ftp->cwd(/home/costas) or die "$ERROR";

Replies are listed 'Best First'.
Re: error handling with Net::FTP
by mla (Beadle) on Apr 04, 2002 at 14:42 UTC
    Net::FTP inherits from Net::Cmd, so you should be able to do something like:

    $ftp->cwd('/home/costas') or die "cwd failed: [", $ftp->code, "] ", $f +tp->message;
Re: error handling with Net::FTP
by DigitalKitty (Parson) on Apr 04, 2002 at 14:31 UTC
    Hi Costas,

    I would use something like this:

    #!/usr/bin/perl -w use strict; use Net::FTP; my $ftp = Net::FTP->new("ftp.host.name") or die("Couldn't connect: $@\n"); $ftp->login("Username"); $ftp->cwd("/pub/scripts/networking"); $ftp->get("ping.pl"); $ftp->close;

    The error will be stored in the special $@ variable.

    Hope this helps,

    Thanks,
    -DigitalKitty
Re: error handling with Net::FTP
by jlongino (Parson) on Apr 04, 2002 at 17:55 UTC
    Just for the sake of clarity, both responses above are correct but it appears as though each should be used in the context given and are not interchangeable (at least from what I could determine from some hasty testing). Use   $@ when testing the result of the constructor method and $ftp->code, $ftp->message when checking the other methods. Please let me know if there are further exceptions.

    This thread was informative for me since when I checked to see how I had handled this in my programs, I found that I had neglected to test the result of the constructor method call (tested everything else though). Thanks!

    --Jim