Created
May 23, 2012 13:36
-
-
Save belden/2775258 to your computer and use it in GitHub Desktop.
Locally replace die to not.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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; |
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
Output: