Perl Advent Calendar 2020 - There is No Try
eval Stinks!
Even if not a huge increase in the code needed for the operation, it's a bunch of magic to remember every time. If you don't do this sort of thing, though, you've got a big opening for horrible error-handling problems.
use Try::Tiny;
my $start = Time::HiRes::time;
return try {
$self->computation_helper->compute_stuff;
} catch {
my $error = $_ eq '' ? 'unknown error' : "$_";
$self->set_last_error( "couldn't get stuff computed: $error" );
return undef;
} finally {
my $failed = @_;
my $end = Time::HiRes::time;
warn sprintf "took %f seconds to %s",
$end - $start,
$failed ? 'fail' : 'succeed';
};
January 6, 2021 at 2:55:14 PM EST
*
FILLER