http://www.perlmonks.org?node_id=763143


in reply to Re^7: Modern Perl Programming Highs and Lows
in thread Modern Perl Programming Highs and Lows

Well... you neglected to account for DESTROY { eval {} } and DESTROY { die } for any objects created in your test code. You failed to account for buggy overloading in exception objects. To do all of that correctly you've got to use the return value of the eval{} to decide failure, hook and unhook $SIG{__DIE__}, save $@ so when you stringify it, you don't accidentally cause it to clobber itself.

Perl exception handling is actually a garden of pain.

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Replies are listed 'Best First'.
Re^9: Modern Perl Programming Highs and Lows
by educated_foo (Vicar) on May 10, 2009 at 22:45 UTC
    Those things you mention fall into the "doctor, it hurts when I do this" category. Normal code shouldn't need DESTROY at all, and if it does, it should be extremely careful. And if you're creating complex overloaded exception objects, you have already lost. What does your test module's overloaded $SIG{__DIE__} do with a module that itself overloads $SIG{__DIE__}?

    Sure, it's possible to build ever-more-complicated exception mechanisms with lots of corner cases, and to require more testing modules to control them. Sometimes you may even be forced into this silliness. But since you're inviting a world of hurt by doing so, you should ask yourself first whether a simpler solution could work.

      No, none of this is "it hurts doctor" stuff. It's quite normal to have exception objects. It's normal for exception objects to have stringification. It's unusual but not not bad to have a destructor. It is bad to fail to protect your caller against $@ clobbering but mistakes can happen. It's pretty unusual to see your tests $SIG{__DIE__} get overridden and it's worth automating the instrumentation of that happening. Mostly, I really like Test::Exception because with perl, you must be paranoid and this lets me outsource my paranoia.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