Skip to content

Instantly share code, notes, and snippets.

@arodland
Created July 23, 2009 21:08
Show Gist options
  • Save arodland/153599 to your computer and use it in GitHub Desktop.
Save arodland/153599 to your computer and use it in GitHub Desktop.
# 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;
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");
}
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