Other operating systems may handle this case differently. Because Perl uses the underlying operating system, you must be familiar with the quirks and gotchas that apply there too. In case you think it unlikely you’d write a program that created files and then removed them in the same invocation, think about temporary storage or checkpoint/restart capability. 9.3 Taking Exception to Yourself Having discussed how we handle exceptions generated by Perl, it’s time to talk about how we make exceptions of our own. You’re already used to the most basic exception-generating method, which is to call die. The arguments are printed, and your program exits with a nonzero return code. (Just which nonzero return code is documented in gory detail under perldoc -f die.) You may see some additional text output by die, such as the line number of code it died on. If instead it says (eval 13), it’s telling you that it was executing the thirteenth call to eval and prints the line number relative to the evaled code, not the program containing the eval. If it says chunk 42, it means that you had the filehandle FOO open and had made 42 calls to the readlinefunction (or <>) on it. If you didn’t change the $/ variable so that readline read more than one line at a time, newer Perls will say line 42instead. Maybe you’re tired of tacking ‘ or die…’ onto every system call and just want it to happen automatically. Well, as of version 5.005_03, you can use Lionel Cons and Ilya Zakharevich’s Fatal.pm and just specify for which routines you want this magical behavior to occur. Just list them in the use statement: use Fatal qw(open mkdir chdir); The routines have to be overridable, which is not something easily determined by mortals (translation: you have to look at the source). Bottom line: to see if it works, try it out on any core function you want to use it on first. Note that while you can do this for any function (including your own), it’s not useful for the ones like chmod, which return the number of files modified (unless you’re only ever going to modify one file at a time). Although you can’t use it on the execor systemcalls (for esoteric reasons), if you could, it wouldn’t be any good on system because that call returns 0 in the event of success. 9.4 Playing Catch-Up Let’s say that you want to go in the other direction and instead of turning errors into exceptions, you want to know when your program is about to die. The brute-force approach is to define a pseudo signal handler for __DIE__: $SIG{__DIE__} = sub { print “Igor lives!n” };
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services
This entry was posted
on Monday, October 30th, 2006 at 11:08 pm and is filed under perl.
You can follow any responses to this entry through the RSS 2.0 feed.
Responses are currently closed, but you can trackback from your own site.