Exit.pm 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. use Exporter qw(import);
  21. our @EXPORT_OK = qw(push_exit_handler pop_exit_handler run_exit_handlers);
  22. # XXX: Backwards compatibility, stop exporting on VERSION 2.00.
  23. ## no critic (Variables::ProhibitPackageVars)
  24. our @handlers = ();
  25. ## use critic
  26. =encoding utf8
  27. =head1 NAME
  28. Dpkg::Exit - program exit handlers
  29. =head1 DESCRIPTION
  30. The Dpkg::Exit module provides support functions to run handlers on exit.
  31. =head1 FUNCTIONS
  32. =over 4
  33. =item push_exit_handler($func)
  34. Register a code reference into the exit function handlers stack.
  35. =cut
  36. sub push_exit_handler {
  37. my ($func) = shift;
  38. push @handlers, $func;
  39. }
  40. =item pop_exit_handler()
  41. Pop the last registered exit handler from the handlers stack.
  42. =cut
  43. sub pop_exit_handler {
  44. pop @handlers;
  45. }
  46. =item run_exit_handlers()
  47. Run the registered exit handlers.
  48. =cut
  49. sub run_exit_handlers {
  50. &$_() foreach (reverse @handlers);
  51. }
  52. sub exit_handler {
  53. run_exit_handlers();
  54. exit(127);
  55. }
  56. $SIG{INT} = \&exit_handler;
  57. $SIG{HUP} = \&exit_handler;
  58. $SIG{QUIT} = \&exit_handler;
  59. =back
  60. =head1 CHANGES
  61. =head2 Version 1.01
  62. New functions: push_exit_handler(), pop_exit_handler(), run_exit_handlers()
  63. Deprecated variable: @handlers
  64. =cut
  65. 1;