• Shaarli
  • Tag cloud
  • Picture wall
  • Daily
  • RSS
  • Login
4253 shaares
Filters

Perl Advent Calendar 2020 - There is No Try

QRCode

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';
};
http://perladvent.org/2020/2020-12-03.html
January 6, 2021 at 2:55:14 PM EST *
perl
FILLER
Shaarli · The personal, minimalist, super fast, database-free, bookmarking service by the Shaarli community · Documentation
Fold Fold all Expand Expand all Are you sure you want to delete this link? Are you sure you want to delete this tag? The personal, minimalist, super fast, database-free, bookmarking service by the Shaarli community