blib/lib/Carp/Clan.pm

filter: Statementstmt 79.2% 107 / 135 Branchbran 55.7% 39 / 70 Conditioncond 38.8% 7 / 18 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 4 totaltotal 66.8% 169 / 253 CC 44 SCAR 35.7
CC
44
Cov
69%
_longmsg
69.9
_shortmsg
12.9
import
8.3
CRAP 35.7
↑ Summary
1 # ABSTRACT: Report errors from perspective of caller of a "clan" of modules
2  
3 ##
4 ## Based on Carp.pm from Perl 5.005_03.
5 ## Last modified 22-May-2016 by Kent Fredric.
6 ## Should be reasonably backwards compatible.
7 ##
8 ## This module is free software and can
9 ## be used, modified and redistributed
10 ## under the same terms as Perl itself.
11 ##
12  
13 3 @DB::args = (); # Avoid warning "used only once" in Perl 5.003
14  
15 package Carp::Clan; # git description: v6.07-8-g8b5dba6
16  
17 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # Original comments by Andy Wardley <abw@kfs.org> 09-Apr-1998.
21  
22 # The $Max(EvalLen|(Arg(Len|Nums)) variables are used to specify how
23 # the eval text and function arguments should be formatted when printed.
24  
25 3 our $MaxEvalLen = 0; # How much eval '...text...' to show. 0 = all.
26 3 our $MaxArgLen = 64; # How much of each argument to print. 0 = all.
27 3 our $MaxArgNums = 8; # How many arguments to print. 0 = all.
28  
29 3 our $Verbose = 0; # If true then make _shortmsg call _longmsg instead.
30  
31 3 our $VERSION = '6.08';
32  
33 # _longmsg() crawls all the way up the stack reporting on all the function
34 # calls made. The error string, $error, is originally constructed from the
35 # arguments passed into _longmsg() via confess(), cluck() or _shortmsg().
36 # This gets appended with the stack trace messages which are generated for
37 # each function call on the stack.
38  
39 52 ▶sub _longmsg {
Subroutine _longmsg: called CC=26 SCAR=42.5
40 52 ▶ return (@_) if ( ref $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 52 52
41 52 local $_; # Protect surrounding program - just in case...
42 52 my ( $pack, $file, $line, $sub, $hargs, $eval, $require, @parms, $push );
43 52 my $error = join( '', @_ );
44 52 my $msg = '';
45 52 my $i = 0;
46 52 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if (do { { package DB; () = caller $i++; } }) 420 52 472
47 do {
48 {
49  
50 472 package # hide from PAUSE
51 DB;
52 472 ( $pack, $file, $line, $sub, $hargs, undef, $eval, $require )
53 = caller( $i++ )
54 }
55 }
56 )
57 {
58 420 ▶ next if ( $pack eq 'Carp::Clan' );
Branch: 1 branch
BranchTrueFalseTotal
if $pack eq "Carp::Clan" 52 368 420
59 368 ▶ if ( $error eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error eq '') { } 316 52 368
60 316 ▶ if ( defined $eval ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $eval) { } 0 316 316
elsif ($sub eq '(eval)') { } 52 264 316
61 0 ▶ $eval =~ s/([\\\'])/\\$1/g unless ($require); # Escape \ and '
Branch: 1 branch
BranchTrueFalseTotal
unless $require 0 0 0
62 0 $eval
63 0 =~ s/([\x00-\x1F\x7F-\xFF])/sprintf("\\x%02X",ord($1))/eg;
64 0 ▶ substr( $eval, $MaxEvalLen ) = '...'
Branch: 1 branch
BranchTrueFalseTotal
if $Carp::Clan::MaxEvalLen and length $eval > $Carp::Clan::MaxEvalLen 0 0 0
Condition0%0/3
$Carp::Clan::MaxEvalLen and length $eval > $Carp::Clan::MaxEvalLen
!ll&&!rl&&r
000
MC/DC0%0/2
$Carp::Clan::MaxEvalLen and length $eval > $Carp::Clan::MaxEvalLen
$Carp::Clan::MaxEvalLen length $eval > $Carp::Clan::MaxEvalLen
Truth table0%0/3
A and B
A= $Carp::Clan::MaxEvalLenB= length $eval > $Carp::Clan::MaxEvalLen
ABresult
0X0
100
111
65 if ( $MaxEvalLen && length($eval) > $MaxEvalLen );
66 0 ▶ if ($require) { $sub = "require $eval"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($require) { } 0 0 0
67 0 else { $sub = "eval '$eval'"; }
68 }
69 52 elsif ( $sub eq '(eval)' ) { $sub = 'eval {...}'; }
70 else {
71 264 @parms = ();
72 264 ▶ if ($hargs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hargs) 264 0 264
73 264 $push = 0;
74 264 @parms = @DB::args
75 ; # We may trash some of the args so we take a copy
76 264 ▶ if ( $MaxArgNums and @parms > $MaxArgNums ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Carp::Clan::MaxArgNums and @parms > $Carp::Clan::MaxArgNums) 0 264 264
Condition33%1/3
$Carp::Clan::MaxArgNums and @parms > $Carp::Clan::MaxArgNums
!ll&&!rl&&r
02640
MC/DC0%0/2
$Carp::Clan::MaxArgNums and @parms > $Carp::Clan::MaxArgNums
$Carp::Clan::MaxArgNums @parms > $Carp::Clan::MaxArgNums
Truth table33%1/3
A and B
A= $Carp::Clan::MaxArgNumsB= @parms > $Carp::Clan::MaxArgNums
ABresult
0X0
100
111
77 0 $#parms = $MaxArgNums;
78 0 pop(@parms);
79 0 $push = 1;
80 }
81 264 for (@parms) {
82 528 ▶ if ( defined $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) { } 528 0 528
83 528 ▶ if ( ref $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 0 528 528
84 0 $_ = overload::StrVal($_);
85 }
86 else {
87 528 ▶ unless ( /^-?\d+(?:\.\d+(?:[eE][+-]\d+)?)?$/
Branch: 1 branch
BranchTrueFalseTotal
unless (/^-?\d+(?:\.\d+(?:[eE][+-]\d+)?)?$/) 396 132 528
88 ) # Looks numeric
89 {
90 396 s/([\\\'])/\\$1/g; # Escape \ and '
91 0 s/([\x00-\x1F\x7F-\xFF])/sprintf("\\x%02X",ord($1))/eg;
92 396 ▶ substr( $_, $MaxArgLen ) = '...'
Branch: 1 branch
BranchTrueFalseTotal
if $Carp::Clan::MaxArgLen and length $_ > $Carp::Clan::MaxArgLen 0 396 396
Condition33%1/3
$Carp::Clan::MaxArgLen and length $_ > $Carp::Clan::MaxArgLen
!ll&&!rl&&r
03960
MC/DC0%0/2
$Carp::Clan::MaxArgLen and length $_ > $Carp::Clan::MaxArgLen
$Carp::Clan::MaxArgLen length $_ > $Carp::Clan::MaxArgLen
Truth table33%1/3
A and B
A= $Carp::Clan::MaxArgLenB= length $_ > $Carp::Clan::MaxArgLen
ABresult
0X0
100
111
93 if ( $MaxArgLen
94 and length($_) > $MaxArgLen );
95 396 $_ = "'$_'";
96 }
97 }
98 }
99 0 else { $_ = 'undef'; }
100 }
101 264 ▶ push( @parms, '...' ) if ($push);
Branch: 1 branch
BranchTrueFalseTotal
if $push 0 264 264
102 }
103 264 $sub .= '(' . join( ', ', @parms ) . ')';
104 }
105 0 ▶ if ( $msg eq '' ) { $msg = "$sub called"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($msg eq '') { } 0 316 316
106 316 else { $msg .= "\t$sub called"; }
107 }
108 else {
109 52 $msg = quotemeta($sub);
110 0 ▶ if ( $error =~ /\b$msg\b/ ) { $msg = $error; }
Branch: 1 branch
BranchTrueFalseTotal
if ($error =~ /\b$msg\b/) { } 0 52 52
111 else {
112 52 ▶ if ( $sub =~ /::/ ) { $msg = "$sub(): $error"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($sub =~ /::/) { } 52 0 52
113 0 else { $msg = "$sub: $error"; }
114 }
115 }
116 368 ▶ $msg .= " at $file line $line\n" unless ( $error =~ /\n$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $error =~ /\n$/ 368 0 368
117 368 $error = '';
118 }
119 52 ▶ $msg ||= $error;
Condition33%1/3
$msg ||= $error
l!l&&r!l&&!r
5200
MC/DC0%0/2
$msg ||= $error
$msg $error
Truth table33%1/3
A ||= B
A= $msgB= $error
ABresult
1X1
011
000
120 52 $msg =~ tr/\0//d; # Circumvent die's incorrect handling of NUL characters
121 52 $msg;
122 }
123  
124 # _shortmsg() is called by carp() and croak() to skip all the way up to
125 # the top-level caller's package and report the error from there. confess()
126 # and cluck() generate a full stack trace so they call _longmsg() to
127 # generate that. In verbose mode _shortmsg() calls _longmsg() so you
128 # always get a stack trace.
129  
130 44 ▶sub _shortmsg {
Subroutine _shortmsg: called CC=11 SCAR=25.6
131 44 my $pattern = shift;
132 44 my $verbose = shift;
133 44 ▶ return (@_) if ( ref $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 44 44
134 44 ▶ goto &_longmsg if ( $Verbose or $verbose );
Branch: 1 branch
BranchTrueFalseTotal
if $Carp::Clan::Verbose or $verbose 0 44 44
Condition33%1/3
$Carp::Clan::Verbose or $verbose
l!l&&r!l&&!r
0044
MC/DC0%0/2
$Carp::Clan::Verbose or $verbose
$Carp::Clan::Verbose $verbose
Truth table33%1/3
A or B
A= $Carp::Clan::VerboseB= $verbose
ABresult
1X1
011
000
135 44 my ( $pack, $file, $line, $sub );
136 44 my $error = join( '', @_ );
137 44 my $msg = '';
138 44 my $i = 0;
139 44 ▶ while ( ( $pack, $file, $line, $sub ) = caller( $i++ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = caller $i++) 232 8 240
140 232 ▶ next if ( $pack eq 'Carp::Clan' or $pack =~ /$pattern/ );
Branch: 1 branch
BranchTrueFalseTotal
if $pack eq "Carp::Clan" or $pack =~ /$pattern/ 196 36 232
Condition100%3/3
$pack eq "Carp::Clan" or $pack =~ /$pattern/
l!l&&r!l&&!r
4415236
MC/DC100%2/2
$pack eq "Carp::Clan" or $pack =~ /$pattern/
$pack eq "Carp::Clan" $pack =~ /$pattern/
Truth table100%3/3
A or B
A= $pack eq "Carp::Clan"B= $pack =~ /$pattern/
ABresult
1X1
011
000
141 0 ▶ if ( $error eq '' ) { $msg = "$sub() called"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($error eq '') { } 0 36 36
142 else {
143 36 $msg = quotemeta($sub);
144 0 ▶ if ( $error =~ /\b$msg\b/ ) { $msg = $error; }
Branch: 1 branch
BranchTrueFalseTotal
if ($error =~ /\b$msg\b/) { } 0 36 36
145 else {
146 36 ▶ if ( $sub =~ /::/ ) { $msg = "$sub(): $error"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($sub =~ /::/) { } 36 0 36
147 0 else { $msg = "$sub: $error"; }
148 }
149 }
150 36 ▶ $msg .= " at $file line $line\n" unless ( $error =~ /\n$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $error =~ /\n$/ 36 0 36
151 36 $msg =~ tr/\0//d; # Circumvent die's incorrect handling of NUL characters
152 36 return $msg;
153 }
154 8 goto &_longmsg;
155 }
156  
157 # In the two identical regular expressions (immediately after the two occurrences of
158 # "quotemeta") above, the "\b ... \b" helps to avoid confusion between function names
159 # which are prefixes of each other, e.g. "My::Class::print" and "My::Class::println".
160  
161 # The following four functions call _longmsg() or _shortmsg() depending on
162 # whether they should generate a full stack trace (confess() and cluck())
163 # or simply report the caller's package (croak() and carp()), respectively.
164 # confess() and croak() die, carp() and cluck() warn.
165  
166 # Following code kept for calls with fully qualified subroutine names:
167 # (For backward compatibility with the original Carp.pm)
168  
169 2 ▶sub croak {
Subroutine croak: called CC=2 SCAR=7.1
Pod undocumented
170 2 my $callpkg = caller(0);
171 2 ▶ my $pattern = ( $callpkg eq 'main' ) ? '^:::' : "^$callpkg\$";
Branch: 1 branch
BranchTrueFalseTotal
$callpkg eq 'main' ? : 2 0 2
172 2 die _shortmsg( $pattern, 0, @_ );
173 }
174 2 ▶sub confess { die _longmsg(@_); }
Subroutine confess: called CC=1 SCAR=0.0
Pod undocumented
175  
176 2 ▶sub carp {
Subroutine carp: called CC=2 SCAR=7.1
Pod undocumented
177 2 my $callpkg = caller(0);
178 2 ▶ my $pattern = ( $callpkg eq 'main' ) ? '^:::' : "^$callpkg\$";
Branch: 1 branch
BranchTrueFalseTotal
$callpkg eq 'main' ? : 2 0 2
179 2 warn _shortmsg( $pattern, 0, @_ );
180 }
181 2 ▶sub cluck { warn _longmsg(@_); }
Subroutine cluck: called CC=1 SCAR=0.0
Pod undocumented
182  
183 # The following method imports a different closure for every caller.
184 # I.e., different modules can use this module at the same time
185 # and in parallel and still use different patterns.
186  
187 22 ▶sub import {
Subroutine import: called CC=7 SCAR=21.2
188 22 my $pkg = shift;
189 22 my $callpkg = caller(0);
190 22 ▶ my $pattern = ( $callpkg eq 'main' ) ? '^:::' : "^$callpkg\$";
Branch: 1 branch
BranchTrueFalseTotal
$callpkg eq 'main' ? : 4 18 22
191 22 my $verbose = 0;
192 22 my $item;
193 my $file;
194  
195 22 for $item (@_) {
196 18 ▶ if ( $item =~ /^\d/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item =~ /^\d/) { } 0 18 18
elsif ($item =~ /^verbose$/i) { } 0 18 18
197 0 ▶ if ( $VERSION < $item ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Carp::Clan::VERSION < $item) 0 0 0
198 0 $file = "$pkg.pm";
199 0 $file =~ s!::!/!g;
200 0 $file = $INC{$file};
201 0 die _shortmsg( '^:::', 0,
202 "$pkg $item required--this is only version $VERSION ($file)"
203 );
204 }
205 }
206 0 elsif ( $item =~ /^verbose$/i ) { $verbose = 1; }
207 18 else { $pattern = $item; }
208 }
209  
210 22 eval { $pattern = qr/$pattern/ };
211  
212 22 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 22 22
213 0 $@ =~ s/\s+$//;
214 0 $@ =~ s/\s+at\s.+$//;
215 0 die _shortmsg( '^:::', 0, $@ );
216 }
217  
218 {
219 22 local ($^W) = 0;
220 3 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
221 22 ▶ *{"${callpkg}::croak"} = sub { die _shortmsg( $pattern, $verbose, @_ ); };
Subroutine __ANON__: called CC=1 SCAR=0.0
222 22 ▶ *{"${callpkg}::confess"} = sub { die _longmsg ( @_ ); };
Subroutine __ANON__: called CC=1 SCAR=0.0
223 22 ▶ *{"${callpkg}::carp"} = sub { warn _shortmsg( $pattern, $verbose, @_ ); };
Subroutine __ANON__: called CC=1 SCAR=0.0
224 22 ▶ *{"${callpkg}::cluck"} = sub { warn _longmsg ( @_ ); };
Subroutine __ANON__: called CC=1 SCAR=0.0
225 }
226 }
227  
228 3 1;
229  
230 __END__
↑ Summary