Question

I have a SOAP client written in Perl using LWP package for the HTTPS transport and XML::Simple for the parsing of the XML payloads. From time to time the call to XMLin fails with a die() and then my script dies and has to be restarted by a monitoring program that I have written to detect this. This is really not desirable, and so I was wondering if Perl has any facility like the C++ exception handling mechanism where I can catch the die message, ignore it report the error and let my script continue just as if an error occurred? I have read a number of Perl books and looked for this but I have not managed to find something. This is killing my application but I dont want to write my own XML parsing code unless I absolutely have to.

Was it helpful?

Solution

Yes; the basic mechanism for doing this would be an eval:

sub a { die "BAD"; }
eval { a(); }
print "Survived an exception $@";

However, there are reasons why you should use more high-level constructs (which are nevertheless constructed on top of this), like Try::Tiny et al. (see the links at the bottom of its documentation).

OTHER TIPS

You can catch the "die" but you can't stop your script from dying by catching it: *When a "__DIE__" hook routine returns, the exception processing continues as it would have in the absence of the hook, unless the hook routine itself exits via a "goto", a loop exit, or a "die()".*

You can run the routines which are susceptible to call the die() inside an eval{} block, though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top