Sink.pm

filter: Statementstmt 20.2% 16 / 79 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 30.0% 3 / 10 Podpod 100% 7 / 7 totaltotal 17.6% 26 / 147 CC 26 SCAR 42.5
CC
26
Cov
12%
surface
156.0
dive
42.0
sink
30.0
CRAP 69.8
↑ Summary Sink/Class.pm »
1 ##############################################################################
2 #
3 # Exception::Sink
4 # Copyright (c) 2006-2024 Vladi Belperchinov-Shabanski "Cade"
5 # <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org>
6 # http://cade.noxrun.com/
7 #
8 # GPLv2
9 #
10 ##############################################################################
11 #
12 # compact general purpose exception handling
13 #
14 ##############################################################################
15 package Exception::Sink;
16 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 our @ISA = qw( Exporter );
18 1 our @EXPORT = qw(
19 sink
20 dive
21 surface
22 surface2
23
24 boom_skip
25 boom
26 );
27  
28 1 our @EXPORT_OK = qw(
29 $DEBUG_SINK
30 get_stack_trace
31 );
32
33 1 our %EXPORT_TAGS = ( 'none' => [ ] );
34 1 our $VERSION = '3.09';
35 1 ▶use Exception::Sink::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 1 our $DEBUG_SINK = 0;
39  
40 sub sink($);
41 sub dive();
42 sub surface(@);
43 sub surface2(@);
44  
45 sub boom_skip($$);
46 sub boom($);
47  
48 ##############################################################################
49 #
50 # sink( "CLASS: ID: message..." )
51 # sinks the ship of class and id with reason message
52 #
53  
54 sub sink($)
55 {
56 0 ▶ my $msg = shift;
Subroutine sink: not called CC=5 SCAR=34.0
Pod documented
57  
58 0 my $org = $msg;
59  
60 0 my $class = 'UNKNOWN';
61 0 my $id = 'UNKNOWN';
62  
63 0 ▶ $class = $1 || $2 if $msg =~ s/^([a-z0-9_]+)\s*:\s*|^([a-z0-9_]+)$//i;
Branch: 1 branch
BranchTrueFalseTotal
if $msg =~ s/^([a-z0-9_]+)\s*:\s*|^([a-z0-9_]+)$//i 0 0 0
Condition0%0/3
$1 || $2
l!l&&r!l&&!r
000
MC/DC0%0/2
$1 || $2
$1 $2
Truth table0%0/3
A || B
A= $1B= $2
ABresult
1X1
011
000
64 0 ▶ $id = $1 if $msg =~ s/^([a-z0-9_]+)\s*:\s*|^([a-z0-9_]+)$//i;
Branch: 1 branch
BranchTrueFalseTotal
if $msg =~ s/^([a-z0-9_]+)\s*:\s*|^([a-z0-9_]+)$//i 0 0 0
65  
66 0 $msg =~ s/\s+at\s+\/\S+.+$//;
67 0 chomp( $msg );
68  
69 0 my ( $p, $f, $l ) = caller();
70 0 $f =~ s/^(.*\/)([^\/]+)$/$2/;
71  
72 0 $class = uc $class;
73  
74 0 ▶ print STDERR "sink: $class ($f:$l)\n" if $DEBUG_SINK;
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Sink::DEBUG_SINK 0 0 0
75  
76 0 die Exception::Sink::Class->new(
77 'CLASS' => $class,
78 'ID' => $id,
79 'MSG' => $msg,
80 'PACKAGE' => $p,
81 'FILE' => $f,
82 'LINE' => $l,
83 'ORG' => $org,
84 );
85 }
86  
87 ##############################################################################
88 #
89 # dive()
90 # continue sinking...
91 #
92  
93 sub dive()
94 {
95 0 ▶ print STDERR "dive: pre: $@\n" if $DEBUG_SINK;
Subroutine dive: not called CC=6 SCAR=37.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Sink::DEBUG_SINK 0 0 0
96 0 ▶ return 0 unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
97 0 ▶ if( !ref($@) )
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $@) 0 0 0
98 {
99 0 ▶ print STDERR "dive: non-ship, resink: $@\n" if $DEBUG_SINK;
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Sink::DEBUG_SINK 0 0 0
100 # re-sink, non-ship
101 0 my $AT=$@;
102 0 eval { sink "SINK: $AT"; }
103 };
104  
105 0 ▶ print STDERR "dive: $@->{CLASS}\n" if $DEBUG_SINK;
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Sink::DEBUG_SINK 0 0 0
106  
107 0 die; # propagate
108 }
109  
110 ##############################################################################
111 #
112 # surface( class list )
113 # stops sinking of specific classes...
114 #
115  
116 sub surface(@)
117 {
118 0 ▶ print STDERR "surface: enter: $@ -> @_\n" if $DEBUG_SINK;
Subroutine surface: not called CC=12 SCAR=50.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Sink::DEBUG_SINK 0 0 0
119 0 ▶ return 0 unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
120 0 ▶ return 1 unless @_; # catch all
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
121 0 ▶ if( !ref($@) )
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $@) 0 0 0
122 {
123 0 ▶ print STDERR "surface: non-ship, resink: $@\n" if $DEBUG_SINK;
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Sink::DEBUG_SINK 0 0 0
124 # re-sink, non-ship
125 0 my $AT=$@;
126 0 ▶ if( $AT =~ /^[A-Z0-9_]+\:/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($AT =~ /^[A-Z0-9_]+\:/) { } 0 0 0
127 {
128 0 eval { sink $AT; }
129 }
130 else
131 {
132 0 eval { sink "SINK: $AT"; }
133 }
134 };
135  
136 0 ▶ print STDERR "surface: $@->{CLASS} -> @_?\n" if $DEBUG_SINK;
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Sink::DEBUG_SINK 0 0 0
137  
138 0 for my $class ( @_ )
139 {
140 0 ▶ return 1 if $class eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "*" 0 0 0
141 0 ▶ return 1 if uc $class eq $@->{ 'CLASS' };
Branch: 1 branch
BranchTrueFalseTotal
if uc $class eq $@->{"CLASS"} 0 0 0
142 }
143 0 ▶ print STDERR "surface: $@->{CLASS} -> continuing...\n" if $DEBUG_SINK;
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Sink::DEBUG_SINK 0 0 0
144 0 return 0;
145 }
146  
147 sub surface2(@)
148 {
149 0 ▶ return 1 if surface(@_);
Subroutine surface2: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if &surface(@_) 0 0 0
150 0 dive();
151 0 return 0;
152 }
153  
154 ##############################################################################
155 #
156 # boom()
157 # sink with stack trace
158 #
159  
160 sub boom_skip($$)
161 {
162 0 ▶ my $msg = shift;
Subroutine boom_skip: not called CC=1 SCAR=6.9
Pod documented
163 0 my $skip = shift;
164 0 chomp( $msg );
165 0 $msg = "BOOM: [$$] $msg\n";
166 0 sink( join '', ( $msg, get_stack_trace( $skip ) ) );
167 }
168  
169 sub boom($)
170 {
171 0 ▶ boom_skip($_[0],0);
Subroutine boom: not called CC=1 SCAR=6.9
Pod documented
172 }
173  
174 sub get_stack_trace
175 {
176 0 ▶ my $skip = shift;
Subroutine get_stack_trace: not called CC=5 SCAR=34.0
Pod documented
177
178 0 my @st;
179 my $i;
180 0 my $ml;
181
182 0 $i = 1 + $skip; # skip get_stack_trace frame and optionally first N frames
183 0 ▶ while ( my ( $pack, $file, $line, $subname ) = caller($i++) )
Branch: 1 branch
BranchTrueFalseTotal
if (() = caller $i++) 0 0 0
184 {
185 0 my $l = length( "$pack::$subname" );
186 0 ▶ $ml = $l if $l > $ml;
Branch: 1 branch
BranchTrueFalseTotal
if $l > $ml 0 0 0
187 }
188
189 0 $i = 1 + $skip; # skip get_stack_trace frame and optionally first N frames
190 0 my $ii;
191 0 ▶ while ( my ( $pack, $file, $line, $subname ) = caller($i++) )
Branch: 1 branch
BranchTrueFalseTotal
if (() = caller $i++) 0 0 0
192 {
193 0 $ii++;
194 0 my $l = length( "$pack::$subname" );
195 0 my $pad = ' ' x ( $ml - $l );
196 0 push @st, " [$$] $ii: $pack::$subname $pad $file line $line\n";
197 }
198
199 0 ▶ return wantarray ? ( @st ) : join( '', @st );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
200 }
201  
202 ##############################################################################
203 1 1;
204 ##############################################################################
205  
206 __END__
↑ Summary Sink/Class.pm »