1 # Carp::Heavy uses some variables in common with Carp.
6 Carp::Heavy - heavy machinery, no user serviceable parts inside
10 # On one line so MakeMaker will see it.
11 use Carp; our $VERSION = $Carp::VERSION;
12 # use strict; # not yet
14 # 'use Carp' just installs some very lightweight stubs; the first time
15 # these are called, they require Carp::Heavy which installs the real
18 # Comments added by Andy Wardley <abw@kfs.org> 09-Apr-98, based on an
19 # _almost_ complete understanding of the package. Corrections and
20 # comments are welcome.
22 # The members of %Internal are packages that are internal to perl.
23 # Carp will not report errors from within these packages if it
24 # can. The members of %CarpInternal are internal to Perl's warning
25 # system. Carp will not report errors from within these packages
26 # either, and will not report calls *to* these packages for carp and
27 # croak. They replace $CarpLevel, which is deprecated. The
28 # $Max(EvalLen|(Arg(Len|Nums)) variables are used to specify how the eval
29 # text and function arguments should be formatted when printed.
31 # Comments added by Jos I. Boumans <kane@dwim.org> 11-Aug-2004
32 # I can not get %CarpInternal or %Internal to work as advertised,
33 # therefore leaving it out of the below documentation.
34 # $CarpLevel may be decprecated according to the last comment, but
35 # after 6 years, it's still around and in heavy use ;)
37 # disable these by default, so they can live w/o require Carp
38 $CarpInternal{Carp}++;
39 $CarpInternal{warnings}++;
40 $Internal{Exporter}++;
41 $Internal{'Exporter::Heavy'}++;
44 our ($CarpLevel, $MaxArgNums, $MaxEvalLen, $MaxArgLen, $Verbose);
46 # XXX longmess_real and shortmess_real should really be merged into
47 # XXX {long|sort}mess_heavy at some point
50 # Icky backwards compatibility wrapper. :-(
51 my $call_pack = caller();
52 if ($Internal{$call_pack} or $CarpInternal{$call_pack}) {
53 return longmess_heavy(@_);
56 local $CarpLevel = $CarpLevel + 1;
57 return longmess_heavy(@_);
62 # Icky backwards compatibility wrapper. :-(
63 my $call_pack = caller();
64 local @CARP_NOT = caller();
68 # replace the two hooks added by Carp
70 # aliasing the whole glob rather than just the CV slot avoids 'redefined'
71 # warnings, even in the presence of perl -W (as used by lib/warnings.t !)
73 *longmess_jmp = *longmess_real;
74 *shortmess_jmp = *shortmess_real;
78 my $i = shift(@_) + 1;
82 qw(pack file line sub has_args wantarray evaltext is_require)
85 unless (defined $call_info{pack}) {
89 my $sub_name = Carp::get_subname(\%call_info);
90 if ($call_info{has_args}) {
91 my @args = map {Carp::format_arg($_)} @DB::args;
92 if ($MaxArgNums and @args > $MaxArgNums) { # More than we want to show?
96 # Push the args onto the subroutine
97 $sub_name .= '(' . join (', ', @args) . ')';
99 $call_info{sub_name} = $sub_name;
100 return wantarray() ? %call_info : \%call_info;
103 # Transform an argument to a function into a string.
107 $arg = defined($overload::VERSION) ? overload::StrVal($arg) : "$arg";
108 }elsif (not defined($arg)) {
112 $arg = str_len_trim($arg, $MaxArgLen);
115 $arg = "'$arg'" unless $arg =~ /^-?[\d.]+\z/;
117 # The following handling of "control chars" is direct from
118 # the original code - it is broken on Unicode though.
121 or $arg =~ s/([[:cntrl:]]|[[:^ascii:]])/sprintf("\\x{%x}",ord($1))/eg;
125 # Takes an inheritance cache and a package and returns
126 # an anon hash of known inheritances and anon array of
127 # inheritances which consequences have not been figured
132 $cache->{$pkg} ||= [{$pkg => $pkg}, [trusts_directly($pkg)]];
133 return @{$cache->{$pkg}};
136 # Takes the info from caller() and figures out the name of
137 # the sub/require/eval
140 if (defined($info->{evaltext})) {
141 my $eval = $info->{evaltext};
142 if ($info->{is_require}) {
143 return "require $eval";
146 $eval =~ s/([\\\'])/\\$1/g;
147 return "eval '" . str_len_trim($eval, $MaxEvalLen) . "'";
151 return ($info->{sub} eq '(eval)') ? 'eval {...}' : $info->{sub};
154 # Figures out what call (from the point of view of the caller)
155 # the long error backtrace should start at.
158 my $lvl = $CarpLevel;
160 my $pkg = caller(++$i);
161 unless(defined($pkg)) {
162 # This *shouldn't* happen.
165 $i = long_error_loc();
169 # OK, now I am irritated.
173 redo if $CarpInternal{$pkg};
174 redo unless 0 > --$lvl;
175 redo if $Internal{$pkg};
182 return @_ if ref($_[0]); # don't break references as exceptions
183 my $i = long_error_loc();
184 return ret_backtrace($i, @_);
187 # Returns a full stack backtrace starting from where it is
190 my ($i, @error) = @_;
192 my $err = join '', @error;
196 if (defined &Thread::tid) {
197 my $tid = Thread->self->tid;
198 $tid_msg = " thread $tid" if $tid;
201 my %i = caller_info($i);
202 $mess = "$err at $i{file} line $i{line}$tid_msg\n";
204 while (my %i = caller_info(++$i)) {
205 $mess .= "\t$i{sub_name} called at $i{file} line $i{line}$tid_msg\n";
212 my ($i, @error) = @_;
213 my $err = join '', @error;
217 if (defined &Thread::tid) {
218 my $tid = Thread->self->tid;
219 $tid_msg = " thread $tid" if $tid;
222 my %i = caller_info($i);
223 return "$err at $i{file} line $i{line}$tid_msg\n";
227 sub short_error_loc {
230 my $lvl = $CarpLevel;
232 my $called = caller($i++);
233 my $caller = caller($i);
235 return 0 unless defined($caller); # What happened?
236 redo if $Internal{$caller};
237 redo if $CarpInternal{$called};
238 redo if trusts($called, $caller, $cache);
239 redo if trusts($caller, $called, $cache);
240 redo unless 0 > --$lvl;
246 sub shortmess_heavy {
247 return longmess_heavy(@_) if $Verbose;
248 return @_ if ref($_[0]); # don't break references as exceptions
249 my $i = short_error_loc();
258 # If a string is too long, trims it with ...
261 my $max = shift || 0;
262 if (2 < $max and $max < length($str)) {
263 substr($str, $max - 3) = '...';
268 # Takes two packages and an optional cache. Says whether the
269 # first inherits from the second.
271 # Recursive versions of this have to work to avoid certain
272 # possible endless loops, and when following long chains of
273 # inheritance are less efficient.
277 my $cache = shift || {};
278 my ($known, $partial) = get_status($cache, $child);
279 # Figure out consequences until we have an answer
280 while (@$partial and not exists $known->{$parent}) {
281 my $anc = shift @$partial;
282 next if exists $known->{$anc};
284 my ($anc_knows, $anc_partial) = get_status($cache, $anc);
285 my @found = keys %$anc_knows;
286 @$known{@found} = ();
287 push @$partial, @$anc_partial;
289 return exists $known->{$parent};
292 # Takes a package and gives a list of those trusted directly
293 sub trusts_directly {
297 return @{"$class\::CARP_NOT"}
298 ? @{"$class\::CARP_NOT"}