Sink.pm

filter: Statementstmt 100% 92 / 92 Branchbran 92.1% 59 / 64 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 10 / 10 Podpod 100% 7 / 7 totaltotal 97.2% 176 / 181 CC 36 SCAR 21.8
CC
36
Cov
97%
surface
14.0
sink
9.0
dive
8.0
CRAP 8.9
↑ Summary Sink/Class.pm »
1 ##############################################################################
2 #
3 # Exception::Sink
4 # Copyright (c) 2006-2026 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 4 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 our @ISA = qw( Exporter );
18 4 our @EXPORT = qw(
19 sink
20 dive
21 surface
22 surface2
23  
24 boom_skip
25 boom
26 );
27  
28 4 our @EXPORT_OK = qw(
29 $DEBUG_SINK
30 get_stack_trace
31 );
32  
33 4 our %EXPORT_TAGS = ( 'none' => [ ] );
34 4 our $VERSION = '3.11';
35 4 ▶use Exception::Sink::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 4 our $DEBUG_SINK = 0;
39  
40 ##############################################################################
41 #
42 # sink( "CLASS: ID: message..." )
43 # sinks the ship of class and id with reason message
44 #
45  
46 sub sink($)
47 97 ▶{
Subroutine sink: called CC=9 SCAR=22.0
Pod documented
48 97 my $msg = shift;
49 97 ▶ $msg = '' unless defined $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg 1 96 97
50  
51 97 my $org = $msg;
52  
53 97 my $class = 'SINK';
54 97 my $id = 'UNKNOWN';
55  
56 97 ▶ $class = uc( $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 89 8 97
Condition100%3/3
$1 || $2
l!l&&r!l&&!r
8513
MC/DC100%2/2
$1 || $2
$1 $2
Truth table100%3/3
A || B
A= $1B= $2
ABresult
1X1
011
000
57 97 ▶ $id = uc( $1 ) if $msg =~ s/^([a-z0-9_]+)\s*:(?!:)\s*//i;
Branch: 1 branch
BranchTrueFalseTotal
if $msg =~ s/^([a-z0-9_]+)\s*:(?!:)\s*//i 24 73 97
58  
59 # strip perl die() location suffix: " at FILE line N." with optional ", <FH> line N."
60 97 $msg =~ s/\s+at\s+\S+\s+line\s+\d+(?:,\s*<[^>]*>\s+(?:line|chunk)\s+\d+)?\.?\s*$//;
61 97 chomp( $msg );
62  
63 # origin is the first frame outside this package, so boom() and re-sinks
64 # from dive()/surface() report the user's call site, not Sink.pm
65 97 my ( $p, $f, $l );
66 97 my $cl = 0;
67 97 ▶ while( ( $p, $f, $l ) = caller( $cl++ ) )
Branch: 1 branch
BranchTrueFalseTotal
if (() = caller $cl++) 167 0 167
68 {
69 167 ▶ last if $p ne __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $p ne __PACKAGE__ 97 70 167
70 }
71 97 ▶ ( $p, $f, $l ) = caller() unless defined $p;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p 0 97 97
72 97 $f =~ s/^(.*\/)([^\/]+)$/$2/;
73  
74 97 $class = uc $class;
75  
76 97 ▶ print STDERR "sink: $class ($f:$l)\n" if $DEBUG_SINK;
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Sink::DEBUG_SINK 1 96 97
77  
78 97 die Exception::Sink::Class->new(
79 'CLASS' => $class,
80 'ID' => $id,
81 'MSG' => $msg,
82 'PACKAGE' => $p,
83 'FILE' => $f,
84 'LINE' => $l,
85 'ORG' => $org,
86 );
87 }
88  
89 ##############################################################################
90 #
91 # dive()
92 # continue sinking...
93 #
94  
95 sub dive()
96 19 ▶{
Subroutine dive: called CC=8 SCAR=20.8
Pod documented
97 19 ▶ print STDERR "dive: pre: $@\n" if $DEBUG_SINK;
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Sink::DEBUG_SINK 1 18 19
98 19 ▶ return 0 unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 3 16 19
99 16 ▶ if( ! UNIVERSAL::isa( $@, 'Exception::Sink::Class' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($@, "Exception::Sink::Class")) 5 11 16
100 {
101 5 ▶ print STDERR "dive: non-ship, resink: $@\n" if $DEBUG_SINK;
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Sink::DEBUG_SINK 0 5 5
102 # re-sink, non-ship
103 5 my $AT=$@;
104 5 ▶ if( ref $AT )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $AT) { } 3 2 5
105 {
106 # foreign reference: keep its text intact, explicit ID stops parsing
107 3 eval { sink "SINK: UNKNOWN: $AT"; }
108 }
109 else
110 {
111 2 eval { sink "SINK: $AT"; }
112 }
113 5 ▶ $@->{ 'OBJ' } = $AT if ref $AT; # original foreign exception
Branch: 1 branch
BranchTrueFalseTotal
if ref $AT 3 2 5
114 };
115  
116 16 ▶ print STDERR "dive: $@->{CLASS}\n" if $DEBUG_SINK;
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Sink::DEBUG_SINK 0 16 16
117  
118 16 die; # propagate
119 }
120  
121 ##############################################################################
122 #
123 # surface( class list )
124 # stops sinking of specific classes...
125 #
126  
127 sub surface(@)
128 59 ▶{
Subroutine surface: called CC=14 SCAR=26.4
Pod documented
129 59 ▶ print STDERR "surface: enter: $@ -> @_\n" if $DEBUG_SINK;
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Sink::DEBUG_SINK 1 58 59
130 59 ▶ return 0 unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 7 52 59
131 52 ▶ return 1 unless @_; # catch all
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 51 52
132 51 ▶ if( ! UNIVERSAL::isa( $@, 'Exception::Sink::Class' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($@, "Exception::Sink::Class")) 22 29 51
133 {
134 22 ▶ print STDERR "surface: non-ship, resink: $@\n" if $DEBUG_SINK;
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Sink::DEBUG_SINK 0 22 22
135 # re-sink, non-ship
136 22 my $AT=$@;
137 22 ▶ if( $AT =~ /^[A-Z0-9_]+\s*:(?!:)/i ) # "CLASS: ..." prefix, but not "Pkg::Name=HASH(...)"
Branch: 2 branches
BranchTrueFalseTotal
if ($AT =~ /^[A-Z0-9_]+\s*:(?!:)/i) { } 7 15 22
elsif (ref $AT) { } 9 6 15
138 {
139 7 eval { sink $AT; }
140 }
141 elsif( ref $AT )
142 {
143 # foreign reference: keep its text intact, explicit ID stops parsing
144 9 eval { sink "SINK: UNKNOWN: $AT"; }
145 }
146 else
147 {
148 6 eval { sink "SINK: $AT"; }
149 }
150 22 ▶ $@->{ 'OBJ' } = $AT if ref $AT; # original foreign exception
Branch: 1 branch
BranchTrueFalseTotal
if ref $AT 10 12 22
151 };
152  
153 51 ▶ print STDERR "surface: $@->{CLASS} -> @_?\n" if $DEBUG_SINK;
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Sink::DEBUG_SINK 1 50 51
154  
155 51 for my $class ( @_ )
156 {
157 56 ▶ return 1 if $class eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "*" 6 50 56
158 50 ▶ return 1 if uc $class eq $@->{ 'CLASS' };
Branch: 1 branch
BranchTrueFalseTotal
if uc $class eq $@->{"CLASS"} 18 32 50
159 }
160 27 ▶ print STDERR "surface: $@->{CLASS} -> continuing...\n" if $DEBUG_SINK;
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Sink::DEBUG_SINK 1 26 27
161 27 return 0;
162 }
163  
164 sub surface2(@)
165 5 ▶{
Subroutine surface2: called CC=2 SCAR=6.9
Pod documented
166 5 ▶ return 1 if surface(@_);
Branch: 1 branch
BranchTrueFalseTotal
if &surface(@_) 2 3 5
167 3 dive();
168 1 return 0;
169 }
170  
171 ##############################################################################
172 #
173 # boom()
174 # sink with stack trace
175 #
176  
177 sub boom_skip($$)
178 10 ▶{
Subroutine boom_skip: called CC=1 SCAR=0.0
Pod documented
179 10 my $msg = shift;
180 10 my $skip = shift;
181 10 chomp( $msg );
182 10 $msg = "BOOM: [$$] $msg\n";
183 10 sink( join '', ( $msg, get_stack_trace( $skip ) ) );
184 }
185  
186 sub boom($)
187 5 ▶{
Subroutine boom: called CC=1 SCAR=0.0
Pod documented
188 5 boom_skip($_[0],1);
189 }
190  
191 sub get_stack_trace
192 18 ▶{
Subroutine get_stack_trace: called CC=7 SCAR=19.5
Pod documented
193 18 ▶ my $skip = shift || 0;
Condition100%2/2
shift() || 0
l!l
117
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
194 18 ▶ $skip = 0 if $skip < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $skip < 0 1 17 18
195  
196 18 my @st;
197 my $i;
198 18 my $ml = 0;
199  
200 18 $i = 1 + $skip; # skip get_stack_trace frame and optionally first N frames
201 18 ▶ while ( my ( $pack, $file, $line, $subname ) = caller($i++) )
Branch: 1 branch
BranchTrueFalseTotal
if (() = caller $i++) 37 18 55
202 {
203 37 my $l = length( "$subname" );
204 37 ▶ $ml = $l if $l > $ml;
Branch: 1 branch
BranchTrueFalseTotal
if $l > $ml 18 19 37
205 }
206  
207 18 $i = 1 + $skip; # skip get_stack_trace frame and optionally first N frames
208 18 my $ii;
209 18 ▶ while ( my ( $pack, $file, $line, $subname ) = caller($i++) )
Branch: 1 branch
BranchTrueFalseTotal
if (() = caller $i++) 37 18 55
210 {
211 37 $ii++;
212 37 my $l = length( "$subname" );
213 37 my $pad = ' ' x ( $ml - $l );
214 37 push @st, " [$$] $ii: $subname $pad $file line $line\n";
215 }
216  
217 18 ▶ return wantarray ? ( @st ) : join( '', @st );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 17 1 18
218 }
219  
220 ##############################################################################
221 4 1;
222 ##############################################################################
223  
224 __END__
↑ Summary Sink/Class.pm »