in reply to Re: Net::FTP not catching errors, my fault?
in thread [solved] Net::FTP not catching errors, my fault?

thanks for your reply.

what I'm trying to do is to accomplish 5 steps and if any of them fail, fail the whole thing. in addition, when one of those five steps fails, log it...which should work because according to the Net::FTP documentation you can do the following:
    $ftp->binary or die "$ftp->message\n";
I'm trying to change that to:
    $ftp->binary or $log->logwarn("text", $ftp->message);

Here's the pseudo-code for what I want my end result to be:
if (login OR log error) AND (switch to binary mode OR log error) AND (change current diretory OR log error) AND (get/put file OR log error) AND (quit OR log error) return success else return failure

thanks,
ryanc

Replies are listed 'Best First'.
Re^3: Net::FTP not catching errors, my fault?
by polettix (Vicar) on Jun 06, 2005 at 15:55 UTC
    The pseudo-code is exactly what tphyahoo was pointing you at. Consider this:
    #!/usr/bin/perl use strict; use warnings; sub fails { 0 } sub logs { print "error!\n"; 1 } (fails or logs) and print "all ok so far...\n"; __END__ ~/sviluppo/perl> perl example.pl error! all ok so far...
    As you can see, you have to beware of the return value for your log function, otherwise it will happily turn an error into something good. You could use some exception handling here:
    #!/usr/bin/perl use strict; use warnings; sub fails { 0 } sub logs { print "error: @_"; 1 } eval { 1 or die "some message here for first part"; fails or die "some message here for second part"; }; if ($@) { logs($@); exit 1; # i.e. return failure } # Otherwise all ok... __END__ ~/sviluppo/perl> perl example.pl error: some message here for second part at example.pl line 11.

    Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

    Don't fool yourself.
Re^3: Net::FTP not catching errors, my fault?
by derby (Abbot) on Jun 06, 2005 at 15:39 UTC
    Dumb question ... what's your logwarn returning? If it returns a true value you continue on to the next condition ...
    if ( change current dir OR log error ) AND ... if ( 0 OR 1 ) AND ... if( 1 ) AND ...

    Personally, I would wrap the code up in an eval:

    eval { $ftp->login($data->{'login'}, $data->{'pw'}) or die($ftp->message); $ftp->binary or die($ftp->message); $ftp->cwd($data->{'remote_dir'}) or die($ftp->message); $ftp->put($to_upload) or die($ftp->message); $ftp->quit; }; if( $@ ) { $log->error("upload_file: xqe $data->{'id'}, " . $@); return undef; } else { return 1; }
    -derby
Re^3: Net::FTP not catching errors, my fault?
by reasonablekeith (Deacon) on Jun 06, 2005 at 16:08 UTC
    As has been pointed out, if logwarn returns true, then all the ftp commands are going to be run regardless (as each condition checked by the and's are going to be true).

    As you print the same log info each time, you could try something like the following, which avoids the constant line-noise log writing.

    if ($ftp->login($data->{'login'}, $data->{'pw'}) and $ftp->binary and $ftp->cwd($data->{'remote_dir'}) and $ftp->put($to_upload) and $ftp->quit) { return 1; } else { $log->logwarn("upload_file: xqe $data->{'id'}, ", $ftp->message) $log->error("upload_file: xqe $data->{'id'}, " . "upload failed fo +r $to_upload"); return undef; }
    ---
    my name's not Keith, and I'm not reasonable.