1 # Carp::Heavy uses some variables in common with Carp.
6 Carp::Heavy - heavy machinery, no user serviceable parts inside
10 # use strict; # not yet
12 # On one line so MakeMaker will see it.
13 use Carp
; our $VERSION = $Carp::VERSION
;
15 our ($CarpLevel, $MaxArgNums, $MaxEvalLen, $MaxArgLen, $Verbose);
18 my $i = shift(@_) + 1;
22 qw(pack file line sub has_args wantarray evaltext is_require)
25 unless (defined $call_info{pack}) {
29 my $sub_name = Carp
::get_subname
(\
%call_info);
30 if ($call_info{has_args
}) {
31 my @args = map {Carp
::format_arg
($_)} @DB::args
;
32 if ($MaxArgNums and @args > $MaxArgNums) { # More than we want to show?
36 # Push the args onto the subroutine
37 $sub_name .= '(' . join (', ', @args) . ')';
39 $call_info{sub_name
} = $sub_name;
40 return wantarray() ?
%call_info : \
%call_info;
43 # Transform an argument to a function into a string.
47 $arg = defined($overload::VERSION
) ? overload
::StrVal
($arg) : "$arg";
48 }elsif (not defined($arg)) {
52 $arg = str_len_trim
($arg, $MaxArgLen);
55 $arg = "'$arg'" unless $arg =~ /^-?[\d.]+\z/;
57 # The following handling of "control chars" is direct from
58 # the original code - it is broken on Unicode though.
61 or $arg =~ s/([[:cntrl:]]|[[:^ascii:]])/sprintf("\\x{%x}",ord($1))/eg;
65 # Takes an inheritance cache and a package and returns
66 # an anon hash of known inheritances and anon array of
67 # inheritances which consequences have not been figured
72 $cache->{$pkg} ||= [{$pkg => $pkg}, [trusts_directly
($pkg)]];
73 return @
{$cache->{$pkg}};
76 # Takes the info from caller() and figures out the name of
77 # the sub/require/eval
80 if (defined($info->{evaltext
})) {
81 my $eval = $info->{evaltext
};
82 if ($info->{is_require
}) {
83 return "require $eval";
86 $eval =~ s/([\\\'])/\\$1/g;
87 return "eval '" . str_len_trim
($eval, $MaxEvalLen) . "'";
91 return ($info->{sub} eq '(eval)') ?
'eval {...}' : $info->{sub};
94 # Figures out what call (from the point of view of the caller)
95 # the long error backtrace should start at.
100 my $pkg = caller(++$i);
101 unless(defined($pkg)) {
102 # This *shouldn't* happen.
105 $i = long_error_loc
();
109 # OK, now I am irritated.
113 redo if $CarpInternal{$pkg};
114 redo unless 0 > --$lvl;
115 redo if $Internal{$pkg};
122 return @_ if ref($_[0]); # don't break references as exceptions
123 my $i = long_error_loc
();
124 return ret_backtrace
($i, @_);
127 # Returns a full stack backtrace starting from where it is
130 my ($i, @error) = @_;
132 my $err = join '', @error;
136 if (defined &Thread
::tid
) {
137 my $tid = Thread
->self->tid;
138 $tid_msg = " thread $tid" if $tid;
141 my %i = caller_info
($i);
142 $mess = "$err at $i{file} line $i{line}$tid_msg\n";
144 while (my %i = caller_info
(++$i)) {
145 $mess .= "\t$i{sub_name} called at $i{file} line $i{line}$tid_msg\n";
152 my ($i, @error) = @_;
153 my $err = join '', @error;
157 if (defined &Thread
::tid
) {
158 my $tid = Thread
->self->tid;
159 $tid_msg = " thread $tid" if $tid;
162 my %i = caller_info
($i);
163 return "$err at $i{file} line $i{line}$tid_msg\n";
167 sub short_error_loc
{
170 my $lvl = $CarpLevel;
172 my $called = caller($i++);
173 my $caller = caller($i);
174 return 0 unless defined($caller); # What happened?
175 redo if $Internal{$caller};
176 redo if $CarpInternal{$called};
177 redo if trusts
($called, $caller, $cache);
178 redo if trusts
($caller, $called, $cache);
179 redo unless 0 > --$lvl;
184 sub shortmess_heavy
{
185 return longmess_heavy
(@_) if $Verbose;
186 return @_ if ref($_[0]); # don't break references as exceptions
187 my $i = short_error_loc
();
196 # If a string is too long, trims it with ...
199 my $max = shift || 0;
200 if (2 < $max and $max < length($str)) {
201 substr($str, $max - 3) = '...';
206 # Takes two packages and an optional cache. Says whether the
207 # first inherits from the second.
209 # Recursive versions of this have to work to avoid certain
210 # possible endless loops, and when following long chains of
211 # inheritance are less efficient.
215 my $cache = shift || {};
216 my ($known, $partial) = get_status
($cache, $child);
217 # Figure out consequences until we have an answer
218 while (@
$partial and not exists $known->{$parent}) {
219 my $anc = shift @
$partial;
220 next if exists $known->{$anc};
222 my ($anc_knows, $anc_partial) = get_status
($cache, $anc);
223 my @found = keys %$anc_knows;
224 @
$known{@found} = ();
225 push @
$partial, @
$anc_partial;
227 return exists $known->{$parent};
230 # Takes a package and gives a list of those trusted directly
231 sub trusts_directly
{
235 return @
{"$class\::CARP_NOT"}
236 ? @
{"$class\::CARP_NOT"}