1Carp(3)               User Contributed Perl Documentation              Carp(3)
2
3
4

NAME

6       Carp - alternative warn and die for modules
7

SYNOPSIS

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, longmess and shortmess not exported by default
21           use Carp qw(cluck longmess shortmess);
22           cluck "This is how we got here!"; # warn with stack backtrace
23           $long_message   = longmess( "message from cluck() or confess()" );
24           $short_message  = shortmess( "message from carp() or croak()" );
25

DESCRIPTION

27       The Carp routines are useful in your own modules because they act like
28       "die()" or "warn()", but with a message which is more likely to be
29       useful to a user of your module.  In the case of "cluck()" and
30       "confess()", that context is a summary of every call in the call-stack;
31       "longmess()" returns the contents of the error message.
32
33       For a shorter message you can use "carp()" or "croak()" which report
34       the error as being from where your module was called.  "shortmess()"
35       returns the contents of this error message.  There is no guarantee that
36       that is where the error was, but it is a good educated guess.
37
38       "Carp" takes care not to clobber the status variables $! and $^E in the
39       course of assembling its error messages.  This means that a
40       $SIG{__DIE__} or $SIG{__WARN__} handler can capture the error
41       information held in those variables, if it is required to augment the
42       error message, and if the code calling "Carp" left useful values there.
43       Of course, "Carp" can't guarantee the latter.
44
45       You can also alter the way the output and logic of "Carp" works, by
46       changing some global variables in the "Carp" namespace. See the section
47       on "GLOBAL VARIABLES" below.
48
49       Here is a more complete description of how "carp" and "croak" work.
50       What they do is search the call-stack for a function call stack where
51       they have not been told that there shouldn't be an error.  If every
52       call is marked safe, they give up and give a full stack backtrace
53       instead.  In other words they presume that the first likely looking
54       potential suspect is guilty.  Their rules for telling whether a call
55       shouldn't generate errors work as follows:
56
57       1.  Any call from a package to itself is safe.
58
59       2.  Packages claim that there won't be errors on calls to or from
60           packages explicitly marked as safe by inclusion in @CARP_NOT, or
61           (if that array is empty) @ISA.  The ability to override what @ISA
62           says is new in 5.8.
63
64       3.  The trust in item 2 is transitive.  If A trusts B, and B trusts C,
65           then A trusts C.  So if you do not override @ISA with @CARP_NOT,
66           then this trust relationship is identical to, "inherits from".
67
68       4.  Any call from an internal Perl module is safe.  (Nothing keeps user
69           modules from marking themselves as internal to Perl, but this
70           practice is discouraged.)
71
72       5.  Any call to Perl's warning system (eg Carp itself) is safe.  (This
73           rule is what keeps it from reporting the error at the point where
74           you call "carp" or "croak".)
75
76       6.  $Carp::CarpLevel can be set to skip a fixed number of additional
77           call levels.  Using this is not recommended because it is very
78           difficult to get it to behave correctly.
79
80   Forcing a Stack Trace
81       As a debugging aid, you can force Carp to treat a croak as a confess
82       and a carp as a cluck across all modules. In other words, force a
83       detailed stack trace to be given.  This can be very helpful when trying
84       to understand why, or from where, a warning or error is being
85       generated.
86
87       This feature is enabled by 'importing' the non-existent symbol
88       'verbose'. You would typically enable it by saying
89
90           perl -MCarp=verbose script.pl
91
92       or by including the string "-MCarp=verbose" in the PERL5OPT environment
93       variable.
94
95       Alternately, you can set the global variable $Carp::Verbose to true.
96       See the "GLOBAL VARIABLES" section below.
97
98   Stack Trace formatting
99       At each stack level, the subroutine's name is displayed along with its
100       parameters.  For simple scalars, this is sufficient.  For complex data
101       types, such as objects and other references, this can simply display
102       'HASH(0x1ab36d8)'.
103
104       Carp gives two ways to control this.
105
106       1.  For objects, a method, "CARP_TRACE", will be called, if it exists.
107           If this method doesn't exist, or it recurses into "Carp", or it
108           otherwise throws an exception, this is skipped, and Carp moves on
109           to the next option, otherwise checking stops and the string
110           returned is used.  It is recommended that the object's type is part
111           of the string to make debugging easier.
112
113       2.  For any type of reference, $Carp::RefArgFormatter is checked (see
114           below).  This variable is expected to be a code reference, and the
115           current parameter is passed in.  If this function doesn't exist
116           (the variable is undef), or it recurses into "Carp", or it
117           otherwise throws an exception, this is skipped, and Carp moves on
118           to the next option, otherwise checking stops and the string
119           returned is used.
120
121       3.  Otherwise, if neither "CARP_TRACE" nor $Carp::RefArgFormatter is
122           available, stringify the value ignoring any overloading.
123

GLOBAL VARIABLES

