1Carp(3pm)              Perl Programmers Reference Guide              Carp(3pm)
2
3
4

NAME

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

SYNOPSIS

16           use Carp;
17           croak "We're outta here!";
18
19           use Carp qw(cluck);
20           cluck "This is how we got here!";
21

DESCRIPTION

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

GLOBAL VARIABLES

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 "croak" 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_NOT
112       This variable, in your package, says which packages are not to be
113       considered as the location of an error. The "carp()" and "cluck()"
114       functions will skip over callers when reporting where an error
115       occurred.
116
117       NB: This variable must be in the package's symbol table, thus:
118
119           # These work
120           our @CARP_NOT; # file scope
121           use vars qw(@CARP_NOT); # package scope
122           @My::Package::CARP_NOT = ... ; # explicit package variable
123
124           # These don't work
125           sub xyz { ... @CARP_NOT = ... } # w/o declarations above
126           my @CARP_NOT; # even at top-level
127
128       Example of use:
129
130           package My::Carping::Package;
131           use Carp;
132           our @CARP_NOT;
133           sub bar     { .... or _error('Wrong input') }
134           sub _error  {
135               # temporary control of where'ness, __PACKAGE__ is implicit
136               local @CARP_NOT = qw(My::Friendly::Caller);
137               carp(@_)
138           }
139
140       This would make "Carp" report the error as coming from a caller not in
141       "My::Carping::Package", nor from "My::Friendly::Caller".
142
143       Also read the "DESCRIPTION" section above, about how "Carp" decides
144       where the error is reported from.
145
146       Use @CARP_NOT, instead of $Carp::CarpLevel.
147
148       Overrides "Carp"'s use of @ISA.
149
150   %Carp::Internal
151       This says what packages are internal to Perl.  "Carp" will never report
152       an error as being from a line in a package that is internal to Perl.
153       For example:
154
155           $Carp::Internal{ (__PACKAGE__) }++;
156           # time passes...
157           sub foo { ... or confess("whatever") };
158
159       would give a full stack backtrace starting from the first caller
160       outside of __PACKAGE__.  (Unless that package was also internal to
161       Perl.)
162
163   %Carp::CarpInternal
164       This says which packages are internal to Perl's warning system.  For
165       generating a full stack backtrace this is the same as being internal to
166       Perl, the stack backtrace will not start inside packages that are
167       listed in %Carp::CarpInternal.  But it is slightly different for the
168       summary message generated by "carp" or "croak".  There errors will not
169       be reported on any lines that are calling packages in
170       %Carp::CarpInternal.
171
172       For example "Carp" itself is listed in %Carp::CarpInternal.  Therefore
173       the full stack backtrace from "confess" will not start inside of
174       "Carp", and the short message from calling "croak" is not placed on the
175       line where "croak" was called.
176
177   $Carp::CarpLevel
178       This variable determines how many additional call frames are to be
179       skipped that would not otherwise be when reporting where an error
180       occurred on a call to one of "Carp"'s functions.  It is fairly easy to
181       count these call frames on calls that generate a full stack backtrace.
182       However it is much harder to do this accounting for calls that generate
183       a short message.  Usually people skip too many call frames.  If they
184       are lucky they skip enough that "Carp" goes all of the way through the
185       call stack, realizes that something is wrong, and then generates a full
186       stack backtrace.  If they are unlucky then the error is reported from
187       somewhere misleading very high in the call stack.
188
189       Therefore it is best to avoid $Carp::CarpLevel.  Instead use @CARP_NOT,
190       %Carp::Internal and %Carp::CarpInternal.
191
192       Defaults to 0.
193

BUGS

195       The Carp routines don't handle exception objects currently.  If called
196       with a first argument that is a reference, they simply call die() or
197       warn(), as appropriate.
198
199
200
201perl v5.12.4                      2011-06-07                         Carp(3pm)
Impressum