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   @CARP_NOT
163       This variable, in your package, says which packages are not to be
164       considered as the location of an error. The "carp()" and "cluck()"
165       functions will skip over callers when reporting where an error
166       occurred.
167
168       NB: This variable must be in the package's symbol table, thus:
169
170           # These work
171           our @CARP_NOT; # file scope
172           use vars qw(@CARP_NOT); # package scope
173           @My::Package::CARP_NOT = ... ; # explicit package variable
174
175           # These don't work
176           sub xyz { ... @CARP_NOT = ... } # w/o declarations above
177           my @CARP_NOT; # even at top-level
178
179       Example of use:
180
181           package My::Carping::Package;
182           use Carp;
183           our @CARP_NOT;
184           sub bar     { .... or _error('Wrong input') }
185           sub _error  {
186               # temporary control of where'ness, __PACKAGE__ is implicit
187               local @CARP_NOT = qw(My::Friendly::Caller);
188               carp(@_)
189           }
190
191       This would make "Carp" report the error as coming from a caller not in
192       "My::Carping::Package", nor from "My::Friendly::Caller".
193
194       Also read the "DESCRIPTION" section above, about how "Carp" decides
195       where the error is reported from.
196
197       Use @CARP_NOT, instead of $Carp::CarpLevel.
198
199       Overrides "Carp"'s use of @ISA.
200
201   %Carp::Internal
202       This says what packages are internal to Perl.  "Carp" will never report
203       an error as being from a line in a package that is internal to Perl.
204       For example:
205
206           $Carp::Internal{ (__PACKAGE__) }++;
207           # time passes...
208           sub foo { ... or confess("whatever") };
209
210       would give a full stack backtrace starting from the first caller
211       outside of __PACKAGE__.  (Unless that package was also internal to
212       Perl.)
213
214   %Carp::CarpInternal
215       This says which packages are internal to Perl's warning system.  For
216       generating a full stack backtrace this is the same as being internal to
217       Perl, the stack backtrace will not start inside packages that are
218       listed in %Carp::CarpInternal.  But it is slightly different for the
219       summary message generated by "carp" or "croak".  There errors will not
220       be reported on any lines that are calling packages in
221       %Carp::CarpInternal.
222
223       For example "Carp" itself is listed in %Carp::CarpInternal.  Therefore
224       the full stack backtrace from "confess" will not start inside of
225       "Carp", and the short message from calling "croak" is not placed on the
226       line where "croak" was called.
227
228   $Carp::CarpLevel
229       This variable determines how many additional call frames are to be
230       skipped that would not otherwise be when reporting where an error
231       occurred on a call to one of "Carp"'s functions.  It is fairly easy to
232       count these call frames on calls that generate a full stack backtrace.
233       However it is much harder to do this accounting for calls that generate
234       a short message.  Usually people skip too many call frames.  If they
235       are lucky they skip enough that "Carp" goes all of the way through the
236       call stack, realizes that something is wrong, and then generates a full
237       stack backtrace.  If they are unlucky then the error is reported from
238       somewhere misleading very high in the call stack.
239
240       Therefore it is best to avoid $Carp::CarpLevel.  Instead use @CARP_NOT,
241       %Carp::Internal and %Carp::CarpInternal.
242
243       Defaults to 0.
244

BUGS

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

SEE ALSO

251       Carp::Always, Carp::Clan
252

CONTRIBUTING

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

AUTHOR

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

LICENSE

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