125   $Carp::MaxEvalLen
126       This variable determines how many characters of a string-eval are to be
127       shown in the output. Use a value of 0 to show all text.
128
129       Defaults to 0.
130
131   $Carp::MaxArgLen
132       This variable determines how many characters of each argument to a
133       function to print. Use a value of 0 to show the full length of the
134       argument.
135
136       Defaults to 64.
137
138   $Carp::MaxArgNums
139       This variable determines how many arguments to each function to show.
140       Use a false value to show all arguments to a function call.  To
141       suppress all arguments, use "-1" or '0 but true'.
142
143       Defaults to 8.
144
145   $Carp::Verbose
146       This variable makes "carp()" and "croak()" generate stack backtraces
147       just like "cluck()" and "confess()".  This is how "use Carp 'verbose'"
148       is implemented internally.
149
150       Defaults to 0.
151
152   $Carp::RefArgFormatter
153       This variable sets a general argument formatter to display references.
154       Plain scalars and objects that implement "CARP_TRACE" will not go
155       through this formatter.  Calling "Carp" from within this function is
156       not supported.
157
158           local $Carp::RefArgFormatter = sub {
159               require Data::Dumper;
160               Data::Dumper->Dump($_[0]); # not necessarily safe
161           };
162
163   @CARP_NOT
164       This variable, in your package, says which packages are not to be
165       considered as the location of an error. The "carp()" and "cluck()"
166       functions will skip over callers when reporting where an error
167       occurred.
168
169       NB: This variable must be in the package's symbol table, thus:
170
171           # These work
172           our @CARP_NOT; # file scope
173           use vars qw(@CARP_NOT); # package scope
174           @My::Package::CARP_NOT = ... ; # explicit package variable
175
176           # These don't work
177           sub xyz { ... @CARP_NOT = ... } # w/o declarations above
178           my @CARP_NOT; # even at top-level
179
180       Example of use:
181
182           package My::Carping::Package;
183           use Carp;
184           our @CARP_NOT;
185           sub bar     { .... or _error('Wrong input') }
186           sub _error  {
187               # temporary control of where'ness, __PACKAGE__ is implicit
188               local @CARP_NOT = qw(My::Friendly::Caller);
189               carp(@_)
190           }
191
192       This would make "Carp" report the error as coming from a caller not in
193       "My::Carping::Package", nor from "My::Friendly::Caller".
194
195       Also read the "DESCRIPTION" section above, about how "Carp" decides
196       where the error is reported from.
197
198       Use @CARP_NOT, instead of $Carp::CarpLevel.
199
200       Overrides "Carp"'s use of @ISA.
201
202   %Carp::Internal
203       This says what packages are internal to Perl.  "Carp" will never report
204       an error as being from a line in a package that is internal to Perl.
205       For example:
206
207           $Carp::Internal{ (__PACKAGE__) }++;
208           # time passes...
209           sub foo { ... or confess("whatever") };
210
211       would give a full stack backtrace starting from the first caller
212       outside of __PACKAGE__.  (Unless that package was also internal to
213       Perl.)
214
215   %Carp::CarpInternal
216       This says which packages are internal to Perl's warning system.  For
217       generating a full stack backtrace this is the same as being internal to
218       Perl, the stack backtrace will not start inside packages that are
219       listed in %Carp::CarpInternal.  But it is slightly different for the
220       summary message generated by "carp" or "croak".  There errors will not
221       be reported on any lines that are calling packages in
222       %Carp::CarpInternal.
223
224       For example "Carp" itself is listed in %Carp::CarpInternal.  Therefore
225       the full stack backtrace from "confess" will not start inside of
226       "Carp", and the short message from calling "croak" is not placed on the
227       line where "croak" was called.
228
229   $Carp::CarpLevel
230       This variable determines how many additional call frames are to be
231       skipped that would not otherwise be when reporting where an error
232       occurred on a call to one of "Carp"'s functions.  It is fairly easy to
233       count these call frames on calls that generate a full stack backtrace.
234       However it is much harder to do this accounting for calls that generate
235       a short message.  Usually people skip too many call frames.  If they
236       are lucky they skip enough that "Carp" goes all of the way through the
237       call stack, realizes that something is wrong, and then generates a full
238       stack backtrace.  If they are unlucky then the error is reported from
239       somewhere misleading very high in the call stack.
240
241       Therefore it is best to avoid $Carp::CarpLevel.  Instead use @CARP_NOT,
242       %Carp::Internal and %Carp::CarpInternal.
243
244       Defaults to 0.
245

BUGS

247       The Carp routines don't handle exception objects currently.  If called
248       with a first argument that is a reference, they simply call die() or
249       warn(), as appropriate.
250

SEE ALSO

252       Carp::Always, Carp::Clan
253

CONTRIBUTING

255       Carp is maintained by the perl 5 porters as part of the core perl 5
256       version control repository. Please see the perlhack perldoc for how to
257       submit patches and contribute to it.
258

AUTHOR

260       The Carp module first appeared in Larry Wall's perl 5.000 distribution.
261       Since then it has been modified by several of the perl 5 porters.
262       Andrew Main (Zefram) <zefram@fysh.org> divested Carp into an
263       independent distribution.
264
266       Copyright (C) 1994-2013 Larry Wall
267
268       Copyright (C) 2011, 2012, 2013 Andrew Main (Zefram) <zefram@fysh.org>
269

LICENSE

271       This module is free software; you can redistribute it and/or modify it
272       under the same terms as Perl itself.
273
274
275
276perl v5.36.0                      2022-07-22                           Carp(3)
Impressum