blib/lib/UR/Exit.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 121 SCAR 56.1
CC
16
Cov
0%
death
90.0
warning
56.0
clean_exit
6.0
CRAP 272.0
1 package UR::Exit;
2  
3 =pod
4  
5 =head1 NAME
6  
7 UR::Exit - methods to allow clean application exits.
8  
9 =head1 SYNOPSIS
10  
11 UR::Exit->exit_handler(\&mysub);
12  
13 UR::Exit->clean_exit($value);
14  
15 =head1 DESCRIPTION
16  
17 This module provides the ability to perform certain operations before
18 an application exits.
19  
20 =cut
21  
22 # set up module
23 require 5.006_000;
24 use warnings;
25 use strict;
26 require UR;
27 our $VERSION = "0.46"; # UR $VERSION;;
28 our (@ISA, @EXPORT, @EXPORT_OK);
29  
30 require Exporter;
31 @ISA = qw(Exporter);
32 @EXPORT = qw();
33 @EXPORT_OK = qw();
34  
35 use Carp;
36  
37  
38 =pod
39  
40 =head1 METHODS
41  
42 These methods provide exit functionality.
43  
44 =over 4
45  
46 =item exit_handler
47  
48 UR::Exit->exit_handler(\&mysub);
49  
50 Specifies that a given subroutine be run when the application exits.
51 (Unimplimented!)
52  
53 =cut
54  
55 sub exit_handler
56 {
57 die "Unimplimented";
58 }
59  
60 =pod
61  
62 =item clean_exit
63  
64 UR::Exit->clean_exit($value);
65  
66 Exit the application, running all registered subroutines.
67 (Unimplimented! Just exits the application directly.)
68  
69 =cut
70  
71 sub clean_exit
72 {
73 my $class = shift;
74 my ($value) = @_;
75 $value = 0 unless defined($value);
76 exit($value);
77 }
78  
79 =pod
80  
81 =item death
82  
83 Catch any die or warn calls. This is a universal place to catch die
84 and warn if debugging.
85  
86 =cut
87  
88 sub death
89 {
90 unless ($ENV{'UR_STACK_DUMP_ON_DIE'}) {
91 return;
92 }
93  
94 # workaround common error
95 if ($_[0] =~ /Can.*t upgrade that kind of scalar during global destruction/)
96 {
97 exit 1;
98 }
99  
100 if (defined $^S) {
101 # $^S is defined when perl is executing (as opposed to interpreting)
102 if ($^S) {
103 # $^S is true when its executing in an eval, false outside of one
104 return;
105 }
106 } else {
107 # interpreter is parsing a module or string eval
108 # check the call stack depth for up-stream evals
109 # fall back to perls default handler if there is one
110 my $call_stack_depth = 0;
111 for (1) {
112 my @details = caller($call_stack_depth);
113 #print Data::Dumper::Dumper(\@details);
114 last if scalar(@details) == 0;
115  
116 if ($details[1] =~ /\(eval .*\)/) {
117 #print "<no carp due to eval string>";
118 return;
119 }
120 elsif ($details[3] eq "(eval)") {
121 #print "<no carp due to eval block>";
122 return;
123 }
124 $call_stack_depth++;
125 redo;
126 }
127 }
128  
129 if
130 (
131 $_[0] =~ /\n$/
132 and UNIVERSAL::can("UR::Context::Process","is_initialized")
133 and defined(UR::Context::Process->is_initialized)
134 and (UR::Context::Process->is_initialized == 1)
135 )
136 {
137 # Do normal death if there is a newline at the end, and all other
138 # things are sane.
139 return;
140 }
141 else
142 {
143 # Dump the call stack in other cases.
144 # This is a developer error occurring while things are
145 # initializing.
146 local $Carp::CarpLevel = 1;
147 Carp::confess(@_);
148 return;
149 }
150 }
151  
152 =pod
153  
154 =item warning
155  
156 Give more informative warnings.
157  
158 =cut
159  
160 sub warning
161 {
162  
163 unless ($ENV{'UR_STACK_DUMP_ON_WARN'}) {
164 warn @_;
165 return;
166 }
167  
168 return if $_[0] =~ /Attempt to free unreferenced scalar/;
169 return if $_[0] =~ /Use of uninitialized value in exit at/;
170 return if $_[0] =~ /Use of uninitialized value in subroutine entry at/;
171 return if $_[0] =~ /One or more DATA sections were not processed by Inline/;
172 UR::ModuleBase->warning_message(@_);
173 if ($_[0] =~ /Deep recursion on subroutine/)
174 {
175 print STDERR "Forced exit by UR::Exit on deep recursion.\n";
176 print STDERR Carp::longmess("Stack tail:");
177 exit 1;
178 }
179 return;
180 }
181  
182 #$SIG{__DIE__} = \&death unless ($SIG{__DIE__});
183 #$SIG{__WARN__} = \&warning unless ($SIG{__WARN__});
184  
185 sub enable_hooks_for_warn_and_die {
186 $SIG{__DIE__} = \&death;
187 $SIG{__WARN__} = \&warning;
188 }
189  
190 &enable_hooks_for_warn_and_die();
191  
192  
193 1;
194 __END__