In simple scripts you probably won't have any special code to catch these exceptions. In bigger applications, when you start writing modules, you will probably already want to really throw exceptions, and then capture them using eval. We'll deal with that in another article. Collecting die calls In a slightly more advanced way, Perl provides a signal handle for die, just as it does for warn.
The big difference is that the signal handler that collects the die call does not stop your script from dieing. It is only interesting in the cases where you already catch the exception e. For those cases see the article about capturing die calls. Toggle navigation Perl Maven. Standard output, standard error and command line redirection Warning when something goes wrong What does die do? Unknown warnings category Can't use string Its very easy to trap errors in Perl and then handling them properly.
Here are few methods which can be used. The if statement is the obvious choice when you need to check the return value from a statement; for example:. Alternatively, we can reduce the statement to one line in situations where it makes sense to do so; for example:. The unless function is the logical opposite to if: statements can completely bypass the success status and only be executed if the expression returns false.
For example:. The unless statement is best used when you want to raise an error or alternative only if the expression fails. The statement also makes sense when used in a single-line statement:. It's not quite so clear here what we are trying to achieve, but the effect is the same as using an if or unless statement. As your handlers get more complicated I recommend writing them as named subroutines, although that carries additional risk which I will address in a later section.
Next, my routine explicitly returns 1. Remember that all Perl subroutines return something. If I hadn't returned 1, I would have been returning 1 or the empty string, depending on the success of the print statement. Yes, print can fail. Should you check your prints? But I digress. My point was that your subroutines should always explicitly return something.
If the subroutine is a 'void' method, have it return undef. So that covers the how, and some of the why, but doesn't give you the full potential. When a program runs into a bad situation, the programmer often has it call die.
This causes Perl to send out sigdie, and if a handler is defined it gets called. You can do this at compile time in 5. But this pragma is scoped so is strict , btw so it only makes YOUR warnings fatal. Yes, some modules redefine die instead of implementing a signal handler. So now onto sigdie. SigDie Catching the signal generated from die is good for one thing, and one thing only: debugging. Don't use it to clean up stuff. If all this OO talk has thrown you for a loop, read Tom's perltoot and the docs for bless.
I believe there are a few more OO resources in the monastery as well. Don't even try to recover from death abusing sigdie Ok, that's only partly true. See my discussion on eval below. So then, the purpose of catching sigdie is to gain more insight into why the program died. Somewhere in the code catacombs I printed a "trick" for getting a call stack easily.
Things are falling down everywhere, so don't make things complicated with subroutine calls and system activity. Sort of.
0コメント