Re: Dealing with exit in a Modulino



On Sat, Mar 29, 2008 at 04:44:28AM -0700, Marc Girod wrote:
The problem is then: how to return to the caller context
(when there is one, i.e. when I do not choose to exit),
instead of to the caller of my exit subroutine?

Some kind of exception mechanism.

Something like this (completely untested):


use Test::More tests => ...;

my $exitcode;
sub script_call(&) {
my $code_to_test = shift;
undef $exitcode;
return &$code_to_test();
EXIT_HOOK:
return;
}

BEGIN { *CORE::GLOBAL::exit = sub { $exitcode = shift; goto EXIT_HOOK } }

use Your::Script;
is(script_call { Your::Script::startup() }, "OK");
is($exitcode, undef, "didn't exit on startup);

script_call { Your::Script::shutdown() };
is($exitcode, 0, "exited on shutdown");

# calls to exit not laundered through script_code end up here
EXIT_HOOK: CORE::exit $exitcode;
.



Relevant Pages