7c8932cc42b1d72d95dca9f535d52c8db10a5417
[p5sagit/p5-mst-13.2.git] / lib / Carp.pm
1 package Carp;
2
3 =head1 NAME
4
5 carp - warn of errors (from perspective of caller)
6
7 croak - die of errors (from perspective of caller)
8
9 confess - die of errors with stack backtrace
10
11 =head1 SYNOPSIS
12
13     use Carp;
14     croak "We're outta here!";
15
16 =head1 DESCRIPTION
17
18 The Carp routines are useful in your own modules because
19 they act like die() or warn(), but report where the error
20 was in the code they were called from.  Thus if you have a 
21 routine Foo() that has a carp() in it, then the carp() 
22 will report the error as occurring where Foo() was called, 
23 not where carp() was called.
24
25 =cut
26
27 # This package implements handy routines for modules that wish to throw
28 # exceptions outside of the current package.
29
30 $CarpLevel = 0;         # How many extra package levels to skip on carp.
31 $MaxEvalLen = 0;        # How much eval '...text...' to show. 0 = all.
32 $MaxArgLen = 64;        # How much of each argument to print. 0 = all.
33 $MaxArgNums = 8;        # How many arguments to print. 0 = all.
34
35 require Exporter;
36 @ISA = Exporter;
37 @EXPORT = qw(confess croak carp);
38
39 sub longmess {
40     my $error = join '', @_;
41     my $mess = "";
42     my $i = 1 + $CarpLevel;
43     my ($pack,$file,$line,$sub,$hargs,$eval,$require);
44     my (@a);
45     while (do { { package DB; @a = caller($i++) } } ) {
46       ($pack,$file,$line,$sub,$hargs,undef,$eval,$require) = @a;
47         if ($error =~ m/\n$/) {
48             $mess .= $error;
49         } else {
50             if (defined $eval) {
51                 if ($require) {
52                     $sub = "require $eval";
53                 } else {
54                     $eval =~ s/([\\\'])/\\$1/g;
55                     if ($MaxEvalLen && length($eval) > $MaxEvalLen) {
56                         substr($eval,$MaxEvalLen) = '...';
57                     }
58                     $sub = "eval '$eval'";
59                 }
60             } elsif ($sub eq '(eval)') {
61                 $sub = 'eval {...}';
62             }
63             if ($hargs) {
64               @a = @DB::args;   # must get local copy of args
65               if ($MaxArgNums and @a > $MaxArgNums) {
66                 $#a = $MaxArgNums;
67                 $a[$#a] = "...";
68               }
69               for (@a) {
70                 $_ = "undef", next unless defined $_;
71                 s/'/\\'/g;
72                 substr($_,$MaxArgLen) = '...' if $MaxArgLen and $MaxArgLen < length;
73                 $_ = "'$_'" unless /^-?[\d.]+$/;
74                 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
75                 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
76               }
77               $sub .= '(' . join(', ', @a) . ')';
78             }
79             $mess .= "\t$sub " if $error eq "called";
80             $mess .= "$error at $file line $line\n";
81         }
82         $error = "called";
83     }
84     # this kludge circumvents die's incorrect handling of NUL
85     my $msg = $mess ||= $error;
86     $msg =~ s/\0//g;
87     return $msg;
88 }
89
90 sub shortmess { # Short-circuit &longmess if called via multiple packages
91     my $error = join '', @_;
92     my ($prevpack) = caller(1);
93     my $extra = $CarpLevel;
94     my $i = 2;
95     my ($pack,$file,$line);
96     my %isa = ($prevpack,1);
97
98     @isa{@{"${prevpack}::ISA"}} = ()
99         if(defined @{"${prevpack}::ISA"});
100
101     while (($pack,$file,$line) = caller($i++)) {
102         if(defined @{$pack . "::ISA"}) {
103             my @i = @{$pack . "::ISA"};
104             my %i;
105             @i{@i} = ();
106             @isa{@i,$pack} = ()
107                 if(exists $i{$prevpack} || exists $isa{$pack});
108         }
109
110         next
111             if(exists $isa{$pack});
112
113         if ($extra-- > 0) {
114             %isa = ($pack,1);
115             @isa{@{$pack . "::ISA"}} = ()
116                 if(defined @{$pack . "::ISA"});
117         }
118         else {
119             # this kludge circumvents die's incorrect handling of NUL
120             (my $msg = "$error at $file line $line\n") =~ s/\0//g;
121             return $msg;
122         }
123     }
124     continue {
125         $prevpack = $pack;
126     }
127
128     goto &longmess;
129 }
130
131 sub confess { die longmess @_; }
132 sub croak { die shortmess @_; }
133 sub carp { warn shortmess @_; }
134
135 1;