(retracted by #13622)
[p5sagit/p5-mst-13.2.git] / lib / Carp.pm
1 package Carp;
2
3 our $VERSION = '1.01';
4
5 =head1 NAME
6
7 carp    - warn of errors (from perspective of caller)
8
9 cluck   - warn of errors with stack backtrace
10           (not exported by default)
11
12 croak   - die of errors (from perspective of caller)
13
14 confess - die of errors with stack backtrace
15
16 =head1 SYNOPSIS
17
18     use Carp;
19     croak "We're outta here!";
20
21     use Carp qw(cluck);
22     cluck "This is how we got here!";
23
24 =head1 DESCRIPTION
25
26 The Carp routines are useful in your own modules because
27 they act like die() or warn(), but with a message which is more
28 likely to be useful to a user of your module.  In the case of
29 cluck and confess that context is a summary of every
30 call in the call-stack.  For a shorter message you can use carp
31 or croak which try to report the error as being from where
32 your module was called.  There is no guarantee that that is where
33 the error was, but it is a good educated guess.
34
35 Here is a more complete description of how the shorter message works.
36 What it does is search the call-stack for a function call stack where
37 it hasn't been told that there shouldn't be an error.  If every
38 call is marked safe, it then gives up and gives a full stack
39 backtrace instead.  In other words it presumes that the first likely
40 looking potential suspect is guilty.  Its rules for telling whether
41 a call shouldn't generate errors work as follows:
42
43 =over 4
44
45 =item 1.
46
47 Any call from a package to itself is safe. 
48
49 =item 2.
50
51 Packages claim that there won't be errors on calls to or from
52 packages explicitly marked as safe by inclusion in @CARP_NOT, or
53 (if that array is empty) @ISA.  The ability to override what
54 @ISA says is new in 5.8.
55
56 =item 3.
57
58 The trust in item 2 is transitive.  If A trusts B, and B
59 trusts C, then A trusts C.  So if you do not override @ISA
60 with @CARP_NOT, then this trust relationship is identical to,
61 "inherits from".
62
63 =item 4.
64
65 Any call from an internal Perl module is safe.  (Nothing keeps
66 user modules from marking themselves as internal to Perl, but
67 this practice is discouraged.)
68
69 =item 5.
70
71 Any call to Carp is safe.  (This rule is what keeps it from
72 reporting the error where you call carp or croak.)
73
74 =back
75
76 =head2 Forcing a Stack Trace
77
78 As a debugging aid, you can force Carp to treat a croak as a confess
79 and a carp as a cluck across I<all> modules. In other words, force a
80 detailed stack trace to be given.  This can be very helpful when trying
81 to understand why, or from where, a warning or error is being generated.
82
83 This feature is enabled by 'importing' the non-existent symbol
84 'verbose'. You would typically enable it by saying
85
86     perl -MCarp=verbose script.pl
87
88 or by including the string C<MCarp=verbose> in the PERL5OPT
89 environment variable.
90
91 =head1 BUGS
92
93 The Carp routines don't handle exception objects currently.
94 If called with a first argument that is a reference, they simply
95 call die() or warn(), as appropriate.
96
97 =cut
98
99 # This package is heavily used. Be small. Be fast. Be good.
100
101 # Comments added by Andy Wardley <abw@kfs.org> 09-Apr-98, based on an
102 # _almost_ complete understanding of the package.  Corrections and
103 # comments are welcome.
104
105 # The members of %Internal are packages that are internal to perl.
106 # Carp will not report errors from within these packages if it
107 # can.  The members of %CarpInternal are internal to Perl's warning
108 # system.  Carp will not report errors from within these packages
109 # either, and will not report calls *to* these packages for carp and
110 # croak.  They replace $CarpLevel, which is deprecated.    The
111 # $Max(EvalLen|(Arg(Len|Nums)) variables are used to specify how the eval
112 # text and function arguments should be formatted when printed.
113
114 $CarpInternal{Carp}++;
115 $CarpLevel = 0;         # How many extra package levels to skip on carp.
116                         # How many calls to skip on confess.
117                         # Reconciling these notions is hard, use
118                         # %Internal and %CarpInternal instead.
119 $MaxEvalLen = 0;        # How much eval '...text...' to show. 0 = all.
120 $MaxArgLen = 64;        # How much of each argument to print. 0 = all.
121 $MaxArgNums = 8;        # How many arguments to print. 0 = all.
122 $Verbose = 0;           # If true then make shortmess call longmess instead
123
124 require Exporter;
125 @ISA = ('Exporter');
126 @EXPORT = qw(confess croak carp);
127 @EXPORT_OK = qw(cluck);
128
129 # we handle verbose usage ("perl -MCarp=verbose script.pl") ourselves
130 # and then erase all traces of this call so that Exporter doesn't
131 # know that we have been here.  BTW subclasses shouldn't try to
132 # do anything useful with 'verbose', including have that be their
133 # name...
134 sub import {
135     if (grep 'verbose' eq $_, @_) {
136         @_ = grep 'verbose' ne $_, @_;
137         $Verbose = "verbose";
138     }
139     goto &Exporter::import;
140 }
141
142
143 # longmess() crawls all the way up the stack reporting on all the function
144 # calls made.  The error string, $error, is originally constructed from the
145 # arguments passed into longmess() via confess(), cluck() or shortmess().
146 # This gets appended with the stack trace messages which are generated for
147 # each function call on the stack.
148
149 sub longmess {
150     { local $@; require Carp::Heavy; }  # XXX fix require to not clear $@?
151     # Icky backwards compatibility wrapper. :-(
152     my $call_pack = caller();
153     if ($Internal{$call_pack} or $CarpInternal{$call_pack}) {
154       return longmess_heavy(@_);
155     }
156     else {
157       local $CarpLevel = $CarpLevel + 1;
158       return longmess_heavy(@_);
159     }
160 }
161
162
163 # shortmess() is called by carp() and croak() to skip all the way up to
164 # the top-level caller's package and report the error from there.  confess()
165 # and cluck() generate a full stack trace so they call longmess() to
166 # generate that.  In verbose mode shortmess() calls longmess() so
167 # you always get a stack trace
168
169 sub shortmess { # Short-circuit &longmess if called via multiple packages
170     { local $@; require Carp::Heavy; }  # XXX fix require to not clear $@?
171     # Icky backwards compatibility wrapper. :-(
172     my $call_pack = caller();
173     local @CARP_NOT = caller();
174     shortmess_heavy(@_);
175 }
176
177
178 # the following four functions call longmess() or shortmess() depending on
179 # whether they should generate a full stack trace (confess() and cluck())
180 # or simply report the caller's package (croak() and carp()), respectively.
181 # confess() and croak() die, carp() and cluck() warn.
182
183 sub croak   { die  shortmess @_ }
184 sub confess { die  longmess  @_ }
185 sub carp    { warn shortmess @_ }
186 sub cluck   { warn longmess  @_ }
187
188 1;