Re: New harness is failing things
[p5sagit/p5-mst-13.2.git] / lib / Carp.pm
CommitLineData
a0d0e21e 1package Carp;
2
f06db76b 3=head1 NAME
4
4d935a29 5carp - warn of errors (from perspective of caller)
f06db76b 6
4d935a29 7cluck - warn of errors with stack backtrace
8 (not exported by default)
9
10croak - die of errors (from perspective of caller)
f06db76b 11
12confess - die of errors with stack backtrace
13
14=head1 SYNOPSIS
15
16 use Carp;
17 croak "We're outta here!";
18
4d935a29 19 use Carp qw(cluck);
20 cluck "This is how we got here!";
21
f06db76b 22=head1 DESCRIPTION
23
24The Carp routines are useful in your own modules because
25they act like die() or warn(), but report where the error
26was in the code they were called from. Thus if you have a
27routine Foo() that has a carp() in it, then the carp()
28will report the error as occurring where Foo() was called,
29not where carp() was called.
30
4d935a29 31=head2 Forcing a Stack Trace
32
33As a debugging aid, you can force Carp to treat a croak as a confess
34and a carp as a cluck across I<all> modules. In other words, force a
35detailed stack trace to be given. This can be very helpful when trying
36to understand why, or from where, a warning or error is being generated.
37
f610777f 38This feature is enabled by 'importing' the non-existent symbol
4d935a29 39'verbose'. You would typically enable it by saying
40
41 perl -MCarp=verbose script.pl
42
43or by including the string C<MCarp=verbose> in the L<PERL5OPT>
44environment variable.
45
d2fe67be 46=head1 BUGS
47
48The Carp routines don't handle exception objects currently.
49If called with a first argument that is a reference, they simply
50call die() or warn(), as appropriate.
51
f06db76b 52=cut
53
4d935a29 54# This package is heavily used. Be small. Be fast. Be good.
a0d0e21e 55
7b8d334a 56# Comments added by Andy Wardley <abw@kfs.org> 09-Apr-98, based on an
57# _almost_ complete understanding of the package. Corrections and
58# comments are welcome.
59
60# The $CarpLevel variable can be set to "strip off" extra caller levels for
61# those times when Carp calls are buried inside other functions. The
62# $Max(EvalLen|(Arg(Len|Nums)) variables are used to specify how the eval
63# text and function arguments should be formatted when printed.
64
748a9306 65$CarpLevel = 0; # How many extra package levels to skip on carp.
c07a80fd 66$MaxEvalLen = 0; # How much eval '...text...' to show. 0 = all.
55497cff 67$MaxArgLen = 64; # How much of each argument to print. 0 = all.
68$MaxArgNums = 8; # How many arguments to print. 0 = all.
6ff81951 69$Verbose = 0; # If true then make shortmess call longmess instead
748a9306 70
66a4a569 71$CarpInternal{Carp}++;
72
a0d0e21e 73require Exporter;
fb73857a 74@ISA = ('Exporter');
a0d0e21e 75@EXPORT = qw(confess croak carp);
4d935a29 76@EXPORT_OK = qw(cluck verbose);
77@EXPORT_FAIL = qw(verbose); # hook to enable verbose mode
78
7b8d334a 79
80# if the caller specifies verbose usage ("perl -MCarp=verbose script.pl")
81# then the following method will be called by the Exporter which knows
82# to do this thanks to @EXPORT_FAIL, above. $_[1] will contain the word
83# 'verbose'.
84
4d935a29 85sub export_fail {
86 shift;
6ff81951 87 $Verbose = shift if $_[0] eq 'verbose';
4d935a29 88 return @_;
89}
90
a0d0e21e 91
7b8d334a 92# longmess() crawls all the way up the stack reporting on all the function
93# calls made. The error string, $error, is originally constructed from the
94# arguments passed into longmess() via confess(), cluck() or shortmess().
95# This gets appended with the stack trace messages which are generated for
96# each function call on the stack.
97
a0d0e21e 98sub longmess {
0bcd2fea 99 { local $@; require Carp::Heavy; } # XXX fix require to not clear $@?
3b5ca523 100 goto &longmess_heavy;
a0d0e21e 101}
102
7b8d334a 103
104# shortmess() is called by carp() and croak() to skip all the way up to
105# the top-level caller's package and report the error from there. confess()
106# and cluck() generate a full stack trace so they call longmess() to
6ff81951 107# generate that. In verbose mode shortmess() calls longmess() so
7b8d334a 108# you always get a stack trace
109
748a9306 110sub shortmess { # Short-circuit &longmess if called via multiple packages
0bcd2fea 111 { local $@; require Carp::Heavy; } # XXX fix require to not clear $@?
3b5ca523 112 goto &shortmess_heavy;
a0d0e21e 113}
114
7b8d334a 115
116# the following four functions call longmess() or shortmess() depending on
117# whether they should generate a full stack trace (confess() and cluck())
118# or simply report the caller's package (croak() and carp()), respectively.
119# confess() and croak() die, carp() and cluck() warn.
120
121sub croak { die shortmess @_ }
122sub confess { die longmess @_ }
123sub carp { warn shortmess @_ }
124sub cluck { warn longmess @_ }
a0d0e21e 125
748a9306 1261;