paxdiablo
paxdiablo

Reputation: 881163

Error reason from Perl Net::FTP

I'm using Net::FTP to transfer files up to a mainframe and I'm testing failure conditions.

My code is basically along the following lines:

    my $ftp = Net::FTP->new ("mainframe.com", Timeout => 20);
    if (! $ftp) {
            logMessage ("Could not connect to host: $!");
            return;
    }

    if (! $ftp->login ("paxdiablo", "demigodemporeroftheuniverse")) {
            logMessage ("Could not log in to host: $!");
            $ftp->quit ();
            return;
    }

    if (! $ftp->put ("myfile.txt", "'CANT.WRITE.TO.THIS'")) {
            logMessage ("Could not put file: $!");
            $ftp->quit ();
            return;
    }

I know I can't create the data set CANT.WRITE.TO.THIS since I don't have the required permissions but, when I try, the only message I see is:

Could not put file:

There is no indication in $! as to what the problem was. I've looked in the Net::FTP doco and all it says is:

put ( LOCAL_FILE [, REMOTE_FILE ] )

Put a file on the remote server. LOCAL_FILE may be a name or a filehandle. If LOCAL_FILE is a filehandle then REMOTE_FILE must be specified. If REMOTE_FILE is not specified then the file will be stored in the current directory with the same leafname as LOCAL_FILE.

Returns REMOTE_FILE or the generated remote filename if REMOTE_FILE is not given.

I also cannot find anything there about retrieving the specific error (like $ftp->getLastError() or something similar).

How can I indicate to the user why the transfer failed?

On an earlier iteration, I resorted to putting the file, then getting it again and checking contents locally. I'd really rather not have to inflict such kludgy code on people again.

Upvotes: 1

Views: 3343

Answers (1)

Sinan Ünür
Sinan Ünür

Reputation: 118118

From Net::FTP:

$ftp = Net::FTP->new("some.host.name", Debug => 0)
    or die "Cannot connect to some.host.name: $@"

Note $@.

$ftp->cwd("/pub")
    or die "Cannot change working directory ", $ftp->message;

Note $ftp->message

Upvotes: 5

Related Questions