1 # Carp::Heavy uses some variables in common with Carp.
4 # use strict; # not yet
6 # On one line so MakeMaker will see it.
7 use Carp; our $VERSION = $Carp::VERSION;
9 our ($CarpLevel, $MaxArgNums, $MaxEvalLen, $MaxLenArg, $Verbose);
12 my $i = shift(@_) + 1;
16 qw(pack file line sub has_args wantarray evaltext is_require)
19 unless (defined $call_info{pack}) {
23 my $sub_name = Carp::get_subname(\%call_info);
24 if ($call_info{has_args}) {
25 # Reuse the @args array to avoid warnings. :-)
26 local @args = map {Carp::format_arg($_)} @args;
27 if ($MaxArgNums and @args > $MaxArgNums) { # More than we want to show?
31 # Push the args onto the subroutine
32 $sub_name .= '(' . join (',', @args) . ')';
34 $call_info{sub_name} = $sub_name;
35 return wantarray() ? %call_info : \%call_info;
38 # Transform an argument to a function into a string.
41 if (not defined($arg)) {
45 $arg .= ''; # Make it a string;
48 $arg = str_len_trim($arg, $MaxLenArg);
51 $arg = "'$arg'" unless $arg =~ /^-?[\d.]+\z/;
53 # The following handling of "control chars" is direct from
54 # the original code - I think it is broken on Unicode though.
56 $arg =~ s/([[:cntrl:]]|[[^:ascii:]])/sprintf("\\x{%x}",ord($1))/eg;
60 # Takes an inheritance cache and a package and returns
61 # an anon hash of known inheritances and anon array of
62 # inheritances which consequences have not been figured
67 $cache->{$pkg} ||= [{$pkg => $pkg}, [trusts_directly($pkg)]];
68 return @{$cache->{$pkg}};
71 # Takes the info from caller() and figures out the name of
72 # the sub/require/eval
75 if (defined($info->{eval})) {
76 my $eval = $info->{eval};
77 if ($info->{is_require}) {
78 return "require $eval";
81 $eval =~ s/([\\\'])/\\$1/g;
82 return str_len_trim($eval, $MaxEvalLen);
86 return ($info->{sub} eq '(eval)') ? 'eval {...}' : $info->{sub};
89 # Figures out what call (from the point of view of the caller)
90 # the long error backtrace should start at.
95 my $pkg = caller(++$i);
96 unless(defined($pkg)) {
97 # This *shouldn't* happen.
100 $i = long_error_loc();
104 # OK, now I am irritated.
108 redo if $CarpInternal{$pkg};
109 redo unless 0 > --$lvl;
110 redo if $Internal{$pkg};
117 return @_ if ref($_[0]); # WHAT IS THIS FOR???
118 my $i = long_error_loc();
119 return ret_backtrace($i, @_);
122 # Returns a full stack backtrace starting from where it is
125 my ($i, @error) = @_;
127 my $err = join '', @error;
131 if (defined &Thread::tid) {
132 my $tid = Thread->self->tid;
133 $tid_msg = " thread $tid" if $tid;
140 my %i = caller_info($i);
141 $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";
148 return $mess || $err;
152 my ($i, @error) = @_;
154 my $err = join '', @error;
158 if (defined &Thread::tid) {
159 my $tid = Thread->self->tid;
160 $tid_msg = " thread $tid" if $tid;
163 my %i = caller_info($i);
164 return "$err at $i{file} line $i{line}$tid_msg\n";
168 sub short_error_loc {
171 my $lvl = $CarpLevel;
173 my $called = caller($i++);
174 my $caller = caller($i);
175 return 0 unless defined($caller); # What happened?
176 redo if $Internal{$caller};
177 redo if $CarpInternal{$called};
178 redo if trusts($called, $caller, $cache);
179 redo if trusts($caller, $called, $cache);
180 redo unless 0 > --$lvl;
185 sub shortmess_heavy {
186 return longmess_heavy(@_) if $Verbose;
187 return @_ if ref($_[0]); # WHAT IS THIS FOR???
188 my $i = short_error_loc();
197 # If a string is too long, trims it with ...
200 my $max = shift || 0;
201 if (2 < $max and $max < length($str)) {
202 substr($str, $max - 3) = '...';
207 # Takes two packages and an optional cache. Says whether the
208 # first inherits from the second.
210 # Recursive versions of this have to work to avoid certain
211 # possible endless loops, and when following long chains of
212 # inheritance are less efficient.
216 my $cache = shift || {};
217 my ($known, $partial) = get_status($cache, $child);
218 # Figure out consequences until we have an answer
219 while (@$partial and not exists $known->{$parent}) {
220 my $anc = shift @$partial;
221 next if exists $known->{$anc};
223 my ($anc_knows, $anc_partial) = get_status($cache, $anc);
224 my @found = keys %$anc_knows;
225 @$known{@found} = ();
226 push @$partial, @$anc_partial;
228 return exists $known->{$parent};
231 # Takes a package and gives a list of those trusted directly
232 sub trusts_directly {
234 return @{"$class\::ISA"};