Skip to content

Instantly share code, notes, and snippets.

@belden
Created May 23, 2012 13:36
Show Gist options
  • Save belden/2775258 to your computer and use it in GitHub Desktop.
Save belden/2775258 to your computer and use it in GitHub Desktop.
Locally replace die to not.
#!/usr/bin/env perl
use strict;
use warnings;
use Want qw(rreturn);
use Test::Resub qw(resub);
BEGIN { *CORE::GLOBAL::die = sub { CORE::die(@_) } }
use Data::Dumper;
{
my @died;
sub died { return wantarray ? @died : scalar @died }
sub died_with { push @died, [@_] }
}
sub do_something_that_might_die {
die "toy example always dies";
}
{
my $rs_die = resub 'CORE::GLOBAL::die', sub { died_with(@_); rreturn undef };
my $out = do_something_that_might_die();
if (!defined $out && died) {
print "looks like we caught a die: " . Dumper(died);
}
}
do_something_that_might_die;
@belden
Copy link
Author

belden commented May 23, 2012

I think the core takeaway here is, "If you dislike eval-ing code that might die, you can simply invoke weirder bits of Perl to gloss away the eval altogether." But it's just hidden, not gone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment