1#############################################################################
2# test-script.pl -- simple testing script for AtExit.pm
3#
4# Copyright (c) 1996 Andrew Langmead. All rights reserved.
5# This file is part of "AtExit". AtExit is free software;
6# This is free software; you can redistribute it and/or modify it under
7# the terms of the Artistic License 1.0.
8#############################################################################
9
10use strict;
11use warnings;
12
13use AtExit;
14
15sub cleanup {
16    my @args = @_;
17    print "cleanup() executing: args = @args\n";
18}
19
20## Register subroutines to be called when this program exits
21
22$_ = atexit(\&cleanup, "This call was registered first");
23print "first call to atexit() returned value of type ", ref($_), "\n";
24
25$_ = atexit("cleanup", "This call was registered second");
26print "second call to atexit() returned value of type ", ref($_), "\n";
27
28$_ = atexit("cleanup", "This call should've been unregistered by rmexit");
29rmexit($_)  or  warn "couldnt' unregister exit-sub $_!";
30
31if (@ARGV == 0) {
32   ## Register subroutines to be called when this lexical scope is exited
33   my $scope1 = AtExit->new( \&cleanup, "Scope 1, Callback 1" );
34   {
35      ## Do the same for this nested scope
36      my $scope2 = AtExit->new;
37      $_ = $scope2->atexit( \&cleanup, "Scope 2, Callback 1" );
38      $scope1->atexit( \&cleanup, "Scope 1, Callback 2");
39      $scope2->atexit( \&cleanup, "Scope 2, Callback 2" );
40      $scope2->rmexit($_) or warn "couldn't unregister exit-sub $_!";
41
42      print "*** Leaving Scope 2 ***\n";
43      #$scope2->rmexit();
44    }
45    print "*** Finished Scope 2 ***\n";
46    print "*** Leaving Scope 1 ***\n";
47}
48print "*** Finished Scope 1 ***\n"  if (@ARGV == 0);
49##rmexit();
50END {
51    print "*** Now performing program-exit processing ***\n";
52}
53
54exit 0;
55