On Jun 1, 10:48 am, [EMAIL PROTECTED] (Ben Edwards) wrote:
> I have a perl script that uses FTP. If the ftp server can not be
> conected to I want to write to a log file and exit with a return code
> of 1.
>
> Something like
>
> my $ftp = Net::FTP->new( $remote_host ) or {
> print LOGFILE "failed to connect to $remote_host\n"
> return(1);
> };
>
> But this does not work. So how do I do 2 statements for an or?
Before I give you the answer, allow me to strongly suggest you abandon
this algorithm, as it leads to bulky confusing code. My preferred way
of seeing this would be:
my $ftp = Net::FTP->new($remote_host);
if (! $ftp ) {
print LOGFILE "failed to connect to $remote_host\n";
return 1;
}
Even more preferred would be to throw an exception, and let the
calling code deal with it:
my $ftp = Net::FTP->new($remote_host) or
die "failed to connect to $remote_host\n";
Then in whatever code calls this command:
eval { connect_to_ftp() };
if ($@) {
print LOGFILE $@;
exit -1;
}
However, to answer your actual question: you need a do{} block.
my $ftp = Net::FTP->new( $remote_host ) or do {
print LOGFILE "failed to connect to $remote_host\n"
return(1);
};
Note that someone else may suggest you do the following:
my $ftp = Net::FTP->new( $remote_host ) or
print LOGFILE "failed to connect to $remote_host\n" and
return(1);
I strongly recommend you do NOT do that. That will only execute the
return() statement if the print() is successful. And especially since
you're printing to a non-STDOUT filehandle, that's not at all a
certainty.
Paul Lalli
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/