Some escapes were mentioned twice, although they're not qr//-specific
[p5sagit/p5-mst-13.2.git] / lib / Carp.pm
CommitLineData
a0d0e21e 1package Carp;
8c3d9721 2
792941b2 3our $VERSION = '1.07';
29ddba3b 4# this file is an utra-lightweight stub. The first time a function is
5# called, Carp::Heavy is loaded, and the real short/longmessmess_jmp
6# subs are installed
b75c8c73 7
8c3d9721 8our $MaxEvalLen = 0;
9our $Verbose = 0;
10our $CarpLevel = 0;
11our $MaxArgLen = 64; # How much of each argument to print. 0 = all.
12our $MaxArgNums = 8; # How many arguments to print. 0 = all.
748a9306 13
a0d0e21e 14require Exporter;
8c3d9721 15our @ISA = ('Exporter');
16our @EXPORT = qw(confess croak carp);
17our @EXPORT_OK = qw(cluck verbose longmess shortmess);
18our @EXPORT_FAIL = qw(verbose); # hook to enable verbose mode
af80c6a7 19
af80c6a7 20# if the caller specifies verbose usage ("perl -MCarp=verbose script.pl")
21# then the following method will be called by the Exporter which knows
22# to do this thanks to @EXPORT_FAIL, above. $_[1] will contain the word
23# 'verbose'.
24
29ddba3b 25sub export_fail { shift; $Verbose = shift if $_[0] eq 'verbose'; @_ }
7b8d334a 26
29ddba3b 27# fixed hooks for stashes to point to
28sub longmess { goto &longmess_jmp }
29sub shortmess { goto &shortmess_jmp }
30# these two are replaced when Carp::Heavy is loaded
090656d2 31sub longmess_jmp {
32 local($@, $!);
33 eval { require Carp::Heavy };
34 return $@ if $@;
792941b2 35 goto &longmess_real;
090656d2 36}
37sub shortmess_jmp {
38 local($@, $!);
39 eval { require Carp::Heavy };
40 return $@ if $@;
792941b2 41 goto &shortmess_real;
090656d2 42}
7b8d334a 43
44sub croak { die shortmess @_ }
45sub confess { die longmess @_ }
46sub carp { warn shortmess @_ }
47sub cluck { warn longmess @_ }
a0d0e21e 48
748a9306 491;
0cda2667 50__END__
51
52=head1 NAME
53
54carp - warn of errors (from perspective of caller)
55
56cluck - warn of errors with stack backtrace
57 (not exported by default)
58
59croak - die of errors (from perspective of caller)
60
61confess - die of errors with stack backtrace
62
0cda2667 63=head1 SYNOPSIS
64
65 use Carp;
66 croak "We're outta here!";
67
68 use Carp qw(cluck);
69 cluck "This is how we got here!";
70
0cda2667 71=head1 DESCRIPTION
72
73The Carp routines are useful in your own modules because
74they act like die() or warn(), but with a message which is more
75likely to be useful to a user of your module. In the case of
76cluck, confess, and longmess that context is a summary of every
d735c2ef 77call in the call-stack. For a shorter message you can use C<carp>
78or C<croak> which report the error as being from where your module
79was called. There is no guarantee that that is where the error
80was, but it is a good educated guess.
0cda2667 81
82You can also alter the way the output and logic of C<Carp> works, by
83changing some global variables in the C<Carp> namespace. See the
84section on C<GLOBAL VARIABLES> below.
85
d735c2ef 86Here is a more complete description of how c<carp> and c<croak> work.
87What they do is search the call-stack for a function call stack where
88they have not been told that there shouldn't be an error. If every
89call is marked safe, they give up and give a full stack backtrace
90instead. In other words they presume that the first likely looking
91potential suspect is guilty. Their rules for telling whether
0cda2667 92a call shouldn't generate errors work as follows:
93
94=over 4
95
96=item 1.
97
98Any call from a package to itself is safe.
99
100=item 2.
101
102Packages claim that there won't be errors on calls to or from
d735c2ef 103packages explicitly marked as safe by inclusion in C<@CARP_NOT>, or
104(if that array is empty) C<@ISA>. The ability to override what
0cda2667 105@ISA says is new in 5.8.
106
107=item 3.
108
109The trust in item 2 is transitive. If A trusts B, and B
d735c2ef 110trusts C, then A trusts C. So if you do not override C<@ISA>
111with C<@CARP_NOT>, then this trust relationship is identical to,
0cda2667 112"inherits from".
113
114=item 4.
115
116Any call from an internal Perl module is safe. (Nothing keeps
117user modules from marking themselves as internal to Perl, but
118this practice is discouraged.)
119
120=item 5.
121
d735c2ef 122Any call to Perl's warning system (eg Carp itself) is safe.
123(This rule is what keeps it from reporting the error at the
124point where you call C<carp> or C<croak>.)
125
126=item 6.
127
128C<$Carp::CarpLevel> can be set to skip a fixed number of additional
129call levels. Using this is not recommended because it is very
130difficult to get it to behave correctly.
0cda2667 131
132=back
133
134=head2 Forcing a Stack Trace
135
136As a debugging aid, you can force Carp to treat a croak as a confess
137and a carp as a cluck across I<all> modules. In other words, force a
138detailed stack trace to be given. This can be very helpful when trying
139to understand why, or from where, a warning or error is being generated.
140
141This feature is enabled by 'importing' the non-existent symbol
142'verbose'. You would typically enable it by saying
143
144 perl -MCarp=verbose script.pl
145
146or by including the string C<MCarp=verbose> in the PERL5OPT
147environment variable.
148
149Alternately, you can set the global variable C<$Carp::Verbose> to true.
150See the C<GLOBAL VARIABLES> section below.
151
152=head1 GLOBAL VARIABLES
153
0cda2667 154=head2 $Carp::MaxEvalLen
155
156This variable determines how many characters of a string-eval are to
157be shown in the output. Use a value of C<0> to show all text.
158
159Defaults to C<0>.
160
161=head2 $Carp::MaxArgLen
162
163This variable determines how many characters of each argument to a
164function to print. Use a value of C<0> to show the full length of the
165argument.
166
167Defaults to C<64>.
168
169=head2 $Carp::MaxArgNums
170
171This variable determines how many arguments to each function to show.
172Use a value of C<0> to show all arguments to a function call.
173
174Defaults to C<8>.
175
176=head2 $Carp::Verbose
177
d735c2ef 178This variable makes C<carp> and C<cluck> generate stack backtraces
179just like C<cluck> and C<confess>. This is how C<use Carp 'verbose'>
180is implemented internally.
181
182Defaults to C<0>.
183
184=head2 %Carp::Internal
185
186This says what packages are internal to Perl. C<Carp> will never
187report an error as being from a line in a package that is internal to
188Perl. For example:
189
190 $Carp::Internal{ __PACKAGE__ }++;
191 # time passes...
192 sub foo { ... or confess("whatever") };
193
194would give a full stack backtrace starting from the first caller
195outside of __PACKAGE__. (Unless that package was also internal to
196Perl.)
197
198=head2 %Carp::CarpInternal
199
200This says which packages are internal to Perl's warning system. For
201generating a full stack backtrace this is the same as being internal
202to Perl, the stack backtrace will not start inside packages that are
203listed in C<%Carp::CarpInternal>. But it is slightly different for
204the summary message generated by C<carp> or C<croak>. There errors
205will not be reported on any lines that are calling packages in
206C<%Carp::CarpInternal>.
207
208For example C<Carp> itself is listed in C<%Carp::CarpInternal>.
209Therefore the full stack backtrace from C<confess> will not start
210inside of C<Carp>, and the short message from calling C<croak> is
211not placed on the line where C<croak> was called.
212
213=head2 $Carp::CarpLevel
0cda2667 214
d735c2ef 215This variable determines how many additional call frames are to be
216skipped that would not otherwise be when reporting where an error
217occurred on a call to one of C<Carp>'s functions. It is fairly easy
218to count these call frames on calls that generate a full stack
219backtrace. However it is much harder to do this accounting for calls
220that generate a short message. Usually people skip too many call
221frames. If they are lucky they skip enough that C<Carp> goes all of
222the way through the call stack, realizes that something is wrong, and
223then generates a full stack backtrace. If they are unlucky then the
224error is reported from somewhere misleading very high in the call
225stack.
226
227Therefore it is best to avoid C<$Carp::CarpLevel>. Instead use
228C<@CARP_NOT>, C<%Carp::Internal> and %Carp::CarpInternal>.
0cda2667 229
230Defaults to C<0>.
231
0cda2667 232=head1 BUGS
233
234The Carp routines don't handle exception objects currently.
235If called with a first argument that is a reference, they simply
236call die() or warn(), as appropriate.
237