blib/lib/Glitch.pm

filter: Statementstmt 92.7% 90 / 97 Branchbran 68.7% 33 / 48 Conditioncond 45.4% 15 / 33 MC/DCmcdc 11.7% 2 / 17 Subroutinesub 70.3% 19 / 27 Podpod 20.0% 1 / 5 totaltotal 70.4% 160 / 227 CC 44 SCAR 18.3
CC
44
Cov
78%
_stack
12.4
_build_glitch
10.4
_log
9.8
CRAP 6.2
↑ Summary
1 package Glitch;
2 4 our $VERSION = '0.05';
3 4 ▶use 5.006; use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Data::Dumper; use feature qw/state/;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 state (%GLITCHES, $LOGGER, $LOGGER_ENABLED, $LOGGER_FORMAT, $LOGGER_METHOD, %STRINGIFY, $STRINGIFY_FORMAT);
6  
7 BEGIN {
8 4 ▶ $Data::Dumper::Deparse = 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 }
10  
11 sub import {
12 5 ▶ my ($pkg, %import) = @_;
Subroutine import: called CC=9 SCAR=22.2
13 5 ▶ if (keys %import) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %import) 2 3 5
14 2 ▶ if ($import{glitch_logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($import{"glitch_logger"}) 1 1 2
15 1 $LOGGER = delete $import{glitch_logger};
16 1 $LOGGER_ENABLED = !!1;
17 }
18 2 ▶ $LOGGER_ENABLED = delete $import{glitch_logger_enabled} if defined $import{glitch_logger_enabled};
Branch: 1 branch
BranchTrueFalseTotal
if defined $import{"glitch_logger_enabled"} 0 2 2
19 2 ▶ $LOGGER_FORMAT = delete $import{glitch_logger_format} if defined $import{glitch_logger_format};
Branch: 1 branch
BranchTrueFalseTotal
if defined $import{"glitch_logger_format"} 1 1 2
20 2 ▶ $LOGGER_METHOD = delete $import{glitch_logger_method} if defined $import{glitch_logger_method};
Branch: 1 branch
BranchTrueFalseTotal
if defined $import{"glitch_logger_method"} 0 2 2
21 2 ▶ %STRINGIFY = (%STRINGIFY, %{ delete $import{glitch_stringify} }) if defined $import{glitch_stringify};
Branch: 1 branch
BranchTrueFalseTotal
if defined $import{"glitch_stringify"} 0 2 2
22 2 ▶ $STRINGIFY_FORMAT = delete $import{glitch_stringify_format} if defined $import{glitch_stringify_format};
Branch: 1 branch
BranchTrueFalseTotal
if defined $import{"glitch_stringify_format"} 0 2 2
23 _build_glitch(
24 name => $_,
25 2 %{ $import{$_} },
26 10 map { $_ => '' } ("file", "filepath", "line", "stacktrace", "module")
27 2 ) for sort keys %import;
28 }
29 5 do {
30 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 5 my $package = caller();
32 5 *{"${package}::glitch"} = \&glitch;
33 };
34 }
35  
36 sub glitch {
37 3 ▶ my %options = (
Subroutine glitch: called CC=2 SCAR=6.9
Pod documented
38 name => shift,
39 _stack(),
40 @_
41 );
42 3 ▶ _build_glitch(%options) if (!$GLITCHES{$options{name}});
Branch: 1 branch
BranchTrueFalseTotal
unless $GLITCHES{$options{"name"}} 1 2 3
43 3 die _log($GLITCHES{$options{name}}->new(%options));
44 }
45  
46 sub logger {
47 0 ▶ $LOGGER = $_[0];
Subroutine logger: not called CC=1 SCAR=6.9
Pod undocumented
48 }
49  
50 sub logger_enabled {
51 0 ▶ $LOGGER_ENABLED = !!$_[0];
Subroutine logger_enabled: not called CC=1 SCAR=6.9
Pod undocumented
52 }
53  
54 sub logger_format {
55 0 ▶ $LOGGER_FORMAT = $_[0];
Subroutine logger_format: not called CC=1 SCAR=6.9
Pod undocumented
56 }
57  
58 sub logger_method {
59 0 ▶ $LOGGER_METHOD = $_[0];
Subroutine logger_method: not called CC=1 SCAR=6.9
Pod undocumented
60 }
61  
62 sub _log {
63 3 ▶ if ($LOGGER_ENABLED && $LOGGER) {
Subroutine _log: called CC=6 SCAR=22.8
Branch: 1 branch
BranchTrueFalseTotal
if ($LOGGER_ENABLED and $LOGGER) 1 2 3
Condition66%2/3
$LOGGER_ENABLED and $LOGGER
!ll&&!rl&&r
201
MC/DC50%1/2
$LOGGER_ENABLED and $LOGGER
$LOGGER_ENABLED $LOGGER
Truth table66%2/3
A and B
A= $LOGGER_ENABLEDB= $LOGGER
ABresult
0X0
100
111
64 1 my $ref = ref $LOGGER;
65 1 ▶ my $glitch = $LOGGER_FORMAT ? $_[0]->stringify($LOGGER_FORMAT) : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
$LOGGER_FORMAT ? : 1 0 1
66 0 ▶ $LOGGER_METHOD ||= 'err';
Condition0%0/2
$LOGGER_METHOD ||= "err"
l!l
00
MC/DC0%0/1
$LOGGER_METHOD ||= "err"
$LOGGER_METHOD
Truth table0%0/2
A
A= $LOGGER_METHOD
Aresult
11
00
67 0 ▶ ($ref eq 'CODE') ? $LOGGER->($glitch) : $LOGGER->$LOGGER_METHOD($glitch);
Branch: 1 branch
BranchTrueFalseTotal
$ref eq 'CODE' ? : 0 0 0
68 }
69 2 return $_[0];
70 }
71  
72  
73 sub _build_glitch {
74 3 ▶ my (%options) = @_;
Subroutine _build_glitch: called CC=8 SCAR=23.4
75 3 ▶ my $class = sprintf q|%s::%s|, $options{object_name} ||= 'Glitch', $options{name};
Condition50%1/2
$options{"object_name"} ||= "Glitch"
l!l
03
MC/DC0%0/1
$options{"object_name"} ||= "Glitch"
$options{"object_name"}
Truth table50%1/2
A
A= $options{"object_name"}
Aresult
11
00
76 3 ▶ my @methods = map { my $struct = $_ =~ m/(file|filepath|line|stacktrace|module)/ ? "''" : _stringify_struct($options{$_}); "sub $_ { return \$_[0]->{$_} || $struct; }" } sort keys %options;
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /(file|filepath|line|stacktrace|module)/ ? : 15 9 24
77 3 unshift @methods, 'sub new { my $self = shift; return bless {@_}, $self; }';
78 3 ▶ $STRINGIFY_FORMAT ||= 'default';
Condition50%1/2
$STRINGIFY_FORMAT ||= "default"
l!l
03
MC/DC0%0/1
$STRINGIFY_FORMAT ||= "default"
$STRINGIFY_FORMAT
Truth table50%1/2
A
A= $STRINGIFY_FORMAT
Aresult
11
00
79 3 push @methods, 'sub stringify { my $type = sprintf "stringify_%s", ($_[1] || "' . $STRINGIFY_FORMAT . '"); return $_[0]->$type(); }';
80 3 ▶ $STRINGIFY{default} ||= 'return $_[0]->message . " at " . $_[0]->filepath . " line " . $_[0]->line . "\n";';
Condition50%1/2
$STRINGIFY{"default"} ||= "return \$_[0]->message . \" at \" . \$_[0]->filepath . \" line \" . \$_[0]->line . \"\\n\";"
l!l
03
MC/DC0%0/1
$STRINGIFY{"default"} ||= "return \$_[0]->message . \" at \" . \$_[0]->filepath . \" line \" . \$_[0]->line . \"\\n\";"
$STRINGIFY{"default"}
Truth table50%1/2
A
A= $STRINGIFY{"default"}
Aresult
11
00
81 3 ▶ $STRINGIFY{json} ||= 'require JSON; JSON->new->encode({%{$_[0]}});';
Condition50%1/2
$STRINGIFY{"json"} ||= "require JSON; JSON->new->encode({%{\$_[0]}});"
l!l
03
MC/DC0%0/1
$STRINGIFY{"json"} ||= "require JSON; JSON->new->encode({%{\$_[0]}});"
$STRINGIFY{"json"}
Truth table50%1/2
A
A= $STRINGIFY{"json"}
Aresult
11
00
82 3 push @methods, sprintf "sub stringify_%s { %s }", $_, $STRINGIFY{$_} for sort keys %STRINGIFY;
83 3 my $package = sprintf(q|package %s;
84 use overload '""' => \&stringify;
85 %s
86 1;|, $class, join( "\n", @methods) );
87 3 ▶ eval $package;
Subroutine BEGIN: called CC=1 SCAR=1.6
Subroutine file: called CC=2 SCAR=9.9
Subroutine filepath: not called CC=2 SCAR=9.9
Subroutine line: not called CC=2 SCAR=9.9
Subroutine message: called CC=2 SCAR=9.9
Subroutine module: not called CC=2 SCAR=9.9
Subroutine name: not called CC=2 SCAR=9.9
Subroutine new: called CC=1 SCAR=1.6
Subroutine object_name: called CC=2 SCAR=9.9
Subroutine stacktrace: called CC=2 SCAR=9.9
Condition28%4/14
$_[0]{'file'} || ''
l!l
10
$_[0]{'filepath'} || ''
l!l
01
$_[0]{'line'} || ''
l!l
00
$_[0]{'message'} || 'this is a test'
l!l
00
$_[0]{'module'} || ''
l!l
00
$_[0]{'name'} || 'one'
l!l
00
$_[0]{'object_name'} || 'Glitch'
l!l
11
MC/DC0%0/1
$_[0]{'file'} || ''
$_[0]{'file'}
MC/DC0%0/1
$_[0]{'filepath'} || ''
$_[0]{'filepath'}
MC/DC0%0/1
$_[0]{'line'} || ''
$_[0]{'line'}
MC/DC0%0/1
$_[0]{'message'} || 'this is a test'
$_[0]{'message'}
MC/DC0%0/1
$_[0]{'module'} || ''
$_[0]{'module'}
MC/DC0%0/1
$_[0]{'name'} || 'one'
$_[0]{'name'}
MC/DC100%1/1
$_[0]{'object_name'} || 'Glitch'
$_[0]{'object_name'}
Truth table50%1/2
A
A= $_[0]{'file'}
Aresult
11
00
Truth table50%1/2
A
A= $_[0]{'filepath'}
Aresult
11
00
Truth table0%0/2
A
A= $_[0]{'line'}
Aresult
11
00
Truth table0%0/2
A
A= $_[0]{'message'}
Aresult
11
00
Truth table0%0/2
A
A= $_[0]{'module'}
Aresult
11
00
Truth table0%0/2
A
A= $_[0]{'name'}
Aresult
11
00
Truth table100%2/2
A
A= $_[0]{'object_name'}
Aresult
11
00
88 3 ▶ die $@ if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
89 3 $GLITCHES{$options{name}} = $class;
90 3 return 1;
91 }
92  
93 sub _stringify_struct {
94 9 ▶ my ( $struct ) = @_;
Subroutine _stringify_struct: called CC=3 SCAR=11.1
95 10 ▶ return 'undefined' unless defined $struct;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $struct 0 9 9
96 10 ▶ $struct = ref $struct ? Dumper $struct : "'$struct'";
Branch: 1 branch
BranchTrueFalseTotal
ref $struct ? : 0 9 9
97 10 $struct =~ s/\$VAR1 = //;
98 9 $struct =~ s/\s*\n*\s*package Glitch\;|use warnings\;|use strict\;//g;
99 9 $struct =~ s/{\s*\n*/{/;
100 12 $struct =~ s/;$//;
101 12 return $struct;
102 }
103  
104 sub _stack {
105 3 ▶ my @caller; my $i = 0; my @stack;
Subroutine _stack: called CC=12 SCAR=25.2
106 5 ▶ while(@caller = caller($i++)){
Branch: 1 branch
BranchTrueFalseTotal
if (@caller = caller $i++) 9 3 12
107 10 ▶ next if $caller[0] eq 'Glitch';
Branch: 1 branch
BranchTrueFalseTotal
if $caller[0] eq "Glitch" 3 6 9
108 7 $stack[$i+1]->{module} = $caller[0];
109 6 $stack[$i+1]->{filepath} = $caller[1];
110 6 ▶ $stack[$i+1]->{file} = $1 if $caller[1] =~ /([^\/]+)$/;;
Branch: 1 branch
BranchTrueFalseTotal
if $caller[1] =~ m[([^/]+)$] 6 0 6
111 6 ▶ $stack[$i+1]->{line} = $1 if $caller[2] =~ /(\d+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $caller[2] =~ /(\d+)/ 6 0 6
112 6 ▶ $stack[$i]->{sub} = $1 if $caller[3] =~ /([^:]+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $caller[3] =~ /([^:]+)$/ 6 0 6
113 }
114 3 my $msg = $stack[-1];
115 $msg->{stacktrace} = join '->', reverse map {
116 6 ▶ my $module = $_->{module} !~ m/^main$/ ? $_->{module} : $_->{file};
Branch: 1 branch
BranchTrueFalseTotal
!($_->{'module'} =~ /^main$/) ? : 0 6 6
117 $_->{sub}
118 ? $module . '::' . $_->{sub} . ':' . $_->{line}
119 : $module . ':' . $_->{line}
120 6 ▶ } grep {
Branch: 1 branch
BranchTrueFalseTotal
$_->{'sub'} ? : 3 3 6
121 3 $_ && $_->{module} && $_->{line} && $_->{file}
122 15 ▶ } @stack;
Branch: 1 branch
BranchTrueFalseTotal
$_ and $_->{"module"} and $_->{"line"} and $_->{"file"} 6 9 15
Condition83%5/6
$_ and $_->{"module"} and $_->{"line"}
!ll&&!rl&&r
906
$_ and $_->{"module"}
!ll&&!rl&&r
736
MC/DC0%0/3
$_ and $_->{"module"} and $_->{"line"}
$_ $_->{"module"} $_->{"line"}
Truth table0%0/4
A and B and C
A= $_B= $_->{"module"}C= $_->{"line"}
ABCresult
0XX0
10X0
1100
1111
123 3 ▶ delete $msg->{stacktrace} unless $msg->{stacktrace};
Branch: 1 branch
BranchTrueFalseTotal
unless $msg->{"stacktrace"} 0 3 3
124 3 return %{$msg};
125 }
126  
127 =head1 NAME
128  
129 Glitch - Exception Handling.
130  
131 =head1 VERSION
132  
133 Version 0.05
134  
135 =cut
136  
137 =head1 SYNOPSIS
138  
139 package Foo;
140  
141 use Glitch;
142  
143 sub bar {
144 do { ... } or glitch('one', message => 'Create a new glitch error message');
145  
146 ... later in your code you can then reuse glitch 'one'
147  
148 do { ... } or glitch('one');
149 }
150  
151 ...
152  
153 package Foo;
154  
155 use Glitch (
156 one => {
157 message => 'Create a new glitch error message'
158 },
159 two => {
160 message => 'A different glitch error message',
161 fileName => '',
162 }
163 );
164  
165 sub bar {
166 eval {
167 do { ... } or glitch('one');
168 ...
169 do { ... } or glitch('two', fileName => 'abc');
170 };
171 if ($@) {
172 do { ... } if $@->name eq 'one';
173 do { ... } if $@->name eq 'two';
174 }
175 }
176  
177 1;
178  
179 ...
180  
181 package Glitches;
182  
183 use Glitch (
184 glitch_logger => sub {
185 open my $fh, '>', 'glitch.log';
186 print $fh $_[0] . "\n";
187 close $fh;
188 },
189 glitch_stringify_format => 'json',
190 one => {
191 message => 'Create a new glitch error message'
192 },
193 two => {
194 message => 'A different glitch error message',
195 fileName => '',
196 }
197 );
198  
199 1;
200  
201 package Foo;
202  
203 use Glitch;
204 use Glitches;
205  
206 sub bar {
207 eval {
208 do { ... } or glitch('one');
209 ...
210 do { ... } or glitch('two', fileName => 'abc');
211 };
212 if ($@) {
213 if ($@->name eq 'one') { ... }
214 elsif ($@->name eq 'two') { ... }
215 }
216 }
217  
218 1;
219  
220 =head1 EXPORT
221  
222 =head2 glitch
223  
224 =cut
225  
226 =head1 AUTHOR
227  
228 LNATION, C<< <email at lnation.org> >>
229  
230 =head1 BUGS
231  
232 Please report any bugs or feature requests to C<bug-glitch at rt.cpan.org>, or through
233 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Glitch>. I will be notified, and then you'll
234 automatically be notified of progress on your bug as I make changes.
235  
236 =head1 SUPPORT
237  
238 You can find documentation for this module with the perldoc command.
239  
240 perldoc Glitch
241  
242 You can also look for information at:
243  
244 =over 4
245  
246 =item * RT: CPAN's request tracker (report bugs here)
247  
248 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Glitch>
249  
250 =item * CPAN Ratings
251  
252 L<https://cpanratings.perl.org/d/Glitch>
253  
254 =item * Search CPAN
255  
256 L<https://metacpan.org/release/Glitch>
257  
258 =back
259  
260 =head1 ACKNOWLEDGEMENTS
261  
262 =head1 LICENSE AND COPYRIGHT
263  
264 This software is Copyright (c) 2022 by LNATION.
265  
266 This is free software, licensed under:
267  
268 The Artistic License 2.0 (GPL Compatible)
269  
270 =cut
271  
272 4 1; # End of Glitch
↑ Summary