in reply to Self resurrecting perl scripts

Ok, why do you want to restart your script, or let it die ?
Try to put yout Net::FTP part into an eval block:
eval { CODE WHAT TO DO || die; }; print $@;
So that script in the eval block will fail, you can avoid the die of your main program. Just check the Code in $@.
If all went well, ok, else doitagain Sam :-)

Just a question to your SIG{__DIE__}: Is this the same like an END{} Block ?

UPDATE: added a ; behind the eval block ;-)

----------------------------------- --the good, the bad and the physi-- -----------------------------------

Replies are listed 'Best First'.
Re: Re: Self resurrecting perl scripts
by KM (Priest) on Apr 11, 2001 at 19:18 UTC
    Just a question to your SIG{__DIE__}: Is this the same like an END{} Block ?

    END happens after the die..

    #!/usr/bin/perl -w $SIG{__DIE__} = sub { print "Dead\n"; }; die "Blah"; END { print "This is the end, my friend.\n"; }

    Outputs:

    Dead blah at ./foo.pl line 5. This is the end, my friend.

    But, you need to define the SIG before you use it. The END will happen regardless. For example:

    #!/usr/bin/perl -w die "Blah"; $SIG{__DIE__} = sub { print "Dead\n"; }; END { print "This is the end, my friend.\n"; }

    Will output:

    Blah at ./foo.pl line 3. This is the end, my friend.

    Cheers,
    KM

Re: Re: Self resurrecting perl scripts
by traveler (Parson) on Apr 11, 2001 at 19:48 UTC
    This is what I do and what I usually find. Note, however, that $@ is the only help you get about what failed. eval retuns undef if it died. That means you have to parse $@ to find out why it died (useful if it was a system event that killed it), or you can set a package or global variable to tell you why it died. I tend to do the latter whenever possible.

    traveler

Re: Re: Self resurrecting perl scripts
by RhetTbull (Curate) on Apr 12, 2001 at 00:51 UTC
    Try to put yout Net::FTP part into an eval block:
    eval { CODE WHAT TO DO || die; }; print $@;
    Thanks, that's exactly what I needed! The Net::FTP code was actually dieing (I didn't call die) so I needed to trap the die and verify that it died because it was timing out. Putting the call to "Net::FTP->get()" in an eval block let me trap that. I didn't know I could do that with eval. Now I just need to look at $@ to see if the Net::FTP Timeout is there. Thanks again!

    As for your question about SIG(__DIE__), KM's response basically sums it up. The END block gets executed last and no matter what caused the script to end. However, when the script dies, I want to know so I can do some extra clean up (log the errors, close files, etc) that doesn't need to happen when the script executes normally.

    Update:

    Added a ; after eval block. :-)