Commit | Line | Data |
---|---|---|
a0d0e21e LW |
1 | package Carp; |
2 | ||
0e06870b CB |
3 | our $VERSION = '1.00'; |
4 | ||
f06db76b AD |
5 | =head1 NAME |
6 | ||
4d935a29 | 7 | carp - warn of errors (from perspective of caller) |
f06db76b | 8 | |
4d935a29 TB |
9 | cluck - warn of errors with stack backtrace |
10 | (not exported by default) | |
11 | ||
12 | croak - die of errors (from perspective of caller) | |
f06db76b AD |
13 | |
14 | confess - die of errors with stack backtrace | |
15 | ||
16 | =head1 SYNOPSIS | |
17 | ||
18 | use Carp; | |
19 | croak "We're outta here!"; | |
20 | ||
4d935a29 TB |
21 | use Carp qw(cluck); |
22 | cluck "This is how we got here!"; | |
23 | ||
f06db76b AD |
24 | =head1 DESCRIPTION |
25 | ||
26 | The Carp routines are useful in your own modules because | |
27 | they act like die() or warn(), but report where the error | |
28 | was in the code they were called from. Thus if you have a | |
29 | routine Foo() that has a carp() in it, then the carp() | |
30 | will report the error as occurring where Foo() was called, | |
31 | not where carp() was called. | |
32 | ||
4d935a29 TB |
33 | =head2 Forcing a Stack Trace |
34 | ||
35 | As a debugging aid, you can force Carp to treat a croak as a confess | |
36 | and a carp as a cluck across I<all> modules. In other words, force a | |
37 | detailed stack trace to be given. This can be very helpful when trying | |
38 | to understand why, or from where, a warning or error is being generated. | |
39 | ||
f610777f | 40 | This feature is enabled by 'importing' the non-existent symbol |
4d935a29 TB |
41 | 'verbose'. You would typically enable it by saying |
42 | ||
43 | perl -MCarp=verbose script.pl | |
44 | ||
45 | or by including the string C<MCarp=verbose> in the L<PERL5OPT> | |
46 | environment variable. | |
47 | ||
d2fe67be GS |
48 | =head1 BUGS |
49 | ||
50 | The Carp routines don't handle exception objects currently. | |
51 | If called with a first argument that is a reference, they simply | |
52 | call die() or warn(), as appropriate. | |
53 | ||
f06db76b AD |
54 | =cut |
55 | ||
4d935a29 | 56 | # This package is heavily used. Be small. Be fast. Be good. |
a0d0e21e | 57 | |
7b8d334a GS |
58 | # Comments added by Andy Wardley <abw@kfs.org> 09-Apr-98, based on an |
59 | # _almost_ complete understanding of the package. Corrections and | |
60 | # comments are welcome. | |
61 | ||
62 | # The $CarpLevel variable can be set to "strip off" extra caller levels for | |
63 | # those times when Carp calls are buried inside other functions. The | |
64 | # $Max(EvalLen|(Arg(Len|Nums)) variables are used to specify how the eval | |
65 | # text and function arguments should be formatted when printed. | |
66 | ||
748a9306 | 67 | $CarpLevel = 0; # How many extra package levels to skip on carp. |
c07a80fd | 68 | $MaxEvalLen = 0; # How much eval '...text...' to show. 0 = all. |
55497cff | 69 | $MaxArgLen = 64; # How much of each argument to print. 0 = all. |
70 | $MaxArgNums = 8; # How many arguments to print. 0 = all. | |
6ff81951 | 71 | $Verbose = 0; # If true then make shortmess call longmess instead |
748a9306 | 72 | |
0e06870b CB |
73 | $CarpInternal{Carp}++; |
74 | ||
a0d0e21e | 75 | require Exporter; |
fb73857a | 76 | @ISA = ('Exporter'); |
a0d0e21e | 77 | @EXPORT = qw(confess croak carp); |
4d935a29 TB |
78 | @EXPORT_OK = qw(cluck verbose); |
79 | @EXPORT_FAIL = qw(verbose); # hook to enable verbose mode | |
80 | ||
7b8d334a GS |
81 | |
82 | # if the caller specifies verbose usage ("perl -MCarp=verbose script.pl") | |
83 | # then the following method will be called by the Exporter which knows | |
84 | # to do this thanks to @EXPORT_FAIL, above. $_[1] will contain the word | |
85 | # 'verbose'. | |
86 | ||
4d935a29 TB |
87 | sub export_fail { |
88 | shift; | |
6ff81951 | 89 | $Verbose = shift if $_[0] eq 'verbose'; |
4d935a29 TB |
90 | return @_; |
91 | } | |
92 | ||
a0d0e21e | 93 | |
7b8d334a GS |
94 | # longmess() crawls all the way up the stack reporting on all the function |
95 | # calls made. The error string, $error, is originally constructed from the | |
96 | # arguments passed into longmess() via confess(), cluck() or shortmess(). | |
97 | # This gets appended with the stack trace messages which are generated for | |
98 | # each function call on the stack. | |
99 | ||
a0d0e21e | 100 | sub longmess { |
0bcd2fea | 101 | { local $@; require Carp::Heavy; } # XXX fix require to not clear $@? |
3b5ca523 | 102 | goto &longmess_heavy; |
a0d0e21e LW |
103 | } |
104 | ||
7b8d334a GS |
105 | |
106 | # shortmess() is called by carp() and croak() to skip all the way up to | |
107 | # the top-level caller's package and report the error from there. confess() | |
108 | # and cluck() generate a full stack trace so they call longmess() to | |
6ff81951 | 109 | # generate that. In verbose mode shortmess() calls longmess() so |
7b8d334a GS |
110 | # you always get a stack trace |
111 | ||
748a9306 | 112 | sub shortmess { # Short-circuit &longmess if called via multiple packages |
0bcd2fea | 113 | { local $@; require Carp::Heavy; } # XXX fix require to not clear $@? |
3b5ca523 | 114 | goto &shortmess_heavy; |
a0d0e21e LW |
115 | } |
116 | ||
7b8d334a GS |
117 | |
118 | # the following four functions call longmess() or shortmess() depending on | |
119 | # whether they should generate a full stack trace (confess() and cluck()) | |
120 | # or simply report the caller's package (croak() and carp()), respectively. | |
121 | # confess() and croak() die, carp() and cluck() warn. | |
122 | ||
123 | sub croak { die shortmess @_ } | |
124 | sub confess { die longmess @_ } | |
125 | sub carp { warn shortmess @_ } | |
126 | sub cluck { warn longmess @_ } | |
a0d0e21e | 127 | |
748a9306 | 128 | 1; |