Created
July 23, 2009 21:08
-
-
Save arodland/153599 to your computer and use it in GitHub Desktop.
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
# Set up the hook | |
my $exit_handler = sub { | |
print "Bang!\n"; | |
}; | |
#And install it | |
BEGIN { | |
*CORE::GLOBAL::exit = sub { $exit_handler->(@_) }; | |
} | |
# Do code that we don't really want to exit | |
exit 1; | |
# The closest thing to an un-override we can do | |
$exit_handler = sub { | |
CORE::exit $_[0]; | |
}; | |
# This will exit for real | |
exit 2; |
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
use Devel::OverrideExit; | |
use Test::More tests => 1; | |
{ | |
my $exits = 0; | |
my $guard = Devel::OverrideExit->new(sub { | |
$exits++; | |
} | |
); | |
# Standin for the code that should exit: | |
exit 1; | |
is($exits, 1, "Called exit once"); | |
} |
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
package Devel::OverrideExit; | |
use strict; | |
use warnings; | |
my $exit_handler = sub { | |
CORE::exit $_[0]; | |
}; | |
BEGIN { | |
*CORE::GLOBAL::exit = sub { $exit_handler->(@_) }; | |
} | |
sub new { | |
my ($class, $exit) = @_; | |
my $self = bless { saved => $exit_handler }, $class; | |
$exit_handler = $exit; | |
return $self; | |
} | |
sub DESTROY { | |
$exit_handler = shift->{saved}; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment