| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$1 || $2
MC/DC100%2/2
$1 || $2
$1
$2
Truth table100%3/3
A || B
A= $1B= $2
| |||||||||||||||||||||||||||||
| 57 | 97 | ▶ | $id = uc( $1 ) if $msg =~ s/^([a-z0-9_]+)\s*:(?!:)\s*//i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 68 | { | ||||||||||||||||||||||||||||
| 69 | 167 | ▶ | last if $p ne __PACKAGE__; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 97 | ▶ | ( $p, $f, $l ) = caller() unless defined $p; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 98 | 19 | ▶ | return 0 unless $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 16 | ▶ | if( ! UNIVERSAL::isa( $@, 'Exception::Sink::Class' ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | { | ||||||||||||||||||||||||||||
| 101 | 5 | ▶ | print STDERR "dive: non-ship, resink: $@\n" if $DEBUG_SINK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | # re-sink, non-ship | ||||||||||||||||||||||||||||
| 103 | 5 | my $AT=$@; | |||||||||||||||||||||||||||
| 104 | 5 | ▶ | if( ref $AT ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 114 | }; | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 16 | ▶ | print STDERR "dive: $@->{CLASS}\n" if $DEBUG_SINK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 130 | 59 | ▶ | return 0 unless $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 52 | ▶ | return 1 unless @_; # catch all | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 51 | ▶ | if( ! UNIVERSAL::isa( $@, 'Exception::Sink::Class' ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | { | ||||||||||||||||||||||||||||
| 134 | 22 | ▶ | print STDERR "surface: non-ship, resink: $@\n" if $DEBUG_SINK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 151 | }; | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 51 | ▶ | print STDERR "surface: $@->{CLASS} -> @_?\n" if $DEBUG_SINK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 51 | for my $class ( @_ ) | |||||||||||||||||||||||||||
| 156 | { | ||||||||||||||||||||||||||||
| 157 | 56 | ▶ | return 1 if $class eq '*'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 50 | ▶ | return 1 if uc $class eq $@->{ 'CLASS' }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | 27 | ▶ | print STDERR "surface: $@->{CLASS} -> continuing...\n" if $DEBUG_SINK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 194 | 18 | ▶ | $skip = 0 if $skip < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 202 | { | ||||||||||||||||||||||||||||
| 203 | 37 | my $l = length( "$subname" ); | |||||||||||||||||||||||||||
| 204 | 37 | ▶ | $ml = $l if $l > $ml; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | ############################################################################## | ||||||||||||||||||||||||||||
| 221 | 4 | 1; | |||||||||||||||||||||||||||
| 222 | ############################################################################## | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | __END__ | ||||||||||||||||||||||||||||