.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Carp 3perl" .TH Carp 3perl 2023-11-30 "perl v5.38.2" "Perl Programmers Reference Guide" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Carp \- alternative warn and die for modules .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use Carp; \& \& # warn user (from perspective of caller) \& carp "string trimmed to 80 chars"; \& \& # die of errors (from perspective of caller) \& croak "We\*(Aqre outta here!"; \& \& # die of errors with stack backtrace \& confess "not implemented"; \& \& # cluck, longmess and shortmess not exported by default \& use Carp qw(cluck longmess shortmess); \& cluck "This is how we got here!"; # warn with stack backtrace \& my $long_message = longmess( "message from cluck() or confess()" ); \& my $short_message = shortmess( "message from carp() or croak()" ); .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" The Carp routines are useful in your own modules because they act like \f(CWdie()\fR or \f(CWwarn()\fR, but with a message which is more likely to be useful to a user of your module. In the case of \&\f(CWcluck()\fR and \f(CWconfess()\fR, that context is a summary of every call in the call-stack; \f(CWlongmess()\fR returns the contents of the error message. .PP For a shorter message you can use \f(CWcarp()\fR or \f(CWcroak()\fR which report the error as being from where your module was called. \f(CWshortmess()\fR returns the contents of this error message. There is no guarantee that that is where the error was, but it is a good educated guess. .PP \&\f(CW\*(C`Carp\*(C'\fR takes care not to clobber the status variables \f(CW$!\fR and \f(CW$^E\fR in the course of assembling its error messages. This means that a \&\f(CW$SIG{_\|_DIE_\|_}\fR or \f(CW$SIG{_\|_WARN_\|_}\fR handler can capture the error information held in those variables, if it is required to augment the error message, and if the code calling \f(CW\*(C`Carp\*(C'\fR left useful values there. Of course, \f(CW\*(C`Carp\*(C'\fR can't guarantee the latter. .PP You can also alter the way the output and logic of \f(CW\*(C`Carp\*(C'\fR works, by changing some global variables in the \f(CW\*(C`Carp\*(C'\fR namespace. See the section on "GLOBAL VARIABLES" below. .PP Here is a more complete description of how \f(CW\*(C`carp\*(C'\fR and \f(CW\*(C`croak\*(C'\fR work. What they do is search the call-stack for a function call stack where they have not been told that there shouldn't be an error. If every call is marked safe, they give up and give a full stack backtrace instead. In other words they presume that the first likely looking potential suspect is guilty. Their rules for telling whether a call shouldn't generate errors work as follows: .IP 1. 4 Any call from a package to itself is safe. .IP 2. 4 Packages claim that there won't be errors on calls to or from packages explicitly marked as safe by inclusion in \f(CW@CARP_NOT\fR, or (if that array is empty) \f(CW@ISA\fR. The ability to override what \&\f(CW@ISA\fR says is new in 5.8. .IP 3. 4 The trust in item 2 is transitive. If A trusts B, and B trusts C, then A trusts C. So if you do not override \f(CW@ISA\fR with \f(CW@CARP_NOT\fR, then this trust relationship is identical to, "inherits from". .IP 4. 4 Any call from an internal Perl module is safe. (Nothing keeps user modules from marking themselves as internal to Perl, but this practice is discouraged.) .IP 5. 4 Any call to Perl's warning system (eg Carp itself) is safe. (This rule is what keeps it from reporting the error at the point where you call \f(CW\*(C`carp\*(C'\fR or \f(CW\*(C`croak\*(C'\fR.) .IP 6. 4 \&\f(CW$Carp::CarpLevel\fR can be set to skip a fixed number of additional call levels. Using this is not recommended because it is very difficult to get it to behave correctly. .SS "Forcing a Stack Trace" .IX Subsection "Forcing a Stack Trace" As a debugging aid, you can force Carp to treat a croak as a confess and a carp as a cluck across \fIall\fR modules. In other words, force a detailed stack trace to be given. This can be very helpful when trying to understand why, or from where, a warning or error is being generated. .PP This feature is enabled by 'importing' the non-existent symbol \&'verbose'. You would typically enable it by saying .PP .Vb 1 \& perl \-MCarp=verbose script.pl .Ve .PP or by including the string \f(CW\*(C`\-MCarp=verbose\*(C'\fR in the PERL5OPT environment variable. .PP Alternately, you can set the global variable \f(CW$Carp::Verbose\fR to true. See the "GLOBAL VARIABLES" section below. .SS "Stack Trace formatting" .IX Subsection "Stack Trace formatting" At each stack level, the subroutine's name is displayed along with its parameters. For simple scalars, this is sufficient. For complex data types, such as objects and other references, this can simply display \f(CW\*(AqHASH(0x1ab36d8)\*(Aq\fR. .PP Carp gives two ways to control this. .IP 1. 4 For objects, a method, \f(CW\*(C`CARP_TRACE\*(C'\fR, will be called, if it exists. If this method doesn't exist, or it recurses into \f(CW\*(C`Carp\*(C'\fR, or it otherwise throws an exception, this is skipped, and Carp moves on to the next option, otherwise checking stops and the string returned is used. It is recommended that the object's type is part of the string to make debugging easier. .IP 2. 4 For any type of reference, \f(CW$Carp::RefArgFormatter\fR is checked (see below). This variable is expected to be a code reference, and the current parameter is passed in. If this function doesn't exist (the variable is undef), or it recurses into \f(CW\*(C`Carp\*(C'\fR, or it otherwise throws an exception, this is skipped, and Carp moves on to the next option, otherwise checking stops and the string returned is used. .IP 3. 4 Otherwise, if neither \f(CW\*(C`CARP_TRACE\*(C'\fR nor \f(CW$Carp::RefArgFormatter\fR is available, stringify the value ignoring any overloading. .SH "GLOBAL VARIABLES" .IX Header "GLOBAL VARIABLES" .ie n .SS $Carp::MaxEvalLen .el .SS \f(CW$Carp::MaxEvalLen\fP .IX Subsection "$Carp::MaxEvalLen" This variable determines how many characters of a string-eval are to be shown in the output. Use a value of \f(CW0\fR to show all text. .PP Defaults to \f(CW0\fR. .ie n .SS $Carp::MaxArgLen .el .SS \f(CW$Carp::MaxArgLen\fP .IX Subsection "$Carp::MaxArgLen" This variable determines how many characters of each argument to a function to print. Use a value of \f(CW0\fR to show the full length of the argument. .PP Defaults to \f(CW64\fR. .ie n .SS $Carp::MaxArgNums .el .SS \f(CW$Carp::MaxArgNums\fP .IX Subsection "$Carp::MaxArgNums" This variable determines how many arguments to each function to show. Use a false value to show all arguments to a function call. To suppress all arguments, use \f(CW\-1\fR or \f(CW\*(Aq0 but true\*(Aq\fR. .PP Defaults to \f(CW8\fR. .ie n .SS $Carp::Verbose .el .SS \f(CW$Carp::Verbose\fP .IX Subsection "$Carp::Verbose" This variable makes \f(CWcarp()\fR and \f(CWcroak()\fR generate stack backtraces just like \f(CWcluck()\fR and \f(CWconfess()\fR. This is how \f(CW\*(C`use Carp \*(Aqverbose\*(Aq\*(C'\fR is implemented internally. .PP Defaults to \f(CW0\fR. .ie n .SS $Carp::RefArgFormatter .el .SS \f(CW$Carp::RefArgFormatter\fP .IX Subsection "$Carp::RefArgFormatter" This variable sets a general argument formatter to display references. Plain scalars and objects that implement \f(CW\*(C`CARP_TRACE\*(C'\fR will not go through this formatter. Calling \f(CW\*(C`Carp\*(C'\fR from within this function is not supported. .PP .Vb 4 \& local $Carp::RefArgFormatter = sub { \& require Data::Dumper; \& Data::Dumper\->Dump($_[0]); # not necessarily safe \& }; .Ve .ie n .SS @CARP_NOT .el .SS \f(CW@CARP_NOT\fP .IX Subsection "@CARP_NOT" This variable, \fIin your package\fR, says which packages are \fInot\fR to be considered as the location of an error. The \f(CWcarp()\fR and \f(CWcluck()\fR functions will skip over callers when reporting where an error occurred. .PP NB: This variable must be in the package's symbol table, thus: .PP .Vb 4 \& # These work \& our @CARP_NOT; # file scope \& use vars qw(@CARP_NOT); # package scope \& @My::Package::CARP_NOT = ... ; # explicit package variable \& \& # These don\*(Aqt work \& sub xyz { ... @CARP_NOT = ... } # w/o declarations above \& my @CARP_NOT; # even at top\-level .Ve .PP Example of use: .PP .Vb 9 \& package My::Carping::Package; \& use Carp; \& our @CARP_NOT; \& sub bar { .... or _error(\*(AqWrong input\*(Aq) } \& sub _error { \& # temporary control of where\*(Aqness, _\|_PACKAGE_\|_ is implicit \& local @CARP_NOT = qw(My::Friendly::Caller); \& carp(@_) \& } .Ve .PP This would make \f(CW\*(C`Carp\*(C'\fR report the error as coming from a caller not in \f(CW\*(C`My::Carping::Package\*(C'\fR, nor from \f(CW\*(C`My::Friendly::Caller\*(C'\fR. .PP Also read the "DESCRIPTION" section above, about how \f(CW\*(C`Carp\*(C'\fR decides where the error is reported from. .PP Use \f(CW@CARP_NOT\fR, instead of \f(CW$Carp::CarpLevel\fR. .PP Overrides \f(CW\*(C`Carp\*(C'\fR's use of \f(CW@ISA\fR. .ie n .SS %Carp::Internal .el .SS \f(CW%Carp::Internal\fP .IX Subsection "%Carp::Internal" This says what packages are internal to Perl. \f(CW\*(C`Carp\*(C'\fR will never report an error as being from a line in a package that is internal to Perl. For example: .PP .Vb 3 \& $Carp::Internal{ (_\|_PACKAGE_\|_) }++; \& # time passes... \& sub foo { ... or confess("whatever") }; .Ve .PP would give a full stack backtrace starting from the first caller outside of _\|_PACKAGE_\|_. (Unless that package was also internal to Perl.) .ie n .SS %Carp::CarpInternal .el .SS \f(CW%Carp::CarpInternal\fP .IX Subsection "%Carp::CarpInternal" This says which packages are internal to Perl's warning system. For generating a full stack backtrace this is the same as being internal to Perl, the stack backtrace will not start inside packages that are listed in \f(CW%Carp::CarpInternal\fR. But it is slightly different for the summary message generated by \f(CW\*(C`carp\*(C'\fR or \f(CW\*(C`croak\*(C'\fR. There errors will not be reported on any lines that are calling packages in \&\f(CW%Carp::CarpInternal\fR. .PP For example \f(CW\*(C`Carp\*(C'\fR itself is listed in \f(CW%Carp::CarpInternal\fR. Therefore the full stack backtrace from \f(CW\*(C`confess\*(C'\fR will not start inside of \f(CW\*(C`Carp\*(C'\fR, and the short message from calling \f(CW\*(C`croak\*(C'\fR is not placed on the line where \f(CW\*(C`croak\*(C'\fR was called. .ie n .SS $Carp::CarpLevel .el .SS \f(CW$Carp::CarpLevel\fP .IX Subsection "$Carp::CarpLevel" This variable determines how many additional call frames are to be skipped that would not otherwise be when reporting where an error occurred on a call to one of \f(CW\*(C`Carp\*(C'\fR's functions. It is fairly easy to count these call frames on calls that generate a full stack backtrace. However it is much harder to do this accounting for calls that generate a short message. Usually people skip too many call frames. If they are lucky they skip enough that \f(CW\*(C`Carp\*(C'\fR goes all of the way through the call stack, realizes that something is wrong, and then generates a full stack backtrace. If they are unlucky then the error is reported from somewhere misleading very high in the call stack. .PP Therefore it is best to avoid \f(CW$Carp::CarpLevel\fR. Instead use \&\f(CW@CARP_NOT\fR, \f(CW%Carp::Internal\fR and \f(CW%Carp::CarpInternal\fR. .PP Defaults to \f(CW0\fR. .SH BUGS .IX Header "BUGS" The Carp routines don't handle exception objects currently. If called with a first argument that is a reference, they simply call \fBdie()\fR or \fBwarn()\fR, as appropriate. .SH "SEE ALSO" .IX Header "SEE ALSO" Carp::Always, Carp::Clan .SH CONTRIBUTING .IX Header "CONTRIBUTING" Carp is maintained by the perl 5 porters as part of the core perl 5 version control repository. Please see the perlhack perldoc for how to submit patches and contribute to it. .SH AUTHOR .IX Header "AUTHOR" The Carp module first appeared in Larry Wall's perl 5.000 distribution. Since then it has been modified by several of the perl 5 porters. Andrew Main (Zefram) divested Carp into an independent distribution. .SH COPYRIGHT .IX Header "COPYRIGHT" Copyright (C) 1994\-2013 Larry Wall .PP Copyright (C) 2011, 2012, 2013 Andrew Main (Zefram) .SH LICENSE .IX Header "LICENSE" This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.