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-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 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.10';
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 ##############################################################################
41 #
42 # sink( "CLASS: ID: message..." )
43 # sinks the ship of class and id with reason message
44 #
45  
46 sub sink($)
47 {
48 0 ▶ my $msg = shift;
Subroutine sink: not called CC=5 SCAR=34.0
Pod documented
49  
50 0 my $org = $msg;
51  
52 0 my $class = 'UNKNOWN';
53 0 my $id = 'UNKNOWN';
54  
55 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
56 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
57  
58 0 $msg =~ s/\s+at\s+\/\S+.+$//;
59 0 chomp( $msg );
60  
61 0 my ( $p, $f, $l ) = caller();
62 0 $f =~ s/^(.*\/)([^\/]+)$/$2/;
63  
64 0 $class = uc $class;
65  
66 0 ▶ print STDERR "sink: $class ($f:$l)\n" if $DEBUG_SINK;
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Sink::DEBUG_SINK 0 0 0
67  
68 0 die Exception::Sink::Class->new(
69 'CLASS' => $class,
70 'ID' => $id,
71 'MSG' => $msg,
72 'PACKAGE' => $p,
73 'FILE' => $f,
74 'LINE' => $l,
75 'ORG' => $org,
76 );
77 }
78  
79 ##############################################################################
80 #
81 # dive()
82 # continue sinking...
83 #
84  
85 sub dive()
86 {
87 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
88 0 ▶ return 0 unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
89 0 ▶ if( !ref($@) )
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $@) 0 0 0
90 {
91 0 ▶ print STDERR "dive: non-ship, resink: $@\n" if $DEBUG_SINK;
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Sink::DEBUG_SINK 0 0 0
92 # re-sink, non-ship
93 0 my $AT=$@;
94 0 eval { sink "SINK: $AT"; }
95 };
96  
97 0 ▶ print STDERR "dive: $@->{CLASS}\n" if $DEBUG_SINK;
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Sink::DEBUG_SINK 0 0 0
98  
99 0 die; # propagate
100 }
101  
102 ##############################################################################
103 #
104 # surface( class list )
105 # stops sinking of specific classes...
106 #
107  
108 sub surface(@)
109 {
110 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
111 0 ▶ return 0 unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
112 0 ▶ return 1 unless @_; # catch all
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
113 0 ▶ if( !ref($@) )
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $@) 0 0 0
114 {
115 0 ▶ print STDERR "surface: non-ship, resink: $@\n" if $DEBUG_SINK;
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Sink::DEBUG_SINK 0 0 0
116 # re-sink, non-ship
117 0 my $AT=$@;
118 0 ▶ if( $AT =~ /^[A-Z0-9_]+\:/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($AT =~ /^[A-Z0-9_]+\:/) { } 0 0 0
119 {
120 0 eval { sink $AT; }
121 }
122 else
123 {
124 0 eval { sink "SINK: $AT"; }
125 }
126 };
127  
128 0 ▶ print STDERR "surface: $@->{CLASS} -> @_?\n" if $DEBUG_SINK;
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Sink::DEBUG_SINK 0 0 0
129  
130 0 for my $class ( @_ )
131 {
132 0 ▶ return 1 if $class eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "*" 0 0 0
133 0 ▶ return 1 if uc $class eq $@->{ 'CLASS' };
Branch: 1 branch
BranchTrueFalseTotal
if uc $class eq $@->{"CLASS"} 0 0 0
134 }
135 0 ▶ print STDERR "surface: $@->{CLASS} -> continuing...\n" if $DEBUG_SINK;
Branch: 1 branch
BranchTrueFalseTotal
if $Exception::Sink::DEBUG_SINK 0 0 0
136 0 return 0;
137 }
138  
139 sub surface2(@)
140 {
141 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
142 0 dive();
143 0 return 0;
144 }
145  
146 ##############################################################################
147 #
148 # boom()
149 # sink with stack trace
150 #
151  
152 sub boom_skip($$)
153 {
154 0 ▶ my $msg = shift;
Subroutine boom_skip: not called CC=1 SCAR=6.9
Pod documented
155 0 my $skip = shift;
156 0 chomp( $msg );
157 0 $msg = "BOOM: [$$] $msg\n";
158 0 sink( join '', ( $msg, get_stack_trace( $skip ) ) );
159 }
160  
161 sub boom($)
162 {
163 0 ▶ boom_skip($_[0],0);
Subroutine boom: not called CC=1 SCAR=6.9
Pod documented
164 }
165  
166 sub get_stack_trace
167 {
168 0 ▶ my $skip = shift;
Subroutine get_stack_trace: not called CC=5 SCAR=34.0
Pod documented
169  
170 0 my @st;
171 my $i;
172 0 my $ml = 0;
173  
174 0 $i = 1 + $skip; # skip get_stack_trace frame and optionally first N frames
175 0 ▶ while ( my ( $pack, $file, $line, $subname ) = caller($i++) )
Branch: 1 branch
BranchTrueFalseTotal
if (() = caller $i++) 0 0 0
176 {
177 0 my $l = length( "$subname" );
178 0 ▶ $ml = $l if $l > $ml;
Branch: 1 branch
BranchTrueFalseTotal
if $l > $ml 0 0 0
179 }
180  
181 0 $i = 1 + $skip; # skip get_stack_trace frame and optionally first N frames
182 0 my $ii;
183 0 ▶ while ( my ( $pack, $file, $line, $subname ) = caller($i++) )
Branch: 1 branch
BranchTrueFalseTotal
if (() = caller $i++) 0 0 0
184 {
185 0 $ii++;
186 0 my $l = length( "$subname" );
187 0 my $pad = ' ' x ( $ml - $l );
188 0 push @st, " [$$] $ii: $subname $pad $file line $line\n";
189 }
190  
191 0 ▶ return wantarray ? ( @st ) : join( '', @st );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
192 }
193  
194 ##############################################################################
195 1 1;
196 ##############################################################################
197  
198 __END__
↑ Summary Sink/Class.pm »