Question

After figuring out (via SO, of course) that the error for a bad $ftp = Net::FTP->new() call is in $@ while subsequent errors can be obtained by $ftp->message(), I'm striking a small problem.

My code is basically:

while (1) {
    # Wait for cycle start, then get file list into @filelist.

    foreach $file (@filelist) {
        my $ftp = Net::FTP->new ($host);
        if (! $ftp) {
            logError ("Could not connect to host [$host]: $@");
            return;
        }
        # More FTP stuff below with $ftp->message() error checking.
        $ftp->quit();
    }
}

Aside: yes, I know I can probably do this in one FTP session, but there are good reasons for leaving it in separate sessions at the moment.

Now this is being called in a loop, once per file, all going to the same host, but I'm getting a slightly different behaviour on the first attempt in most cycles. The script is a long-running one, with each cycle starting on the hour and half hour so it's not some issue with the first ever attempt after program start, since it happens on cycles other than the first as well.

Now I know that these connections should fail, simply because the machines I'm trying to access are not available on my development network.

The trouble is that the errors coming out in the log file are:

E 2012-02-05 18:00:13 Could not connect to host [example.com]: 
E 2012-02-05 18:00:13 Could not connect to host [example.com]:
    Net::FTP: connect: Connection refused
E 2012-02-05 18:00:14 Could not connect to host [example.com]:
    Net::FTP: connect: Connection refused

As you can see, the $@ variable seems to be not populated the first file of the cycle. I've edited this question slightly since I've just noticed the latest cycle had all three lines with the error message. Going back over the logs with the command:

grep refused logfile | awk '{print substr($3,1,5)}' | uniq -c

to get the dates and counts, turns up the following statistics:

  3 11:00
  3 11:30
  3 12:00
  3 12:30
  3 13:00
  3 13:30
  2 14:00
  3 14:30
  3 15:00
  3 15:30
  3 16:00
  2 16:30
  2 17:00
  2 17:30
  2 18:00
  2 18:30
  2 19:00
  3 19:30

indicating that some have the correct count of error messages but not all.

I'm wondering if anyone knows why this may be the case.

Était-ce utile?

La solution

Try upgrading http://cpansearch.perl.org/src/GBARR/libnet-1.22_01/Changes says

libnet 1.22_01 -- Mon May 31 09:40:25 CDT 2010
*Set $@ when ->new returns undef

If you're using a version of libnet prior to 1.22_01, it had a small bug in the new function in regards to responses that didn't start with a code.

For example, FTP.pm 2.77 which is from libnet 1.21 has the following snippet:

unless ($ftp->response() == CMD_OK) {
    $ftp->close();
    $@ = $ftp->message;
    undef $ftp;
}

With FTP.pm 2.77_2 from libnet 1.22_01, this is changed to:

unless ($ftp->response() == CMD_OK) {
    $ftp->close();
    # keep @$ if no message. Happens, when response did not start with a code.
    $@ = $ftp->message || $@;
    undef $ftp;
}

Autres conseils

Is there anything going on between the ->new call and printing the $@? It can overwrite the value of $@, so if it is neccesary, store the value for later use:

my $ftp = Net::FTP->new ($host);
my $potential_error = $@;

$whatever_that->can_call(eval => 'inside');

if (! $ftp) {
        logError ("Could not connect to host [$host]: $potential_error");
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top