10 my $Is_VMS = $^O eq 'VMS';
12 use Carp qw(carp cluck croak confess);
18 { local $SIG{__WARN__} = sub {
19 like $_[0], qr/ok (\d+)\n at.+\b(?i:carp\.t) line \d+$/, 'ok 2\n' };
25 { local $SIG{__WARN__} = sub {
26 like $_[0], qr/(\d+) at.+\b(?i:carp\.t) line \d+$/, 'carp 3' };
34 local $SIG{__WARN__} = sub {
35 like $_[0], qr/^(\d+) at.+\b(?i:carp\.t) line \d+\n\tmain::sub_4\(\) called at.+\b(?i:carp\.t) line \d+$/, 'cluck 4' };
43 { local $SIG{__DIE__} = sub {
44 like $_[0], qr/^(\d+) at.+\b(?i:carp\.t) line \d+\n\teval \Q{...}\E called at.+\b(?i:carp\.t) line \d+$/, 'croak 5' };
50 local $SIG{__DIE__} = sub {
51 like $_[0], qr/^(\d+) at.+\b(?i:carp\.t) line \d+\n\teval \Q{...}\E called at.+\b(?i:carp\.t) line \d+\n\tmain::sub_6\(\) called at.+\b(?i:carp\.t) line \d+$/, 'confess 6' };
60 # test for caller_info API
61 my $eval = "use Carp; return Carp::caller_info(0);";
62 my %info = eval($eval);
63 is($info{sub_name}, "eval '$eval'", 'caller_info API');
65 # test for '...::CARP_NOT used only once' warning from Carp
69 local $SIG{__WARN__} =
70 sub { if( defined $^S ){ warn $_[0] } else { $warning = $_[0] } }
73 BEGIN { eval { Carp::croak() } }
75 ok !$warning, q/'...::CARP_NOT used only once' warning from Carp/;
77 # Test the location of error messages.
78 like(A::short(), qr/^Error at C/, "Short messages skip carped package");
82 like(A::short(), qr/^Error at B/, "Short messages skip inheritance");
87 like(A::short(), qr/^Error at B/, "Short messages skip inheritance");
93 like(A::short(), qr/^Error at A/, "Inheritance is transitive");
99 like(A::short(), qr/^Error at A/, "Inheritance is transitive");
103 local @C::CARP_NOT = "D";
104 like(A::short(), qr/^Error at B/, "Short messages see \@CARP_NOT");
108 local @D::CARP_NOT = "C";
109 like(A::short(), qr/^Error at B/, "Short messages see \@CARP_NOT");
113 local @D::CARP_NOT = "B";
114 local @B::CARP_NOT = "C";
115 like(A::short(), qr/^Error at A/, "\@CARP_NOT is transitive");
119 local @B::CARP_NOT = "D";
120 local @C::CARP_NOT = "B";
121 like(A::short(), qr/^Error at A/, "\@CARP_NOT is transitive");
126 local @D::CARP_NOT = "B";
127 like(A::short(), qr/^Error at C/, "\@CARP_NOT overrides inheritance");
132 local @D::CARP_NOT = "C";
133 like(A::short(), qr/^Error at B/, "\@CARP_NOT overrides inheritance");
138 local $Carp::Internal{C} = 1;
139 like(A::short(), qr/^Error at B/, "Short doesn't report Internal");
143 local $Carp::Internal{D} = 1;
144 like(A::long(), qr/^Error at C/, "Long doesn't report Internal");
147 # %Carp::CarpInternal
149 local $Carp::CarpInternal{D} = 1;
150 like(A::short(), qr/^Error at B/
151 , "Short doesn't report calls to CarpInternal");
155 local $Carp::CarpInternal{D} = 1;
156 like(A::long(), qr/^Error at C/, "Long doesn't report CarpInternal");
159 # tests for global variables
165 qr/t at \S*(?i:carp.t) line \d+/,
166 qr/t at \S*(?i:carp.t) line \d+\n\s*main::x\('t'\) called at \S*(?i:carp.t) line \d+/
170 for my $re (@$aref) {
171 local $Carp::Verbose = $i++;
172 local $SIG{__WARN__} = sub {
173 like $_[0], $re, 'Verbose';
183 my $txt = "Carp::cluck($test_num)";
184 local $Carp::MaxEvalLen = $_;
185 local $SIG{__WARN__} = sub {
186 "@_"=~/'(.+?)(?:\n|')/s;
187 is length($1), length($_?substr($txt,0,$_):substr($txt,0)), 'MaxEvalLen';
189 eval "$txt"; $test_num++;
196 my $arg = 'testtest';
197 local $Carp::MaxArgLen = $_;
198 local $SIG{__WARN__} = sub {
200 is length($1), length($_?substr($arg,0,$_):substr($arg,0)), 'MaxArgLen';
211 qr/1234 at \S*(?i:carp.t) line \d+\n\s*main::w\(1, 2, 3, 4\) called at \S*(?i:carp.t) line \d+/,
212 qr/1234 at \S*(?i:carp.t) line \d+\n\s*main::w\(1, 2, \.\.\.\) called at \S*(?i:carp.t) line \d+/,
216 local $Carp::MaxArgNums = $i++;
217 local $SIG{__WARN__} = sub {
218 like "@_", $_, 'MaxArgNums';
229 qr/1 at \S*(?i:carp.t) line \d+\n\s*main::w\(1\) called at \S*(?i:carp.t) line \d+/,
230 qr/1 at \S*(?i:carp.t) line \d+$/,
234 local $Carp::CarpLevel = $i++;
235 local $SIG{__WARN__} = sub {
236 like "@_", $_, 'CarpLevel';
245 local $TODO = "VMS exit status semantics don't work this way" if $Is_VMS;
247 # Check that croak() and confess() don't clobber $!
248 runperl(prog => 'use Carp; $@=q{Phooey}; $!=42; croak(q{Dead})',
251 is($?>>8, 42, 'croak() doesn\'t clobber $!');
253 runperl(prog => 'use Carp; $@=q{Phooey}; $!=42; confess(q{Dead})',
256 is($?>>8, 42, 'confess() doesn\'t clobber $!');
259 # undef used to be incorrectly reported as the string "undef"
262 local $SIG{__WARN__} = sub {
263 like $_[0], qr/^Bang! at.+\b(?i:carp\.t) line \d+\n\tmain::cluck_undef\(0, 'undef', 2, undef, 4\) called at.+\b(?i:carp\.t) line \d+$/, "cluck doesn't quote undef" };
269 cluck_undef (0, "undef", 2, undef, 4);
271 # check that Carp respects CORE::GLOBAL::caller override after Carp
275 local *CORE::GLOBAL::caller = sub {
276 local *__ANON__="fakecaller";
277 my @c=CORE::caller(@_);
279 $accum .= "@c[0..3]\n";
280 return CORE::caller(($_[0]||0)+1);
282 eval "scalar caller()";
283 like( $accum, qr/main::fakecaller/, "test CORE::GLOBAL::caller override in eval");
286 like( $accum, qr/main::fakecaller/, "test CORE::GLOBAL::caller override in Carp");
322 eval{ Carp::croak("Error") };
327 eval{ Carp::confess("Error") };