Debian Codegraph @

upstream/debian-unstable/man3/Carp.3perl

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\" ========================================================================
56 .\"
57 .IX Title "Carp 3perl"
58 .TH Carp 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide"
59 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
60 .\" way too many mistakes in technical documents.
61 .if n .ad l
62 .nh
63 .SH NAME
64 Carp \- alternative warn and die for modules
65 .SH SYNOPSIS
66 .IX Header "SYNOPSIS"
67 .Vb 1
68 \& use Carp;
69 \&
70 \& # warn user (from perspective of caller)
71 \& carp "string trimmed to 80 chars";
72 \&
73 \& # die of errors (from perspective of caller)
74 \& croak "We\*(Aqre outta here!";
75 \&
76 \& # die of errors with stack backtrace
77 \& confess "not implemented";
78 \&
79 \& # cluck, longmess and shortmess not exported by default
80 \& use Carp qw(cluck longmess shortmess);
81 \& cluck "This is how we got here!"; # warn with stack backtrace
82 \& my $long_message = longmess( "message from cluck() or confess()" );
83 \& my $short_message = shortmess( "message from carp() or croak()" );
84 .Ve
85 .SH DESCRIPTION
86 .IX Header "DESCRIPTION"
87 The Carp routines are useful in your own modules because
88 they act like \f(CWdie()\fR or \f(CWwarn()\fR, but with a message which is more
89 likely to be useful to a user of your module. In the case of
90 \&\f(CWcluck()\fR and \f(CWconfess()\fR, that context is a summary of every
91 call in the call-stack; \f(CWlongmess()\fR returns the contents of the error
92 message.
93 .PP
94 For a shorter message you can use \f(CWcarp()\fR or \f(CWcroak()\fR which report the
95 error as being from where your module was called. \f(CWshortmess()\fR returns the
96 contents of this error message. There is no guarantee that that is where the
97 error was, but it is a good educated guess.
98 .PP
99 \&\f(CW\*(C`Carp\*(C'\fR takes care not to clobber the status variables \f(CW$!\fR and \f(CW$^E\fR
100 in the course of assembling its error messages. This means that a
101 \&\f(CW$SIG{_\|_DIE_\|_}\fR or \f(CW$SIG{_\|_WARN_\|_}\fR handler can capture the error
102 information held in those variables, if it is required to augment the
103 error message, and if the code calling \f(CW\*(C`Carp\*(C'\fR left useful values there.
104 Of course, \f(CW\*(C`Carp\*(C'\fR can't guarantee the latter.
105 .PP
106 You can also alter the way the output and logic of \f(CW\*(C`Carp\*(C'\fR works, by
107 changing some global variables in the \f(CW\*(C`Carp\*(C'\fR namespace. See the
108 section on "GLOBAL VARIABLES" below.
109 .PP
110 Here is a more complete description of how \f(CW\*(C`carp\*(C'\fR and \f(CW\*(C`croak\*(C'\fR work.
111 What they do is search the call-stack for a function call stack where
112 they have not been told that there shouldn't be an error. If every
113 call is marked safe, they give up and give a full stack backtrace
114 instead. In other words they presume that the first likely looking
115 potential suspect is guilty. Their rules for telling whether
116 a call shouldn't generate errors work as follows:
117 .IP 1. 4
118 Any call from a package to itself is safe.
119 .IP 2. 4
120 Packages claim that there won't be errors on calls to or from
121 packages explicitly marked as safe by inclusion in \f(CW@CARP_NOT\fR, or
122 (if that array is empty) \f(CW@ISA\fR. The ability to override what
123 \&\f(CW@ISA\fR says is new in 5.8.
124 .IP 3. 4
125 The trust in item 2 is transitive. If A trusts B, and B
126 trusts C, then A trusts C. So if you do not override \f(CW@ISA\fR
127 with \f(CW@CARP_NOT\fR, then this trust relationship is identical to,
128 "inherits from".
129 .IP 4. 4
130 Any call from an internal Perl module is safe. (Nothing keeps
131 user modules from marking themselves as internal to Perl, but
132 this practice is discouraged.)
133 .IP 5. 4
134 Any call to Perl's warning system (eg Carp itself) is safe.
135 (This rule is what keeps it from reporting the error at the
136 point where you call \f(CW\*(C`carp\*(C'\fR or \f(CW\*(C`croak\*(C'\fR.)
137 .IP 6. 4
138 \&\f(CW$Carp::CarpLevel\fR can be set to skip a fixed number of additional
139 call levels. Using this is not recommended because it is very
140 difficult to get it to behave correctly.
141 .SS "Forcing a Stack Trace"
142 .IX Subsection "Forcing a Stack Trace"
143 As a debugging aid, you can force Carp to treat a croak as a confess
144 and a carp as a cluck across \fIall\fR modules. In other words, force a
145 detailed stack trace to be given. This can be very helpful when trying
146 to understand why, or from where, a warning or error is being generated.
147 .PP
148 This feature is enabled by 'importing' the non-existent symbol
149 \&'verbose'. You would typically enable it by saying
150 .PP
151 .Vb 1
152 \& perl \-MCarp=verbose script.pl
153 .Ve
154 .PP
155 or by including the string \f(CW\*(C`\-MCarp=verbose\*(C'\fR in the PERL5OPT
156 environment variable.
157 .PP
158 Alternately, you can set the global variable \f(CW$Carp::Verbose\fR to true.
159 See the "GLOBAL VARIABLES" section below.
160 .SS "Stack Trace formatting"
161 .IX Subsection "Stack Trace formatting"
162 At each stack level, the subroutine's name is displayed along with
163 its parameters. For simple scalars, this is sufficient. For complex
164 data types, such as objects and other references, this can simply
165 display \f(CW\*(AqHASH(0x1ab36d8)\*(Aq\fR.
166 .PP
167 Carp gives two ways to control this.
168 .IP 1. 4
169 For objects, a method, \f(CW\*(C`CARP_TRACE\*(C'\fR, will be called, if it exists. If
170 this method doesn't exist, or it recurses into \f(CW\*(C`Carp\*(C'\fR, or it otherwise
171 throws an exception, this is skipped, and Carp moves on to the next option,
172 otherwise checking stops and the string returned is used. It is recommended
173 that the object's type is part of the string to make debugging easier.
174 .IP 2. 4
175 For any type of reference, \f(CW$Carp::RefArgFormatter\fR is checked (see below).
176 This variable is expected to be a code reference, and the current parameter
177 is passed in. If this function doesn't exist (the variable is undef), or
178 it recurses into \f(CW\*(C`Carp\*(C'\fR, or it otherwise throws an exception, this is
179 skipped, and Carp moves on to the next option, otherwise checking stops
180 and the string returned is used.
181 .IP 3. 4
182 Otherwise, if neither \f(CW\*(C`CARP_TRACE\*(C'\fR nor \f(CW$Carp::RefArgFormatter\fR is
183 available, stringify the value ignoring any overloading.
184 .SH "GLOBAL VARIABLES"
185 .IX Header "GLOBAL VARIABLES"
186 .ie n .SS $Carp::MaxEvalLen
187 .el .SS \f(CW$Carp::MaxEvalLen\fP
188 .IX Subsection "$Carp::MaxEvalLen"
189 This variable determines how many characters of a string-eval are to
190 be shown in the output. Use a value of \f(CW0\fR to show all text.
191 .PP
192 Defaults to \f(CW0\fR.
193 .ie n .SS $Carp::MaxArgLen
194 .el .SS \f(CW$Carp::MaxArgLen\fP
195 .IX Subsection "$Carp::MaxArgLen"
196 This variable determines how many characters of each argument to a
197 function to print. Use a value of \f(CW0\fR to show the full length of the
198 argument.
199 .PP
200 Defaults to \f(CW64\fR.
201 .ie n .SS $Carp::MaxArgNums
202 .el .SS \f(CW$Carp::MaxArgNums\fP
203 .IX Subsection "$Carp::MaxArgNums"
204 This variable determines how many arguments to each function to show.
205 Use a false value to show all arguments to a function call. To suppress all
206 arguments, use \f(CW\-1\fR or \f(CW\*(Aq0 but true\*(Aq\fR.
207 .PP
208 Defaults to \f(CW8\fR.
209 .ie n .SS $Carp::Verbose
210 .el .SS \f(CW$Carp::Verbose\fP
211 .IX Subsection "$Carp::Verbose"
212 This variable makes \f(CWcarp()\fR and \f(CWcroak()\fR generate stack backtraces
213 just like \f(CWcluck()\fR and \f(CWconfess()\fR. This is how \f(CW\*(C`use Carp \*(Aqverbose\*(Aq\*(C'\fR
214 is implemented internally.
215 .PP
216 Defaults to \f(CW0\fR.
217 .ie n .SS $Carp::RefArgFormatter
218 .el .SS \f(CW$Carp::RefArgFormatter\fP
219 .IX Subsection "$Carp::RefArgFormatter"
220 This variable sets a general argument formatter to display references.
221 Plain scalars and objects that implement \f(CW\*(C`CARP_TRACE\*(C'\fR will not go through
222 this formatter. Calling \f(CW\*(C`Carp\*(C'\fR from within this function is not supported.
223 .PP
224 .Vb 4
225 \& local $Carp::RefArgFormatter = sub {
226 \& require Data::Dumper;
227 \& Data::Dumper\->Dump($_[0]); # not necessarily safe
228 \& };
229 .Ve
230 .ie n .SS @CARP_NOT
231 .el .SS \f(CW@CARP_NOT\fP
232 .IX Subsection "@CARP_NOT"
233 This variable, \fIin your package\fR, says which packages are \fInot\fR to be
234 considered as the location of an error. The \f(CWcarp()\fR and \f(CWcluck()\fR
235 functions will skip over callers when reporting where an error occurred.
236 .PP
237 NB: This variable must be in the package's symbol table, thus:
238 .PP
239 .Vb 4
240 \& # These work
241 \& our @CARP_NOT; # file scope
242 \& use vars qw(@CARP_NOT); # package scope
243 \& @My::Package::CARP_NOT = ... ; # explicit package variable
244 \&
245 \& # These don\*(Aqt work
246 \& sub xyz { ... @CARP_NOT = ... } # w/o declarations above
247 \& my @CARP_NOT; # even at top\-level
248 .Ve
249 .PP
250 Example of use:
251 .PP
252 .Vb 9
253 \& package My::Carping::Package;
254 \& use Carp;
255 \& our @CARP_NOT;
256 \& sub bar { .... or _error(\*(AqWrong input\*(Aq) }
257 \& sub _error {
258 \& # temporary control of where\*(Aqness, _\|_PACKAGE_\|_ is implicit
259 \& local @CARP_NOT = qw(My::Friendly::Caller);
260 \& carp(@_)
261 \& }
262 .Ve
263 .PP
264 This would make \f(CW\*(C`Carp\*(C'\fR report the error as coming from a caller not
265 in \f(CW\*(C`My::Carping::Package\*(C'\fR, nor from \f(CW\*(C`My::Friendly::Caller\*(C'\fR.
266 .PP
267 Also read the "DESCRIPTION" section above, about how \f(CW\*(C`Carp\*(C'\fR decides
268 where the error is reported from.
269 .PP
270 Use \f(CW@CARP_NOT\fR, instead of \f(CW$Carp::CarpLevel\fR.
271 .PP
272 Overrides \f(CW\*(C`Carp\*(C'\fR's use of \f(CW@ISA\fR.
273 .ie n .SS %Carp::Internal
274 .el .SS \f(CW%Carp::Internal\fP
275 .IX Subsection "%Carp::Internal"
276 This says what packages are internal to Perl. \f(CW\*(C`Carp\*(C'\fR will never
277 report an error as being from a line in a package that is internal to
278 Perl. For example:
279 .PP
280 .Vb 3
281 \& $Carp::Internal{ (_\|_PACKAGE_\|_) }++;
282 \& # time passes...
283 \& sub foo { ... or confess("whatever") };
284 .Ve
285 .PP
286 would give a full stack backtrace starting from the first caller
287 outside of _\|_PACKAGE_\|_. (Unless that package was also internal to
288 Perl.)
289 .ie n .SS %Carp::CarpInternal
290 .el .SS \f(CW%Carp::CarpInternal\fP
291 .IX Subsection "%Carp::CarpInternal"
292 This says which packages are internal to Perl's warning system. For
293 generating a full stack backtrace this is the same as being internal
294 to Perl, the stack backtrace will not start inside packages that are
295 listed in \f(CW%Carp::CarpInternal\fR. But it is slightly different for
296 the summary message generated by \f(CW\*(C`carp\*(C'\fR or \f(CW\*(C`croak\*(C'\fR. There errors
297 will not be reported on any lines that are calling packages in
298 \&\f(CW%Carp::CarpInternal\fR.
299 .PP
300 For example \f(CW\*(C`Carp\*(C'\fR itself is listed in \f(CW%Carp::CarpInternal\fR.
301 Therefore the full stack backtrace from \f(CW\*(C`confess\*(C'\fR will not start
302 inside of \f(CW\*(C`Carp\*(C'\fR, and the short message from calling \f(CW\*(C`croak\*(C'\fR is
303 not placed on the line where \f(CW\*(C`croak\*(C'\fR was called.
304 .ie n .SS $Carp::CarpLevel
305 .el .SS \f(CW$Carp::CarpLevel\fP
306 .IX Subsection "$Carp::CarpLevel"
307 This variable determines how many additional call frames are to be
308 skipped that would not otherwise be when reporting where an error
309 occurred on a call to one of \f(CW\*(C`Carp\*(C'\fR's functions. It is fairly easy
310 to count these call frames on calls that generate a full stack
311 backtrace. However it is much harder to do this accounting for calls
312 that generate a short message. Usually people skip too many call
313 frames. If they are lucky they skip enough that \f(CW\*(C`Carp\*(C'\fR goes all of
314 the way through the call stack, realizes that something is wrong, and
315 then generates a full stack backtrace. If they are unlucky then the
316 error is reported from somewhere misleading very high in the call
317 stack.
318 .PP
319 Therefore it is best to avoid \f(CW$Carp::CarpLevel\fR. Instead use
320 \&\f(CW@CARP_NOT\fR, \f(CW%Carp::Internal\fR and \f(CW%Carp::CarpInternal\fR.
321 .PP
322 Defaults to \f(CW0\fR.
323 .SH BUGS
324 .IX Header "BUGS"
325 The Carp routines don't handle exception objects currently.
326 If called with a first argument that is a reference, they simply
327 call \fBdie()\fR or \fBwarn()\fR, as appropriate.
328 .SH "SEE ALSO"
329 .IX Header "SEE ALSO"
330 Carp::Always,
331 Carp::Clan
332 .SH CONTRIBUTING
333 .IX Header "CONTRIBUTING"
334 Carp is maintained by the perl 5 porters as part of the core perl 5
335 version control repository. Please see the perlhack perldoc for how to
336 submit patches and contribute to it.
337 .SH AUTHOR
338 .IX Header "AUTHOR"
339 The Carp module first appeared in Larry Wall's perl 5.000 distribution.
340 Since then it has been modified by several of the perl 5 porters.
341 Andrew Main (Zefram) <[email protected]> divested Carp into an independent
342 distribution.
343 .SH COPYRIGHT
344 .IX Header "COPYRIGHT"
345 Copyright (C) 1994\-2013 Larry Wall
346 .PP
347 Copyright (C) 2011, 2012, 2013 Andrew Main (Zefram) <[email protected]>
348 .SH LICENSE
349 .IX Header "LICENSE"
350 This module is free software; you can redistribute it and/or modify it
351 under the same terms as Perl itself.