3 ## Based on Carp.pm from Perl 5.005_03.
4 ## Last modified 24-Oct-2009 by Steffen Beyer.
5 ## Should be reasonably backwards compatible.
7 ## This module is free software and can
8 ## be used, modified and redistributed
9 ## under the same terms as Perl itself.
12 @DB::args = (); # Avoid warning "used only once" in Perl 5.003
17 use vars qw( $MaxEvalLen $MaxArgLen $MaxArgNums $Verbose $VERSION );
20 # Original comments by Andy Wardley <abw@kfs.org> 09-Apr-1998.
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.
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.
29 $Verbose = 0; # If true then make _shortmsg call _longmsg instead.
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.
40 return (@_) if ( ref $_[0] );
41 local $_; # Protect surrounding program - just in case...
42 my ( $pack, $file, $line, $sub, $hargs, $eval, $require, @parms, $push );
43 my $error = join( '', @_ );
51 ( $pack, $file, $line, $sub, $hargs, undef, $eval, $require )
57 next if ( $pack eq 'Carp::Clan' );
59 if ( defined $eval ) {
60 $eval =~ s/([\\\'])/\\$1/g unless ($require); # Escape \ and '
62 =~ s/([\x00-\x1F\x7F-\xFF])/sprintf("\\x%02X",ord($1))/eg;
63 substr( $eval, $MaxEvalLen ) = '...'
64 if ( $MaxEvalLen && length($eval) > $MaxEvalLen );
65 if ($require) { $sub = "require $eval"; }
66 else { $sub = "eval '$eval'"; }
68 elsif ( $sub eq '(eval)' ) { $sub = 'eval {...}'; }
74 ; # We may trash some of the args so we take a copy
75 if ( $MaxArgNums and @parms > $MaxArgNums ) {
76 $#parms = $MaxArgNums;
83 $_ = overload::StrVal($_);
86 unless ( /^-?\d+(?:\.\d+(?:[eE][+-]\d+)?)?$/
89 s/([\\\'])/\\$1/g; # Escape \ and '
90 s/([\x00-\x1F\x7F-\xFF])/sprintf("\\x%02X",ord($1))/eg;
91 substr( $_, $MaxArgLen ) = '...'
93 and length($_) > $MaxArgLen );
98 else { $_ = 'undef'; }
100 push( @parms, '...' ) if ($push);
102 $sub .= '(' . join( ', ', @parms ) . ')';
104 if ( $msg eq '' ) { $msg = "$sub called"; }
105 else { $msg .= "\t$sub called"; }
108 $msg = quotemeta($sub);
109 if ( $error =~ /\b$msg\b/ ) { $msg = $error; }
111 if ( $sub =~ /::/ ) { $msg = "$sub(): $error"; }
112 else { $msg = "$sub: $error"; }
115 $msg .= " at $file line $line\n" unless ( $error =~ /\n$/ );
119 $msg =~ tr/\0//d; # Circumvent die's incorrect handling of NUL characters
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.
132 return (@_) if ( ref $_[0] );
133 goto &_longmsg if ( $Verbose or $verbose );
134 my ( $pack, $file, $line, $sub );
135 my $error = join( '', @_ );
138 while ( ( $pack, $file, $line, $sub ) = caller( $i++ ) ) {
139 next if ( $pack eq 'Carp::Clan' or $pack =~ /$pattern/ );
140 if ( $error eq '' ) { $msg = "$sub() called"; }
142 $msg = quotemeta($sub);
143 if ( $error =~ /\b$msg\b/ ) { $msg = $error; }
145 if ( $sub =~ /::/ ) { $msg = "$sub(): $error"; }
146 else { $msg = "$sub: $error"; }
149 $msg .= " at $file line $line\n" unless ( $error =~ /\n$/ );
150 $msg =~ tr/\0//d; # Circumvent die's incorrect handling of NUL characters
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".
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.
165 # Following code kept for calls with fully qualified subroutine names:
166 # (For backward compatibility with the original Carp.pm)
169 my $callpkg = caller(0);
170 my $pattern = ( $callpkg eq 'main' ) ? '^:::' : "^$callpkg\$";
171 die _shortmsg( $pattern, 0, @_ );
173 sub confess { die _longmsg(@_); }
176 my $callpkg = caller(0);
177 my $pattern = ( $callpkg eq 'main' ) ? '^:::' : "^$callpkg\$";
178 warn _shortmsg( $pattern, 0, @_ );
180 sub cluck { warn _longmsg(@_); }
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.
188 my $callpkg = caller(0);
189 my $pattern = ( $callpkg eq 'main' ) ? '^:::' : "^$callpkg\$";
195 if ( $item =~ /^\d/ ) {
196 if ( $VERSION < $item ) {
200 die _shortmsg( '^:::', 0,
201 "$pkg $item required--this is only version $VERSION ($file)"
205 elsif ( $item =~ /^verbose$/i ) { $verbose = 1; }
206 else { $pattern = $item; }
209 # Speed up pattern matching in Perl versions >= 5.005:
210 # (Uses "eval ''" because qr// is a syntax error in previous Perl versions)
212 eval '$pattern = qr/$pattern/;';
215 eval { $pkg =~ /$pattern/; };
219 $@ =~ s/\s+at\s.+$//;
220 die _shortmsg( '^:::', 0, $@ );
225 *{"${callpkg}::croak"} = sub { die _shortmsg( $pattern, $verbose, @_ ); };
226 *{"${callpkg}::confess"} = sub { die _longmsg ( @_ ); };
227 *{"${callpkg}::carp"} = sub { warn _shortmsg( $pattern, $verbose, @_ ); };
228 *{"${callpkg}::cluck"} = sub { warn _longmsg ( @_ ); };