Exit.pm 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Copyright © 2002 Adam Heath <doogie@debian.org>
  2. # Copyright © 2012-2013 Guillem Jover <guillem@debian.org>
  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 <https://www.gnu.org/licenses/>.
  16. package Dpkg::Exit;
  17. use strict;
  18. use warnings;
  19. our $VERSION = '1.01';
  20. our @EXPORT_OK = qw(
  21. push_exit_handler
  22. pop_exit_handler
  23. run_exit_handlers
  24. );
  25. use Exporter qw(import);
  26. # XXX: Backwards compatibility, stop exporting on VERSION 2.00.
  27. ## no critic (Variables::ProhibitPackageVars)
  28. our @handlers = ();
  29. ## use critic
  30. =encoding utf8
  31. =head1 NAME
  32. Dpkg::Exit - program exit handlers
  33. =head1 DESCRIPTION
  34. The Dpkg::Exit module provides support functions to run handlers on exit.
  35. =head1 FUNCTIONS
  36. =over 4
  37. =item push_exit_handler($func)
  38. Register a code reference into the exit function handlers stack.
  39. =cut
  40. sub push_exit_handler {
  41. my ($func) = shift;
  42. push @handlers, $func;
  43. }
  44. =item pop_exit_handler()
  45. Pop the last registered exit handler from the handlers stack.
  46. =cut
  47. sub pop_exit_handler {
  48. pop @handlers;
  49. }
  50. =item run_exit_handlers()
  51. Run the registered exit handlers.
  52. =cut
  53. sub run_exit_handlers {
  54. &$_() foreach (reverse @handlers);
  55. }
  56. sub _exit_handler {
  57. run_exit_handlers();
  58. exit(127);
  59. }
  60. $SIG{INT} = \&_exit_handler;
  61. $SIG{HUP} = \&_exit_handler;
  62. $SIG{QUIT} = \&_exit_handler;
  63. =back
  64. =head1 CHANGES
  65. =head2 Version 1.01 (dpkg 1.17.2)
  66. New functions: push_exit_handler(), pop_exit_handler(), run_exit_handlers()
  67. Deprecated variable: @handlers
  68. =head2 Version 1.00 (dpkg 1.15.6)
  69. Mark the module as public.
  70. =cut
  71. 1;