File Coverage

blib/lib/Carp/Clan.pm
Criterion Covered Total %
statement 103 133 77.4
branch 36 68 52.9
condition 7 18 38.8
subroutine 15 15 100.0
pod 0 4 0.0
total 161 238 67.6


line stmt bran cond sub pod time code
1              
2             ##
3             ## Based on Carp.pm from Perl 5.005_03.
4             ## Last modified 22-May-2016 by Kent Fredric.
5             ## Should be reasonably backwards compatible.
6             ##
7             ## This module is free software and can
8             ## be used, modified and redistributed
9             ## under the same terms as Perl itself.
10             ##
11              
12             @DB::args = (); # Avoid warning "used only once" in Perl 5.003
13              
14             package Carp::Clan;
15              
16 4     4   2319 use strict;
  4         3  
  4         103  
17 4     4   9 use vars qw( $MaxEvalLen $MaxArgLen $MaxArgNums $Verbose $VERSION );
  4         4  
  4         201  
18 4     4   11 use overload ();
  4         8  
  4         3799  
19              
20             # Original comments by Andy Wardley 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             $MaxEvalLen = 0; # How much eval '...text...' to show. 0 = all.
26             $MaxArgLen = 64; # How much of each argument to print. 0 = all.
27             $MaxArgNums = 8; # How many arguments to print. 0 = all.
28              
29             $Verbose = 0; # If true then make _shortmsg call _longmsg instead.
30              
31             $VERSION = '6.06';
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             sub _longmsg {
40 52 50   52   70 return (@_) if ( ref $_[0] );
41 52         38 local $_; # Protect surrounding program - just in case...
42 52         43 my ( $pack, $file, $line, $sub, $hargs, $eval, $require, @parms, $push );
43 52         59 my $error = join( '', @_ );
44 52         31 my $msg = '';
45 52         32 my $i = 0;
46 52         33 while (
47             do {
48             {
49              
50 472         257 package DB;
51 472         1591 ( $pack, $file, $line, $sub, $hargs, undef, $eval, $require )
52             = caller( $i++ )
53             }
54             }
55             )
56             {
57 420 100       559 next if ( $pack eq 'Carp::Clan' );
58 368 100       339 if ( $error eq '' ) {
59 316 50       374 if ( defined $eval ) {
    100          
60 0 0       0 $eval =~ s/([\\\'])/\\$1/g unless ($require); # Escape \ and '
61 0         0 $eval
62 0         0 =~ s/([\x00-\x1F\x7F-\xFF])/sprintf("\\x%02X",ord($1))/eg;
63 0 0 0     0 substr( $eval, $MaxEvalLen ) = '...'
64             if ( $MaxEvalLen && length($eval) > $MaxEvalLen );
65 0 0       0 if ($require) { $sub = "require $eval"; }
  0         0  
66 0         0 else { $sub = "eval '$eval'"; }
67             }
68 52         35 elsif ( $sub eq '(eval)' ) { $sub = 'eval {...}'; }
69             else {
70 264         211 @parms = ();
71 264 50       282 if ($hargs) {
72 264         149 $push = 0;
73 264         241 @parms = @DB::args
74             ; # We may trash some of the args so we take a copy
75 264 50 33     703 if ( $MaxArgNums and @parms > $MaxArgNums ) {
76 0         0 $#parms = $MaxArgNums;
77 0         0 pop(@parms);
78 0         0 $push = 1;
79             }
80 264         247 for (@parms) {
81 528 50       436 if ( defined $_ ) {
82 528 50       440 if ( ref $_ ) {
83 0         0 $_ = overload::StrVal($_);
84             }
85             else {
86 528 100       1004 unless ( /^-?\d+(?:\.\d+(?:[eE][+-]\d+)?)?$/
87             ) # Looks numeric
88             {
89 396         287 s/([\\\'])/\\$1/g; # Escape \ and '
90 396         238 s/([\x00-\x1F\x7F-\xFF])/sprintf("\\x%02X",ord($1))/eg;
  0         0  
91 396 50 33     880 substr( $_, $MaxArgLen ) = '...'
92             if ( $MaxArgLen
93             and length($_) > $MaxArgLen );
94 396         492 $_ = "'$_'";
95             }
96             }
97             }
98 0         0 else { $_ = 'undef'; }
99             }
100 264 50       289 push( @parms, '...' ) if ($push);
101             }
102 264         300 $sub .= '(' . join( ', ', @parms ) . ')';
103             }
104 316 50       285 if ( $msg eq '' ) { $msg = "$sub called"; }
  0         0  
105 316         299 else { $msg .= "\t$sub called"; }
106             }
107             else {
108 52         54 $msg = quotemeta($sub);
109 52 50       154 if ( $error =~ /\b$msg\b/ ) { $msg = $error; }
  0         0  
110             else {
111 52 50       78 if ( $sub =~ /::/ ) { $msg = "$sub(): $error"; }
  52         70  
112 0         0 else { $msg = "$sub: $error"; }
113             }
114             }
115 368 50       597 $msg .= " at $file line $line\n" unless ( $error =~ /\n$/ );
116 368         236 $error = '';
117             }
118 52   33     57 $msg ||= $error;
119 52         79 $msg =~ tr/\0//d; # Circumvent die's incorrect handling of NUL characters
120 52         193 $msg;
121             }
122              
123             # _shortmsg() is called by carp() and croak() to skip all the way up to
124             # the top-level caller's package and report the error from there. confess()
125             # and cluck() generate a full stack trace so they call _longmsg() to
126             # generate that. In verbose mode _shortmsg() calls _longmsg() so you
127             # always get a stack trace.
128              
129             sub _shortmsg {
130 44     44   35 my $pattern = shift;
131 44         28 my $verbose = shift;
132 44 50       63 return (@_) if ( ref $_[0] );
133 44 50 33     136 goto &_longmsg if ( $Verbose or $verbose );
134 44         28 my ( $pack, $file, $line, $sub );
135 44         49 my $error = join( '', @_ );
136 44         27 my $msg = '';
137 44         32 my $i = 0;
138 44         182 while ( ( $pack, $file, $line, $sub ) = caller( $i++ ) ) {
139 232 100 100     1230 next if ( $pack eq 'Carp::Clan' or $pack =~ /$pattern/ );
140 36 50       45 if ( $error eq '' ) { $msg = "$sub() called"; }
  0         0  
141             else {
142 36         39 $msg = quotemeta($sub);
143 36 50       170 if ( $error =~ /\b$msg\b/ ) { $msg = $error; }
  0         0  
144             else {
145 36 50       52 if ( $sub =~ /::/ ) { $msg = "$sub(): $error"; }
  36         56  
146 0         0 else { $msg = "$sub: $error"; }
147             }
148             }
149 36 50       73 $msg .= " at $file line $line\n" unless ( $error =~ /\n$/ );
150 36         37 $msg =~ tr/\0//d; # Circumvent die's incorrect handling of NUL characters
151 36         161 return $msg;
152             }
153 8         13 goto &_longmsg;
154             }
155              
156             # In the two identical regular expressions (immediately after the two occurrences of
157             # "quotemeta") above, the "\b ... \b" helps to avoid confusion between function names
158             # which are prefixes of each other, e.g. "My::Class::print" and "My::Class::println".
159              
160             # The following four functions call _longmsg() or _shortmsg() depending on
161             # whether they should generate a full stack trace (confess() and cluck())
162             # or simply report the caller's package (croak() and carp()), respectively.
163             # confess() and croak() die, carp() and cluck() warn.
164              
165             # Following code kept for calls with fully qualified subroutine names:
166             # (For backward compatibility with the original Carp.pm)
167              
168             sub croak {
169 2     2 0 36 my $callpkg = caller(0);
170 2 50       4 my $pattern = ( $callpkg eq 'main' ) ? '^:::' : "^$callpkg\$";
171 2         4 die _shortmsg( $pattern, 0, @_ );
172             }
173 2     2 0 21 sub confess { die _longmsg(@_); }
174              
175             sub carp {
176 2     2 0 33 my $callpkg = caller(0);
177 2 50       4 my $pattern = ( $callpkg eq 'main' ) ? '^:::' : "^$callpkg\$";
178 2         2 warn _shortmsg( $pattern, 0, @_ );
179             }
180 2     2 0 36 sub cluck { warn _longmsg(@_); }
181              
182             # The following method imports a different closure for every caller.
183             # I.e., different modules can use this module at the same time
184             # and in parallel and still use different patterns.
185              
186             sub import {
187 23     23   797 my $pkg = shift;
188 23         23 my $callpkg = caller(0);
189 23 100       74 my $pattern = ( $callpkg eq 'main' ) ? '^:::' : "^$callpkg\$";
190 23         20 my $verbose = 0;
191 23         14 my $item;
192             my $file;
193              
194 23         27 for $item (@_) {
195 19 50       48 if ( $item =~ /^\d/ ) {
    50          
196 0 0       0 if ( $VERSION < $item ) {
197 0         0 $file = "$pkg.pm";
198 0         0 $file =~ s!::!/!g;
199 0         0 $file = $INC{$file};
200 0         0 die _shortmsg( '^:::', 0,
201             "$pkg $item required--this is only version $VERSION ($file)"
202             );
203             }
204             }
205 0         0 elsif ( $item =~ /^verbose$/i ) { $verbose = 1; }
206 19         25 else { $pattern = $item; }
207             }
208              
209             # Speed up pattern matching in Perl versions >= 5.005:
210             # (Uses "eval ''" because qr// is a syntax error in previous Perl versions)
211 23 50       28 if ( $] >= 5.005 ) {
212 23         1034 eval '$pattern = qr/$pattern/;';
213             }
214             else {
215 0         0 eval { $pkg =~ /$pattern/; };
  0         0  
216             }
217 23 50       67 if ($@) {
218 0         0 $@ =~ s/\s+$//;
219 0         0 $@ =~ s/\s+at\s.+$//;
220 0         0 die _shortmsg( '^:::', 0, $@ );
221             }
222             {
223 23         17 local ($^W) = 0;
  23         48  
224 4     4   16 no strict "refs";
  4         3  
  4         643  
225 23     20   57 *{"${callpkg}::croak"} = sub { die _shortmsg( $pattern, $verbose, @_ ); };
  23         95  
  20         231  
226 23     20   34 *{"${callpkg}::confess"} = sub { die _longmsg ( @_ ); };
  23         45  
  20         365  
227 23     20   28 *{"${callpkg}::carp"} = sub { warn _shortmsg( $pattern, $verbose, @_ ); };
  23         52  
  20         642  
228 23     20   30 *{"${callpkg}::cluck"} = sub { warn _longmsg ( @_ ); };
  23         98  
  20         508  
229             }
230             }
231              
232             1;
233