From: <jam...@te...> - 2002-09-18 12:08:56
|
> Unfortunately, miniserv already defines an END function that is uses for > a different purpose .. Is it possible to have more than one though, in different > packages? Yeah, you can do that. It is using is probably using at_exit() under the hood to register callback to be performed at exit(). These callbacks are put on stack of functions to be called at exit time. The one issue I see is that you are using the "do" or "require" statement to pull in your libraries, so what happens is that though you would end up calling the END routine, since the "require" is doing everything in the same namespace as the script when the calling script's END subroutine gets parsed it redefines the one that was created in the library. So unfortunatley, doing what I said is not possible at first glance (it would be if web-lib.pl was web-lib.pm, and called as use web-lib). You can still get there with your current architecture though. What you would have to do is create a function in the web-lib.pl, that registers callbacks to be ran at exit(). It would create a global (could be lexically private with file scope) list of callbacks. This list would then get ran in web-lib.pl's END function. Then the END fucntion in the miniserv.pl program would just need a name change, and be registered via the registration function. Here is a quick dirty example: <<<web-lib.pl>>> # # Instantiate the private END callback stack my @END_callbacks = (); sub register_END { my $call_back = shift; push(@END_callbacks, $call_back); } sub END { # # Run callbacks foreach my $callback (@END_callbacks) { &{$callback}(); } # # Do all the other web-lib things... } <<<miniserv.pl>>> # # Somewhere at the begining you register your miniserv.pl end # function. register_END(\&miniserv_END); Cheers...james |