Viki@Stag has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I need help on this. I have read the documentation of Net::FTP, it says for put() " NOTE: If for some reason the transfer does not complete and an error is returned then the contents that had been transferred will not be remove automatically. " What is the error returned? And how can I check it in the code? I currently call put() in a loop with retries, here is a part of the code:
my $ftp_obj = get_ftp_obj(); my $put_try = 0; my $cflag = 0; my $MAX_RETRIES = 5; while ($put_try < $MAX_RETRIES) { my $ret; $ret = $ftp_obj->put("$file_name") if ($put_try == 0); $ret = $ftp_obj->append("$file_name") if ($put_try > 0); last if (!$ret); # is this correct???? $put_try++; print "Retrying to append $file_name, $put_try\n"; if ($put_try == $MAX_RETRIES) { die "Not able to copy $file_name\n"; } }
Am i checking the return value of the put()/append() properly?
Thanks in advance

Replies are listed 'Best First'.
Re: NET::FTP put()/append() error handling
by pc88mxer (Vicar) on Jul 02, 2008 at 05:24 UTC
    For starters, declaring my $ret twice in the same scope isn't going to work as expected. Consider this:
    sub myohmy { my $y = shift; my $x = "true" if ($y); my $x = "not true" if (!$y); return $x; } print "myohmy(0) = ", myohmy(0), "\n"; print "myohmy(1) = ", myohmy(1), "\n"; __END__ # outputs: myohmy(0) = not true myohmy(1) =
    use warnings will catch this problem. To fix it you'll need to re-write it as:
    my $ret; $ret = ... if ($put_try == 0); $ret = ... if ($put_try > 0);
    but I'd probably just use an if...else... construct.

    Another issue with your retry logic is that if you want to recover from a failed put by appending data, you need to:

    1. figure out how much data was transfered by the put operation.
    2. open the source file and seek to that location
    3. perform an append operation using the opened file handle (as opposed to the source file's name)
    It might be easier just to re-put the file (depending on its size.)
      Thanks for the logic... ++
      since this file is a binary file, should i use binmode(FILEHANDLE)?
        It wouldn't hurt, especially if you are on a Windows system. Net::FTP uses read() to read from the local file during a put.
Re: NET::FTP put()/append() error handling
by Mr. Muskrat (Canon) on Jul 02, 2008 at 18:18 UTC

    What is the error returned? And how can I check it in the code?

    Use $ftp->message to get the error. See the Net::FTP synopsis for an example.