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

     Is there a way to get the error message that is returned if a $ftp->get or $ftp->put fails? I understand that these methods return undef if they fail.
     For example, I would like to know if a put failed because of "access denied" or "disk space" or something else.

Invulnerable. Unlimited XP. Unlimited Votes. I must be...
        GhodMode

Replies are listed 'Best First'.
Re: Net::FTP server errors?
by lestrrat (Deacon) on Oct 31, 2002 at 20:11 UTC

    Have you tried looking at $ftp->message()?

      ++lestrrat for the correct answer.

      In case you're wondering GhodMode, the trick is that the Net::FTP module has a 'ISA' relationship with Net::Cmd which means that in addition to the methods listed on the Net::FTP man page, you can also use the methods listed in the Net::Cmd man page.

      If you're checking whether a specific error occurred, you should use the $ftp->code() method which returns the three digit error code - this should be consistent for RFC compliant FTP servers whereas the text message may change. But if you just want to print out a diagnostic message when something goes wrong then $ftp->message() is what you want.

Re: Net::FTP server errors?
by Mr. Muskrat (Canon) on Oct 31, 2002 at 19:34 UTC

    If you set the Debug option to a true value, then the errors will go to STDERR.

    #!/usr/bin/perl use strict; use warnings; use Net::FTP; my $ftp = Net ::FTP->new("some.host.name", Debug => 1); $ftp->login("anonymous",'me@here.there'); $ftp->cwd("/pub"); $ftp->get("that.file"); $ftp->quit;
Re: Net::FTP server errors?
by robartes (Priest) on Oct 31, 2002 at 19:30 UTC
    Have a look in perlvar for the variables $!, $?, $@ and $^E. They are conveniently grouped together under the label "Error Indicators".

    $! is probably of most use to you:

    $ftp->get("/that/camel/off/my/back") or die "Blerch: $!\n";

    CU
    Robartes-

      Get is the only command that will return useful information in $!. If the file doesn't exist, it would print "Bad file descriptor at filename line x." to STDERR.

        Oops!

        my $ftp = Net::FTP->new("some.host.name") or die $!; would print "Invalid argument at filename line x." to STDERR.