1Carp(3pm) Perl Programmers Reference Guide Carp(3pm)
2
3
4
6 carp - warn of errors (from perspective of caller)
7
8 cluck - warn of errors with stack backtrace
9 (not exported by default)
10
11 croak - die of errors (from perspective of caller)
12
13 confess - die of errors with stack backtrace
14
16 use Carp;
17 croak "We're outta here!";
18
19 use Carp qw(cluck);
20 cluck "This is how we got here!";
21
23 The Carp routines are useful in your own modules because they act like
24 die() or warn(), but with a message which is more likely to be useful
25 to a user of your module. In the case of cluck, confess, and longmess
26 that context is a summary of every call in the call-stack. For a
27 shorter message you can use "carp" or "croak" which report the error as
28 being from where your module was called. There is no guarantee that
29 that is where the error was, but it is a good educated guess.
30
31 You can also alter the way the output and logic of "Carp" works, by
32 changing some global variables in the "Carp" namespace. See the section
33 on "GLOBAL VARIABLES" below.
34
35 Here is a more complete description of how "carp" and "croak" work.
36 What they do is search the call-stack for a function call stack where
37 they have not been told that there shouldn't be an error. If every
38 call is marked safe, they give up and give a full stack backtrace
39 instead. In other words they presume that the first likely looking
40 potential suspect is guilty. Their rules for telling whether a call
41 shouldn't generate errors work as follows:
42
43 1. Any call from a package to itself is safe.
44
45 2. Packages claim that there won't be errors on calls to or from
46 packages explicitly marked as safe by inclusion in @CARP_NOT, or
47 (if that array is empty) @ISA. The ability to override what @ISA
48 says is new in 5.8.
49
50 3. The trust in item 2 is transitive. If A trusts B, and B trusts C,
51 then A trusts C. So if you do not override @ISA with @CARP_NOT,
52 then this trust relationship is identical to, "inherits from".
53
54 4. Any call from an internal Perl module is safe. (Nothing keeps user
55 modules from marking themselves as internal to Perl, but this
56 practice is discouraged.)
57
58 5. Any call to Perl's warning system (eg Carp itself) is safe. (This
59 rule is what keeps it from reporting the error at the point where
60 you call "carp" or "croak".)
61
62 6. $Carp::CarpLevel can be set to skip a fixed number of additional
63 call levels. Using this is not recommended because it is very
64 difficult to get it to behave correctly.
65
66 Forcing a Stack Trace
67 As a debugging aid, you can force Carp to treat a croak as a confess
68 and a carp as a cluck across all modules. In other words, force a
69 detailed stack trace to be given. This can be very helpful when trying
70 to understand why, or from where, a warning or error is being
71 generated.
72
73 This feature is enabled by 'importing' the non-existent symbol
74 'verbose'. You would typically enable it by saying
75
76 perl -MCarp=verbose script.pl
77
78 or by including the string "-MCarp=verbose" in the PERL5OPT environment
79 variable.
80
81 Alternately, you can set the global variable $Carp::Verbose to true.
82 See the "GLOBAL VARIABLES" section below.
83
85 $Carp::MaxEvalLen
86 This variable determines how many characters of a string-eval are to be
87 shown in the output. Use a value of 0 to show all text.
88
89 Defaults to 0.
90
91 $Carp::MaxArgLen
92 This variable determines how many characters of each argument to a
93 function to print. Use a value of 0 to show the full length of the
94 argument.
95
96 Defaults to 64.
97
98 $Carp::MaxArgNums
99 This variable determines how many arguments to each function to show.
100 Use a value of 0 to show all arguments to a function call.
101
102 Defaults to 8.
103
104 $Carp::Verbose
105 This variable makes "carp" and "cluck" generate stack backtraces just
106 like "cluck" and "confess". This is how "use Carp 'verbose'" is
107 implemented internally.
108
109 Defaults to 0.
110
111 %Carp::Internal
112 This says what packages are internal to Perl. "Carp" will never report
113 an error as being from a line in a package that is internal to Perl.
114 For example:
115
116 $Carp::Internal{ (__PACKAGE__) }++;
117 # time passes...
118 sub foo { ... or confess("whatever") };
119
120 would give a full stack backtrace starting from the first caller
121 outside of __PACKAGE__. (Unless that package was also internal to
122 Perl.)
123
124 %Carp::CarpInternal
125 This says which packages are internal to Perl's warning system. For
126 generating a full stack backtrace this is the same as being internal to
127 Perl, the stack backtrace will not start inside packages that are
128 listed in %Carp::CarpInternal. But it is slightly different for the
129 summary message generated by "carp" or "croak". There errors will not
130 be reported on any lines that are calling packages in
131 %Carp::CarpInternal.
132
133 For example "Carp" itself is listed in %Carp::CarpInternal. Therefore
134 the full stack backtrace from "confess" will not start inside of
135 "Carp", and the short message from calling "croak" is not placed on the
136 line where "croak" was called.
137
138 $Carp::CarpLevel
139 This variable determines how many additional call frames are to be
140 skipped that would not otherwise be when reporting where an error
141 occurred on a call to one of "Carp"'s functions. It is fairly easy to
142 count these call frames on calls that generate a full stack backtrace.
143 However it is much harder to do this accounting for calls that generate
144 a short message. Usually people skip too many call frames. If they
145 are lucky they skip enough that "Carp" goes all of the way through the
146 call stack, realizes that something is wrong, and then generates a full
147 stack backtrace. If they are unlucky then the error is reported from
148 somewhere misleading very high in the call stack.
149
150 Therefore it is best to avoid $Carp::CarpLevel. Instead use @CARP_NOT,
151 %Carp::Internal and %Carp::CarpInternal.
152
153 Defaults to 0.
154
156 The Carp routines don't handle exception objects currently. If called
157 with a first argument that is a reference, they simply call die() or
158 warn(), as appropriate.
159
160
161
162perl v5.10.1 2009-07-03 Carp(3pm)