File Coverage

blib/lib/Dpkg/Exit.pm
Criterion Covered Total %
statement 24 25 96.0
branch 3 4 75.0
condition n/a
subroutine 10 10 100.0
pod 3 3 100.0
total 40 42 95.2


line stmt bran cond sub pod time code
1             # Copyright © 2002 Adam Heath
2             # Copyright © 2012-2013 Guillem Jover
3             #
4             # This program is free software; you can redistribute it and/or modify
5             # it under the terms of the GNU General Public License as published by
6             # the Free Software Foundation; either version 2 of the License, or
7             # (at your option) any later version.
8             #
9             # This program is distributed in the hope that it will be useful,
10             # but WITHOUT ANY WARRANTY; without even the implied warranty of
11             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12             # GNU General Public License for more details.
13             #
14             # You should have received a copy of the GNU General Public License
15             # along with this program. If not, see .
16              
17             package Dpkg::Exit;
18              
19 1     1   72119 use strict;
  1         12  
  1         29  
20 1     1   6 use warnings;
  1         3  
  1         111  
21              
22             our $VERSION = '2.00';
23             our @EXPORT_OK = qw(
24             push_exit_handler
25             pop_exit_handler
26             run_exit_handlers
27             );
28              
29 1     1   7 use Exporter qw(import);
  1         2  
  1         355  
30              
31             my @handlers = ();
32              
33             =encoding utf8
34              
35             =head1 NAME
36              
37             Dpkg::Exit - program exit handlers
38              
39             =head1 DESCRIPTION
40              
41             The Dpkg::Exit module provides support functions to run handlers on exit.
42              
43             =head1 FUNCTIONS
44              
45             =over 4
46              
47             =item push_exit_handler($func)
48              
49             Register a code reference into the exit function handlers stack.
50              
51             =cut
52              
53             sub push_exit_handler {
54 5     5 1 2429 my ($func) = shift;
55              
56 5 100       19 _setup_exit_handlers() if @handlers == 0;
57 5         14 push @handlers, $func;
58             }
59              
60             =item pop_exit_handler()
61              
62             Pop the last registered exit handler from the handlers stack.
63              
64             =cut
65              
66             sub pop_exit_handler {
67 1 50   1 1 7 _reset_exit_handlers() if @handlers == 1;
68 1         3 pop @handlers;
69             }
70              
71             =item run_exit_handlers()
72              
73             Run the registered exit handlers.
74              
75             =cut
76              
77             sub run_exit_handlers {
78 5     5 1 111 while (my $handler = pop @handlers) {
79 4         440 $handler->();
80             }
81 4         432 _reset_exit_handlers();
82             }
83              
84             sub _exit_handler {
85 1     1   238 run_exit_handlers();
86 0         0 exit(127);
87             }
88              
89             my @SIGNAMES = qw(INT HUP QUIT);
90             my %SIGOLD;
91              
92             sub _setup_exit_handlers
93             {
94 3     3   6 foreach my $signame (@SIGNAMES) {
95 9         29 $SIGOLD{$signame} = $SIG{$signame};
96 9         103 $SIG{$signame} = \&_exit_handler;
97             }
98             }
99              
100             sub _reset_exit_handlers
101             {
102 5     5   10 foreach my $signame (@SIGNAMES) {
103 15         215 $SIG{$signame} = $SIGOLD{$signame};
104             }
105             }
106              
107             END {
108 1     1   576 run_exit_handlers();
109             }
110              
111             =back
112              
113             =head1 CHANGES
114              
115             =head2 Version 2.00 (dpkg 1.20.0)
116              
117             Hide variable: @handlers.
118              
119             =head2 Version 1.01 (dpkg 1.17.2)
120              
121             New functions: push_exit_handler(), pop_exit_handler(), run_exit_handlers()
122              
123             Deprecated variable: @handlers
124              
125             =head2 Version 1.00 (dpkg 1.15.6)
126              
127             Mark the module as public.
128              
129             =cut
130              
131             1;