Locale-related pod patches, take 2
[p5sagit/p5-mst-13.2.git] / lib / Carp.pm
CommitLineData
a0d0e21e 1package Carp;
2
f06db76b 3=head1 NAME
4
5carp - warn of errors (from perspective of caller)
6
7croak - die of errors (from perspective of caller)
8
9confess - 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
18The Carp routines are useful in your own modules because
19they act like die() or warn(), but report where the error
20was in the code they were called from. Thus if you have a
21routine Foo() that has a carp() in it, then the carp()
22will report the error as occurring where Foo() was called,
23not where carp() was called.
24
25=cut
26
a0d0e21e 27# This package implements handy routines for modules that wish to throw
28# exceptions outside of the current package.
29
748a9306 30$CarpLevel = 0; # How many extra package levels to skip on carp.
c07a80fd 31$MaxEvalLen = 0; # How much eval '...text...' to show. 0 = all.
55497cff 32$MaxArgLen = 64; # How much of each argument to print. 0 = all.
33$MaxArgNums = 8; # How many arguments to print. 0 = all.
748a9306 34
a0d0e21e 35require Exporter;
36@ISA = Exporter;
37@EXPORT = qw(confess croak carp);
38
39sub longmess {
40 my $error = shift;
41 my $mess = "";
748a9306 42 my $i = 1 + $CarpLevel;
55497cff 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;
c1bce5d7 47 if ($error =~ m/\n$/) {
48 $mess .= $error;
49 } else {
c07a80fd 50 if (defined $eval) {
51 if ($require) {
52 $sub = "require $eval";
53 } else {
9c7d8621 54 $eval =~ s/([\\\'])/\\$1/g;
c07a80fd 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 }
55497cff 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 s/'/\\'/g;
71 substr($_,$MaxArgLen) = '...' if $MaxArgLen and $MaxArgLen < length;
72 s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/;
73 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
74 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
75 }
76 $sub .= '(' . join(', ', @a) . ')';
77 }
c1bce5d7 78 $mess .= "\t$sub " if $error eq "called";
79 $mess .= "$error at $file line $line\n";
80 }
a0d0e21e 81 $error = "called";
82 }
83 $mess || $error;
84}
85
748a9306 86sub shortmess { # Short-circuit &longmess if called via multiple packages
87 my $error = $_[0]; # Instead of "shift"
9c7d8621 88 my ($prevpack) = caller(1);
748a9306 89 my $extra = $CarpLevel;
a0d0e21e 90 my $i = 2;
c07a80fd 91 my ($pack,$file,$line);
9c7d8621 92 my %isa = ($prevpack,1);
93
94 @isa{@{"${prevpack}::ISA"}} = ()
95 if(defined @{"${prevpack}::ISA"});
96
c07a80fd 97 while (($pack,$file,$line) = caller($i++)) {
9c7d8621 98 if(defined @{$pack . "::ISA"}) {
99 my @i = @{$pack . "::ISA"};
100 my %i;
101 @i{@i} = ();
102 @isa{@i,$pack} = ()
103 if(exists $i{$prevpack} || exists $isa{$pack});
104 }
105
106 next
107 if(exists $isa{$pack});
108
109 if ($extra-- > 0) {
110 %isa = ($pack,1);
111 @isa{@{$pack . "::ISA"}} = ()
112 if(defined @{$pack . "::ISA"});
113 }
114 else {
115 return "$error at $file line $line\n";
748a9306 116 }
a0d0e21e 117 }
9c7d8621 118 continue {
119 $prevpack = $pack;
120 }
121
748a9306 122 goto &longmess;
a0d0e21e 123}
124
125sub confess { die longmess @_; }
126sub croak { die shortmess @_; }
127sub carp { warn shortmess @_; }
128
748a9306 1291;