Monday, October 19, 2009

Try Try::Tiny for easier Perl 5 exception handling

Perl 5 exception handling is not an easy thing. For one thing, it makes use of the eval construct. Any Perl code that uses eval for exception handling directly moves a notch closer to a "write-only" mode. Basically, you have to figure out whether the eval means dynamic code compilation or exception handling.

This is the reason that there are is a good number of exception handling libraries on CPAN.

There are a number of worthy contenders for the exception handling crown, but for minimalist reasons, I like Try::Tiny. First of all, it doesn't try to do too much. Secondly, it covers the cases where using eval/if becomes difficult in practice. The use of $@ pretty much dominates the difficulty in using eval/if. In a nutshell, $@ must be localized to prevent clobbering previous values. At the same time, all die statements should have the proper effect.

Try::Tiny is currently at version 0.02. For the few times that I will need to write in Perl, this is definitely a the top of my list for exception handling.

No comments: