@node AtExit, AutoLoader, Archie, Module List
@unnumbered Register a subroutine to be invoked at program-exit time.


@unnumberedsec NAME

atexit -- Register a subroutine to be invoked at program-exit time.

rmexit -- Unregister a subroutine that was registered with atexit.

@unnumberedsec SYNOPSIS

@example
use AtExit;

sub cleanup @{
    my @@args = @@_;
    print "cleanup() executing: args = @@args\n";
@}

$_ = atexit(\&cleanup, "This call was registered first");
print "first call to atexit() returned $_\n";
@end example

@example
$_ = atexit("cleanup", "This call was registered second");
print "second call to atexit() returned $_\n";
@end example

@example
$_ = atexit("cleanup", "This call should@'ve been unregistered by rmexit");
rmexit($_)  ||  warn "couldnt@' unregister exit-sub $_!";
@end example

@example
END @{
    print "*** Now performing program-exit processing ***\n";
@}
@end example

@unnumberedsec DESCRIPTION

The @strong{AtExit} module provides ANSI-C style exit processing modeled after
the @code{atexit()} function in the standard C library (see @samp{atexit(3C)} in this node).
Various exit processing routines may be registered by calling
@code{atexit()} and passing it the desired subroutine along with any
desired arguments. Then, at program-exit time, the subroutines registered
with @code{atexit()} are invoked with their given arguments in the
reverse order of registration (last one registered is invoked first).
Registering the same subroutine more than once will cause that subroutine
to be invoked once for each registration.

The @code{atexit()} function exported by @strong{AtExit} should be passed a
subroutine name or reference, optionally followed by the list of
arguments with which to invoke it at program-exit time.  Anonymous
subroutine references passed to @code{atexit()} act as "closures" (which are
described in @xref{(perl.info)perlref,Perlref},).  If a subroutine @emph{name} is specified (as
opposed to a subroutine reference) then, unless the subroutine name has
an explicit package prefix, it is assumed to be the name of a subroutine
in the caller@'s current package.  A reference to the specified
subroutine is obtained, and, if invocation arguments were specified, it
is "wrapped up" in a closure which invokes the subroutine with the
specified arguments.  The resulting subroutine reference is prepended to
the front of the @code{@@AtExit::EXIT_SUBS} list of exit-handling subroutines
and the reference is then returned to the caller (just in case you might
want to unregister it later using @code{rmexit()}). If the given subroutine could
@emph{not} be registered, then the value zero is returned.

The @code{rmexit()} function exported by @strong{AtExit} should be passed one or
more subroutine references, each of which was returned by a previous
call to @code{atexit()}. For each argument given, @code{rmexit()} will look for
it in the @code{@@AtExit::EXIT_SUBS} list of exit-handling subroutines and
remove the first such match from the list. The value returned will be
the number of subroutines that were successfully unregistered.

At program-exit time, the @code{END@{@}} block in the @strong{AtExit} module
iterates over the subroutine references in the @code{@@AtExit::EXIT_SUBS}
array and invokes each one in turn (each subroutine is removed from the
front of the queue immediately before it is invoked).  Note that the
subroutines in this queue are invoked in first-to-last order (the
reverse order in which they were registered with @code{atexit()}).

@unnumberedsubsec Invoking @code{atexit()} and @code{rmexit()} during program-exit

The variable @code{$AtExit::IGNORE_WHEN_EXITING} specifies how calls to
@code{atexit()} will be handled if they occur during the time that
subroutines registered with @code{atexit()} are being invoked.  By default,
this variable is set to a non-zero value, which causes @code{atexit()} to
@emph{ignore} any calls made to it during this time (a value of zero
will be returned). This behavior is consistent with that of the standard
C library function of the same name. If desired however, the user may
enable the registration of subroutines by @code{atexit()} during this time
by setting @code{$AtExit::IGNORE_WHEN_EXITING} to zero or to the empty
string. Just remember that any subroutines registered with @code{atexit()}
during program-exit time will be placed at the @emph{front} of the queue of
yet-to-be-invoked exit-processing subroutines.

Regardless of when it is invoked, @code{rmexit()} will @emph{always} attempt to
unregister the given subroutines (even when called during program-exit
processing). Keep in mind however that if it is invoked during program-exit
processing then it will @emph{fail} to unregister any exit-processing
subroutines that have @emph{already been invoked} by the @code{END@{@}} block in
the @strong{AtExit} module (since those subroutine calls have already been
removed from the @code{@@AtExit::EXIT_SUBS} list).

The variable @code{$AtExit::EXITING} may be examined to determine if
routines registered using @code{atexit()} are currently in the process of
being invoked. It will be non-zero if they are and zero otherwise.

@unnumberedsec NOTES

The usual Perl way of doing exit processing is through the use of
@code{END@{@}} blocks (see @samp{"Package Constructors and Destructors"}, @xref{(perl.info)perlmod,Perlmod},).
The @strong{AtExit} module implements its exit processing with an @code{END@{@}}
block that invokes all the subroutines registered by @code{atexit()} in the
array @code{@@AtExit::EXIT_SUBS}.  If any other @code{END@{@}} block processing is
specified in the user@'s code or in any other packages it uses, then the
order in which the exit processing takes place is subject to Perl@'s
rules for the order in which @code{END@{@}} blocks are processed. This may
affect when subroutines registered with @code{atexit()} are invoked with
respect to other exit processing that is to be performed. In particular,
if @code{atexit()} is invoked from within an @code{END@{@}} block that executes
@emph{after} the @code{END@{@}} block in the @strong{AtExit} module, then the corresponding
subroutine that was registered will never be invoked by the @strong{AtExit}
module@'s exit-processing code.

@unnumberedsubsec @code{END@{@}} block processing order

@code{END@{@}} blocks, including those in other packages, get called in the
reverse order in which they appear in the code. (@code{atexit()} subroutines
get called in the reverse order in which they are registered.) If a
package gets read via "use", it will act as if the @code{END@{@}} block was
defined at that particular part of the "main" code.  Packages read via
"require" will be executed after the code of "main" has been parsed and
will be seen last so will execute first (they get executed in the
context of the package in which they exist).

It is important to note that @code{END@{@}} blocks only get called on normal
termination (which includes calls to @code{die()} or @code{Carp::croak()}). They
do @emph{not} get called when the program terminates @emph{abnormally} (due to a
signal for example) unless special arrangements have been made by the
programmer (e.g. using a signal handler -- see @samp{"%SIG@{expr@}"}, @xref{(perl.info)perlvar,Perlvar},).

@unnumberedsec SEE ALSO

@samp{atexit(3C)} in this node describes the @code{atexit()} function for the standard C
library (the actual Unix manual section in which it appears may differ
from platform to platform - try sections 3C, 3, 2C, and 2).  Further
information on anonymous subroutines ("closures") may be found in
@xref{(perl.info)perlref,Perlref},.  For more information on @code{END@{@}} blocks, see
@samp{"Package Constructors and Destructors"}, @xref{(perl.info)perlmod,Perlmod},.  See
@samp{"%SIG@{expr@}"}, @xref{(perl.info)perlvar,Perlvar}, for handling abnormal program termination.

@unnumberedsec AUTHOR

Andrew Langmead <aml@@world.std.com> (initial draft).

Brad Appleton <Brad_Appleton-GBDA001@@email.mot.com> (final version).

