7 carp - warn of errors (from perspective of caller)
9 cluck - warn of errors with stack backtrace
10 (not exported by default)
12 croak - die of errors (from perspective of caller)
14 confess - die of errors with stack backtrace
16 shortmess - return the message that carp and croak produce
18 longmess - return the message that cluck and confess produce
23 croak "We're outta here!";
26 cluck "This is how we got here!";
28 print FH Carp::shortmess("This will have caller's details added");
29 print FH Carp::longmess("This will have stack backtrace added");
33 The Carp routines are useful in your own modules because
34 they act like die() or warn(), but with a message which is more
35 likely to be useful to a user of your module. In the case of
36 cluck, confess, and longmess that context is a summary of every
37 call in the call-stack. For a shorter message you can use carp,
38 croak or shortmess which report the error as being from where
39 your module was called. There is no guarantee that that is where
40 the error was, but it is a good educated guess.
42 Here is a more complete description of how shortmess works. What
43 it does is search the call-stack for a function call stack where
44 it hasn't been told that there shouldn't be an error. If every
45 call is marked safe, it then gives up and gives a full stack
46 backtrace instead. In other words it presumes that the first likely
47 looking potential suspect is guilty. Its rules for telling whether
48 a call shouldn't generate errors work as follows:
54 Any call from a package to itself is safe.
58 Packages claim that there won't be errors on calls to or from
59 packages explicitly marked as safe by inclusion in @CARP_NOT, or
60 (if that array is empty) @ISA. The ability to override what
61 @ISA says is new in 5.8.
65 The trust in item 2 is transitive. If A trusts B, and B
66 trusts C, then A trusts C. So if you do not override @ISA
67 with @CARP_NOT, then this trust relationship is identical to,
72 Any call from an internal Perl module is safe. (Nothing keeps
73 user modules from marking themselves as internal to Perl, but
74 this practice is discouraged.)
78 Any call to Carp is safe. (This rule is what keeps it from
79 reporting the error where you call carp/croak/shortmess.)
83 =head2 Forcing a Stack Trace
85 As a debugging aid, you can force Carp to treat a croak as a confess
86 and a carp as a cluck across I<all> modules. In other words, force a
87 detailed stack trace to be given. This can be very helpful when trying
88 to understand why, or from where, a warning or error is being generated.
90 This feature is enabled by 'importing' the non-existent symbol
91 'verbose'. You would typically enable it by saying
93 perl -MCarp=verbose script.pl
95 or by including the string C<MCarp=verbose> in the PERL5OPT
100 The Carp routines don't handle exception objects currently.
101 If called with a first argument that is a reference, they simply
102 call die() or warn(), as appropriate.
106 # This package is heavily used. Be small. Be fast. Be good.
108 # Comments added by Andy Wardley <abw@kfs.org> 09-Apr-98, based on an
109 # _almost_ complete understanding of the package. Corrections and
110 # comments are welcome.
112 # The members of %Internal are packages that are internal to perl.
113 # Carp will not report errors from within these packages if it
114 # can. The members of %CarpInternal are internal to Perl's warning
115 # system. Carp will not report errors from within these packages
116 # either, and will not report calls *to* these packages for carp and
117 # croak. They replace $CarpLevel, which is deprecated. The
118 # $Max(EvalLen|(Arg(Len|Nums)) variables are used to specify how the eval
119 # text and function arguments should be formatted when printed.
121 $CarpInternal{Carp}++;
122 $CarpLevel = 0; # How many extra package levels to skip on carp.
123 # How many calls to skip on confess.
124 # Reconciling these notions is hard, use
125 # %Internal and %CarpInternal instead.
126 $MaxEvalLen = 0; # How much eval '...text...' to show. 0 = all.
127 $MaxArgLen = 64; # How much of each argument to print. 0 = all.
128 $MaxArgNums = 8; # How many arguments to print. 0 = all.
129 $Verbose = 0; # If true then make shortmess call longmess instead
133 @EXPORT = qw(confess croak carp);
134 @EXPORT_OK = qw(cluck verbose longmess shortmess);
135 @EXPORT_FAIL = qw(verbose); # hook to enable verbose mode
138 # if the caller specifies verbose usage ("perl -MCarp=verbose script.pl")
139 # then the following method will be called by the Exporter which knows
140 # to do this thanks to @EXPORT_FAIL, above. $_[1] will contain the word
145 $Verbose = shift if $_[0] eq 'verbose';
150 # longmess() crawls all the way up the stack reporting on all the function
151 # calls made. The error string, $error, is originally constructed from the
152 # arguments passed into longmess() via confess(), cluck() or shortmess().
153 # This gets appended with the stack trace messages which are generated for
154 # each function call on the stack.
157 { local $@; require Carp::Heavy; } # XXX fix require to not clear $@?
158 # Icky backwards compatibility wrapper. :-(
159 my $call_pack = caller();
160 if ($Internal{$call_pack} or $CarpInternal{$call_pack}) {
161 return longmess_heavy(@_);
164 local $CarpLevel = $CarpLevel + 1;
165 return longmess_heavy(@_);
170 # shortmess() is called by carp() and croak() to skip all the way up to
171 # the top-level caller's package and report the error from there. confess()
172 # and cluck() generate a full stack trace so they call longmess() to
173 # generate that. In verbose mode shortmess() calls longmess() so
174 # you always get a stack trace
176 sub shortmess { # Short-circuit &longmess if called via multiple packages
177 { local $@; require Carp::Heavy; } # XXX fix require to not clear $@?
178 # Icky backwards compatibility wrapper. :-(
179 my $call_pack = caller();
180 local @CARP_NOT = caller();
185 # the following four functions call longmess() or shortmess() depending on
186 # whether they should generate a full stack trace (confess() and cluck())
187 # or simply report the caller's package (croak() and carp()), respectively.
188 # confess() and croak() die, carp() and cluck() warn.
190 sub croak { die shortmess @_ }
191 sub confess { die longmess @_ }
192 sub carp { warn shortmess @_ }
193 sub cluck { warn longmess @_ }