1 # Carp::Heavy uses some variables in common with Carp.
6 Carp 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, $MaxLenArg, $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 # Reuse the @args array to avoid warnings. :-)
32 local @args = map {Carp::format_arg($_)} @args;
33 if ($MaxArgNums and @args > $MaxArgNums) { # More than we want to show?
37 # Push the args onto the subroutine
38 $sub_name .= '(' . join (',', @args) . ')';
40 $call_info{sub_name} = $sub_name;
41 return wantarray() ? %call_info : \%call_info;
44 # Transform an argument to a function into a string.
47 if (not defined($arg)) {
51 $arg = defined($overload::VERSION) ? overload::StrVal($arg) : "$arg";
54 $arg = str_len_trim($arg, $MaxLenArg);
57 $arg = "'$arg'" unless $arg =~ /^-?[\d.]+\z/;
59 # The following handling of "control chars" is direct from
60 # the original code - I think it is broken on Unicode though.
62 $arg =~ s/([[:cntrl:]]|[[^:ascii:]])/sprintf("\\x{%x}",ord($1))/eg;
66 # Takes an inheritance cache and a package and returns
67 # an anon hash of known inheritances and anon array of
68 # inheritances which consequences have not been figured
73 $cache->{$pkg} ||= [{$pkg => $pkg}, [trusts_directly($pkg)]];
74 return @{$cache->{$pkg}};
77 # Takes the info from caller() and figures out the name of
78 # the sub/require/eval
81 if (defined($info->{eval})) {
82 my $eval = $info->{eval};
83 if ($info->{is_require}) {
84 return "require $eval";
87 $eval =~ s/([\\\'])/\\$1/g;
88 return str_len_trim($eval, $MaxEvalLen);
92 return ($info->{sub} eq '(eval)') ? 'eval {...}' : $info->{sub};
95 # Figures out what call (from the point of view of the caller)
96 # the long error backtrace should start at.
101 my $pkg = caller(++$i);
102 unless(defined($pkg)) {
103 # This *shouldn't* happen.
106 $i = long_error_loc();
110 # OK, now I am irritated.
114 redo if $CarpInternal{$pkg};
115 redo unless 0 > --$lvl;
116 redo if $Internal{$pkg};
123 return @_ if ref($_[0]); # WHAT IS THIS FOR???
124 my $i = long_error_loc();
125 return ret_backtrace($i, @_);
128 # Returns a full stack backtrace starting from where it is
131 my ($i, @error) = @_;
133 my $err = join '', @error;
137 if (defined &Thread::tid) {
138 my $tid = Thread->self->tid;
139 $tid_msg = " thread $tid" if $tid;
146 my %i = caller_info($i);
147 $mess = "$err at $i{file} line $i{line}$tid_msg\n";
150 while (my %i = caller_info(++$i)) {
151 $mess .= "\t$i{sub_name} called at $i{file} line $i{line}$tid_msg\n";
154 return $mess || $err;
158 my ($i, @error) = @_;
160 my $err = join '', @error;
164 if (defined &Thread::tid) {
165 my $tid = Thread->self->tid;
166 $tid_msg = " thread $tid" if $tid;
169 my %i = caller_info($i);
170 return "$err at $i{file} line $i{line}$tid_msg\n";
174 sub short_error_loc {
177 my $lvl = $CarpLevel;
179 my $called = caller($i++);
180 my $caller = caller($i);
181 return 0 unless defined($caller); # What happened?
182 redo if $Internal{$caller};
183 redo if $CarpInternal{$called};
184 redo if trusts($called, $caller, $cache);
185 redo if trusts($caller, $called, $cache);
186 redo unless 0 > --$lvl;
191 sub shortmess_heavy {
192 return longmess_heavy(@_) if $Verbose;
193 return @_ if ref($_[0]); # WHAT IS THIS FOR???
194 my $i = short_error_loc();
203 # If a string is too long, trims it with ...
206 my $max = shift || 0;
207 if (2 < $max and $max < length($str)) {
208 substr($str, $max - 3) = '...';
213 # Takes two packages and an optional cache. Says whether the
214 # first inherits from the second.
216 # Recursive versions of this have to work to avoid certain
217 # possible endless loops, and when following long chains of
218 # inheritance are less efficient.
222 my $cache = shift || {};
223 my ($known, $partial) = get_status($cache, $child);
224 # Figure out consequences until we have an answer
225 while (@$partial and not exists $known->{$parent}) {
226 my $anc = shift @$partial;
227 next if exists $known->{$anc};
229 my ($anc_knows, $anc_partial) = get_status($cache, $anc);
230 my @found = keys %$anc_knows;
231 @$known{@found} = ();
232 push @$partial, @$anc_partial;
234 return exists $known->{$parent};
237 # Takes a package and gives a list of those trusted directly
238 sub trusts_directly {
240 return @{"$class\::ISA"};