1Carp(3) User Contributed Perl Documentation Carp(3)
2
3
4
6 Carp - alternative warn and die for modules
7
9 use Carp;
10
11 # warn user (from perspective of caller)
12 carp "string trimmed to 80 chars";
13
14 # die of errors (from perspective of caller)
15 croak "We're outta here!";
16
17 # die of errors with stack backtrace
18 confess "not implemented";
19
20 # cluck not exported by default
21 use Carp qw(cluck);
22 cluck "This is how we got here!";
23
25 The Carp routines are useful in your own modules because they act like
26 die() or warn(), but with a message which is more likely to be useful
27 to a user of your module. In the case of cluck, confess, and longmess
28 that context is a summary of every call in the call-stack. For a
29 shorter message you can use "carp" or "croak" which report the error as
30 being from where your module was called. There is no guarantee that
31 that is where the error was, but it is a good educated guess.
32
33 You can also alter the way the output and logic of "Carp" works, by
34 changing some global variables in the "Carp" namespace. See the section
35 on "GLOBAL VARIABLES" below.
36
37 Here is a more complete description of how "carp" and "croak" work.
38 What they do is search the call-stack for a function call stack where
39 they have not been told that there shouldn't be an error. If every
40 call is marked safe, they give up and give a full stack backtrace
41 instead. In other words they presume that the first likely looking
42 potential suspect is guilty. Their rules for telling whether a call
43 shouldn't generate errors work as follows:
44
45 1. Any call from a package to itself is safe.
46
47 2. Packages claim that there won't be errors on calls to or from
48 packages explicitly marked as safe by inclusion in @CARP_NOT, or
49 (if that array is empty) @ISA. The ability to override what @ISA
50 says is new in 5.8.
51
52 3. The trust in item 2 is transitive. If A trusts B, and B trusts C,
53 then A trusts C. So if you do not override @ISA with @CARP_NOT,
54 then this trust relationship is identical to, "inherits from".
55
56 4. Any call from an internal Perl module is safe. (Nothing keeps user
57 modules from marking themselves as internal to Perl, but this
58 practice is discouraged.)
59
60 5. Any call to Perl's warning system (eg Carp itself) is safe. (This
61 rule is what keeps it from reporting the error at the point where
62 you call "carp" or "croak".)
63
64 6. $Carp::CarpLevel can be set to skip a fixed number of additional
65 call levels. Using this is not recommended because it is very
66 difficult to get it to behave correctly.
67
68 Forcing a Stack Trace
69 As a debugging aid, you can force Carp to treat a croak as a confess
70 and a carp as a cluck across all modules. In other words, force a
71 detailed stack trace to be given. This can be very helpful when trying
72 to understand why, or from where, a warning or error is being
73 generated.
74
75 This feature is enabled by 'importing' the non-existent symbol
76 'verbose'. You would typically enable it by saying
77
78 perl -MCarp=verbose script.pl
79
80 or by including the string "-MCarp=verbose" in the PERL5OPT environment
81 variable.
82
83 Alternately, you can set the global variable $Carp::Verbose to true.
84 See the "GLOBAL VARIABLES" section below.
85
87 $Carp::MaxEvalLen
88 This variable determines how many characters of a string-eval are to be
89 shown in the output. Use a value of 0 to show all text.
90
91 Defaults to 0.
92
93 $Carp::MaxArgLen
94 This variable determines how many characters of each argument to a
95 function to print. Use a value of 0 to show the full length of the
96 argument.
97
98 Defaults to 64.
99
100 $Carp::MaxArgNums
101 This variable determines how many arguments to each function to show.
102 Use a value of 0 to show all arguments to a function call.
103
104 Defaults to 8.
105
106 $Carp::Verbose
107 This variable makes "carp" and "croak" generate stack backtraces just
108 like "cluck" and "confess". This is how "use Carp 'verbose'" is
109 implemented internally.
110
111 Defaults to 0.
112
113 @CARP_NOT
114 This variable, in your package, says which packages are not to be
115 considered as the location of an error. The "carp()" and "cluck()"
116 functions will skip over callers when reporting where an error
117 occurred.
118
119 NB: This variable must be in the package's symbol table, thus:
120
121 # These work
122 our @CARP_NOT; # file scope
123 use vars qw(@CARP_NOT); # package scope
124 @My::Package::CARP_NOT = ... ; # explicit package variable
125
126 # These don't work
127 sub xyz { ... @CARP_NOT = ... } # w/o declarations above
128 my @CARP_NOT; # even at top-level
129
130 Example of use:
131
132 package My::Carping::Package;
133 use Carp;
134 our @CARP_NOT;
135 sub bar { .... or _error('Wrong input') }
136 sub _error {
137 # temporary control of where'ness, __PACKAGE__ is implicit
138 local @CARP_NOT = qw(My::Friendly::Caller);
139 carp(@_)
140 }
141
142 This would make "Carp" report the error as coming from a caller not in
143 "My::Carping::Package", nor from "My::Friendly::Caller".
144
145 Also read the "DESCRIPTION" section above, about how "Carp" decides
146 where the error is reported from.
147
148 Use @CARP_NOT, instead of $Carp::CarpLevel.
149
150 Overrides "Carp"'s use of @ISA.
151
152 %Carp::Internal
153 This says what packages are internal to Perl. "Carp" will never report
154 an error as being from a line in a package that is internal to Perl.
155 For example:
156
157 $Carp::Internal{ (__PACKAGE__) }++;
158 # time passes...
159 sub foo { ... or confess("whatever") };
160
161 would give a full stack backtrace starting from the first caller
162 outside of __PACKAGE__. (Unless that package was also internal to
163 Perl.)
164
165 %Carp::CarpInternal
166 This says which packages are internal to Perl's warning system. For
167 generating a full stack backtrace this is the same as being internal to
168 Perl, the stack backtrace will not start inside packages that are
169 listed in %Carp::CarpInternal. But it is slightly different for the
170 summary message generated by "carp" or "croak". There errors will not
171 be reported on any lines that are calling packages in
172 %Carp::CarpInternal.
173
174 For example "Carp" itself is listed in %Carp::CarpInternal. Therefore
175 the full stack backtrace from "confess" will not start inside of
176 "Carp", and the short message from calling "croak" is not placed on the
177 line where "croak" was called.
178
179 $Carp::CarpLevel
180 This variable determines how many additional call frames are to be
181 skipped that would not otherwise be when reporting where an error
182 occurred on a call to one of "Carp"'s functions. It is fairly easy to
183 count these call frames on calls that generate a full stack backtrace.
184 However it is much harder to do this accounting for calls that generate
185 a short message. Usually people skip too many call frames. If they
186 are lucky they skip enough that "Carp" goes all of the way through the
187 call stack, realizes that something is wrong, and then generates a full
188 stack backtrace. If they are unlucky then the error is reported from
189 somewhere misleading very high in the call stack.
190
191 Therefore it is best to avoid $Carp::CarpLevel. Instead use @CARP_NOT,
192 %Carp::Internal and %Carp::CarpInternal.
193
194 Defaults to 0.
195
197 The Carp routines don't handle exception objects currently. If called
198 with a first argument that is a reference, they simply call die() or
199 warn(), as appropriate.
200
202 Carp::Always, Carp::Clan
203
205 The Carp module first appeared in Larry Wall's perl 5.000 distribution.
206 Since then it has been modified by several of the perl 5 porters.
207 Andrew Main (Zefram) <zefram@fysh.org> divested Carp into an
208 independent distribution.
209
211 Copyright (C) 1994-2012 Larry Wall
212
213 Copyright (C) 2011, 2012 Andrew Main (Zefram) <zefram@fysh.org>
214
216 This module is free software; you can redistribute it and/or modify it
217 under the same terms as Perl itself.
218
219
220
221perl v5.16.3 2012-06-18 Carp(3)