Wednesday, January 27, 2010

Try::Tiny - 'Lightweight' try{} catch{} finally{}

As a lot of people will know error handling in Perl normally amounts to

my $val = eval { do_something_evil()};
die "Something evil happened: $@" if $@;

Not too much of a problem here except this (in some very odd cases) will not correctly trap the error and it means writing cleanup code can be hard (whilst trying not to clobber the value of $@ at the same time). This is not an easy thing to do so enter Try::Tiny which is a fantastic module giving you full try catch & finally support in Perl. Meaning the above code looks more like

my $val = try {
do_something_evil();
}
catch {
die "Something evil happened: $_";
};

Since I am a Java programmer first this syntax looks more natural to me. What is really impressive though is that finally support has now been added allowing us to do:

my $val = try {
do_something_evil();
}
catch {
die "Something evil happened: $_";
}
finally {
warn 'But first maybe some cleanup';
};

This is a perfect tool for those situations where local just does not cut it (say resetting a value in an object no matter what happens to the code).

Anyway next time you want to do error handling in Perl have a look at Try::Tiny; you won't regret it (and if you're wondering why I'm recommending this have a look at Try::Tiny's commit history on github & you may see why).

No comments: