On 1/26/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
snip
> ok I read the perldoc and this seems to be the ideal way, but when any
> condition fails, meaning
> a connection fails, a login fails or a cwd fails I want to update the log
> with the appropriate message for each failed
> condition.  Using this ONE way your way will not do this for me instead I
> will have to write THREE of these blocks  once for each failure...
> UNLESS I can put an if in this routine like so:
>
>      my $olddie = $SIG{__DIE__};
>      $SIG{__DIE__} =
>
>      sub {
>          my $error = shift;
>          $olddie->{$error} if ref $olddie;
>
>
>          #other error stuff as below
>           if ($RC == 0) {
>               print FTPLOG message about connection
>               mailme($error);
>           }
>           elif {
>                ($RC == 1) {
>                print FTPLOG message about login
>                mailme($error);
>           }
>           else {
>                ($RC == 2) {
>                print FTPLOG message about cwd
>                mailme($error);
>      };
>
> Here would be the ftp calls
>
> my $ftp = Net::FTP->new($remotehost, Debug => 10) or
>     die  "Cannot connect to $remotehost: $!", my $RC=0;
>
> $ftp->login($user, $passs ) or die "Login failed $!", my $RC=1;
>
> $ftp->cwd ($remotedir) or die "CWD failed $!", my $RC=2
>
> Does this make sense logically?
snip

I believe you are slightly confused about what is going on when you
register the error handler.  It is my fault for not commenting the
code.

my $old_error_handler = $SIG{__DIE__}; #get the error handler
currently assigned to die()

# assign an anonymous function to the error handler for die
# we are using an anonymous function instead of a normal function
# to get around needing a global variable to hold the old error handler
# for more information run a google search for closures and perl
$SIG{__DIE__} = sub {
    my $error = shift; #$error now holds the message passed to die()

    #if $old_error_handler is set then call it
    $old_error_handler->() if ref $old_error_handler eq 'CODE';

    #you should consider using one of the logging modules instead
    open FTPLOG, '>>', "/path/to/log";
    print FTPLOG $error;
    close FTPLOG;

    mailme($error);
};

#stuff happend

my $ftp = Net::FTP->new($remotehost, Debug => 10) or
    die  "Cannot connect to $remotehost: $!";
$ftp->login($user, $passs ) or die "Login as $user on $remotehost failed $!";
$ftp->cwd ($remotedir) or die "Could not change directory to
$remotedirfailed $!";

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to