Commit | Line | Data |
a0d0e21e |
1 | package Carp; |
2 | |
f06db76b |
3 | =head1 NAME |
4 | |
5 | carp - warn of errors (from perspective of caller) |
6 | |
7 | croak - die of errors (from perspective of caller) |
8 | |
9 | confess - die of errors with stack backtrace |
10 | |
11 | =head1 SYNOPSIS |
12 | |
13 | use Carp; |
14 | croak "We're outta here!"; |
15 | |
16 | =head1 DESCRIPTION |
17 | |
18 | The Carp routines are useful in your own modules because |
19 | they act like die() or warn(), but report where the error |
20 | was in the code they were called from. Thus if you have a |
21 | routine Foo() that has a carp() in it, then the carp() |
22 | will report the error as occurring where Foo() was called, |
23 | not where carp() was called. |
24 | |
25 | =cut |
26 | |
a0d0e21e |
27 | # This package implements handy routines for modules that wish to throw |
28 | # exceptions outside of the current package. |
29 | |
748a9306 |
30 | $CarpLevel = 0; # How many extra package levels to skip on carp. |
c07a80fd |
31 | $MaxEvalLen = 0; # How much eval '...text...' to show. 0 = all. |
748a9306 |
32 | |
a0d0e21e |
33 | require Exporter; |
34 | @ISA = Exporter; |
35 | @EXPORT = qw(confess croak carp); |
36 | |
37 | sub longmess { |
38 | my $error = shift; |
39 | my $mess = ""; |
748a9306 |
40 | my $i = 1 + $CarpLevel; |
c07a80fd |
41 | my ($pack,$file,$line,$sub,$eval,$require); |
42 | while (($pack,$file,$line,$sub,undef,undef,$eval,$require) = caller($i++)) { |
c1bce5d7 |
43 | if ($error =~ m/\n$/) { |
44 | $mess .= $error; |
45 | } else { |
c07a80fd |
46 | if (defined $eval) { |
47 | if ($require) { |
48 | $sub = "require $eval"; |
49 | } else { |
9c7d8621 |
50 | $eval =~ s/([\\\'])/\\$1/g; |
c07a80fd |
51 | if ($MaxEvalLen && length($eval) > $MaxEvalLen) { |
52 | substr($eval,$MaxEvalLen) = '...'; |
53 | } |
54 | $sub = "eval '$eval'"; |
55 | } |
56 | } elsif ($sub eq '(eval)') { |
57 | $sub = 'eval {...}'; |
58 | } |
c1bce5d7 |
59 | $mess .= "\t$sub " if $error eq "called"; |
60 | $mess .= "$error at $file line $line\n"; |
61 | } |
a0d0e21e |
62 | $error = "called"; |
63 | } |
64 | $mess || $error; |
65 | } |
66 | |
748a9306 |
67 | sub shortmess { # Short-circuit &longmess if called via multiple packages |
68 | my $error = $_[0]; # Instead of "shift" |
9c7d8621 |
69 | my ($prevpack) = caller(1); |
748a9306 |
70 | my $extra = $CarpLevel; |
a0d0e21e |
71 | my $i = 2; |
c07a80fd |
72 | my ($pack,$file,$line); |
9c7d8621 |
73 | my %isa = ($prevpack,1); |
74 | |
75 | @isa{@{"${prevpack}::ISA"}} = () |
76 | if(defined @{"${prevpack}::ISA"}); |
77 | |
c07a80fd |
78 | while (($pack,$file,$line) = caller($i++)) { |
9c7d8621 |
79 | if(defined @{$pack . "::ISA"}) { |
80 | my @i = @{$pack . "::ISA"}; |
81 | my %i; |
82 | @i{@i} = (); |
83 | @isa{@i,$pack} = () |
84 | if(exists $i{$prevpack} || exists $isa{$pack}); |
85 | } |
86 | |
87 | next |
88 | if(exists $isa{$pack}); |
89 | |
90 | if ($extra-- > 0) { |
91 | %isa = ($pack,1); |
92 | @isa{@{$pack . "::ISA"}} = () |
93 | if(defined @{$pack . "::ISA"}); |
94 | } |
95 | else { |
96 | return "$error at $file line $line\n"; |
748a9306 |
97 | } |
a0d0e21e |
98 | } |
9c7d8621 |
99 | continue { |
100 | $prevpack = $pack; |
101 | } |
102 | |
748a9306 |
103 | goto &longmess; |
a0d0e21e |
104 | } |
105 | |
106 | sub confess { die longmess @_; } |
107 | sub croak { die shortmess @_; } |
108 | sub carp { warn shortmess @_; } |
109 | |
748a9306 |
110 | 1; |