File Coverage

blib/lib/Dpkg/Exit.pm
Criterion Covered Total %
statement 21 22 95.4
branch 3 4 75.0
condition n/a
subroutine 9 9 100.0
pod 3 3 100.0
total 36 38 94.7


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   71674 use strict;
  1         12  
  1         31  
20 1     1   5 use warnings;
  1         2  
  1         76  
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         1  
  1         326  
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 3     3 1 1924 my ($func) = shift;
55              
56 3 100       13 _setup_exit_handlers() if @handlers == 0;
57 3         7 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 8 _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 4     4 1 110 $_->() foreach (reverse @handlers);
79             }
80              
81             sub _exit_handler {
82 1     1   239 run_exit_handlers();
83 0         0 exit(127);
84             }
85              
86             my @SIGNAMES = qw(INT HUP QUIT __DIE__);
87             my %SIGOLD;
88              
89             sub _setup_exit_handlers
90             {
91 2     2   5 foreach my $signame (@SIGNAMES) {
92 8         41 $SIGOLD{$signame} = $SIG{$signame};
93 8         85 $SIG{$signame} = \&_exit_handler;
94             }
95             }
96              
97             sub _reset_exit_handlers
98             {
99 1     1   3 foreach my $signame (@SIGNAMES) {
100 4         35 $SIG{$signame} = $SIGOLD{$signame};
101             }
102             }
103              
104             =back
105              
106             =head1 CHANGES
107              
108             =head2 Version 2.00 (dpkg 1.20.0)
109              
110             Hide variable: @handlers.
111              
112             =head2 Version 1.01 (dpkg 1.17.2)
113              
114             New functions: push_exit_handler(), pop_exit_handler(), run_exit_handlers()
115              
116             Deprecated variable: @handlers
117              
118             =head2 Version 1.00 (dpkg 1.15.6)
119              
120             Mark the module as public.
121              
122             =cut
123              
124             1;