11 my $Is_VMS = $^O eq 'VMS';
13 use Carp qw(carp cluck croak confess);
18 # This test must be run at BEGIN time, because code later in this file
19 # sets CORE::GLOBAL::caller
20 ok !exists $CORE::GLOBAL::{caller},
21 "Loading doesn't create CORE::GLOBAL::caller";
25 local $SIG{__WARN__} = sub {
26 like $_[0], qr/ok (\d+)\n at.+\b(?i:carp\.t) line \d+$/, 'ok 2\n';
33 local $SIG{__WARN__} = sub {
34 like $_[0], qr/(\d+) at.+\b(?i:carp\.t) line \d+$/, 'carp 3';
41 local $SIG{__WARN__} = sub {
43 qr/^(\d+) at.+\b(?i:carp\.t) line \d+\n\tmain::sub_4\(\) called at.+\b(?i:carp\.t) line \d+$/,
53 local $SIG{__DIE__} = sub {
55 qr/^(\d+) at.+\b(?i:carp\.t) line \d+\n\teval \Q{...}\E called at.+\b(?i:carp\.t) line \d+$/,
63 local $SIG{__DIE__} = sub {
65 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+$/,
76 # test for caller_info API
77 my $eval = "use Carp; return Carp::caller_info(0);";
78 my %info = eval($eval);
79 is( $info{sub_name}, "eval '$eval'", 'caller_info API' );
81 # test for '...::CARP_NOT used only once' warning from Carp
85 local $SIG{__WARN__} = sub {
86 if ( defined $^S ) { warn $_[0] }
87 else { $warning = $_[0] }
94 eval { Carp::croak() };
97 ok !$warning, q/'...::CARP_NOT used only once' warning from Carp/;
99 # Test the location of error messages.
100 like( A::short(), qr/^Error at C/, "Short messages skip carped package" );
104 like( A::short(), qr/^Error at B/, "Short messages skip inheritance" );
109 like( A::short(), qr/^Error at B/, "Short messages skip inheritance" );
115 like( A::short(), qr/^Error at A/, "Inheritance is transitive" );
121 like( A::short(), qr/^Error at A/, "Inheritance is transitive" );
125 local @C::CARP_NOT = "D";
126 like( A::short(), qr/^Error at B/, "Short messages see \@CARP_NOT" );
130 local @D::CARP_NOT = "C";
131 like( A::short(), qr/^Error at B/, "Short messages see \@CARP_NOT" );
135 local @D::CARP_NOT = "B";
136 local @B::CARP_NOT = "C";
137 like( A::short(), qr/^Error at A/, "\@CARP_NOT is transitive" );
141 local @B::CARP_NOT = "D";
142 local @C::CARP_NOT = "B";
143 like( A::short(), qr/^Error at A/, "\@CARP_NOT is transitive" );
148 local @D::CARP_NOT = "B";
149 like( A::short(), qr/^Error at C/, "\@CARP_NOT overrides inheritance" );
154 local @D::CARP_NOT = "C";
155 like( A::short(), qr/^Error at B/, "\@CARP_NOT overrides inheritance" );
160 local $Carp::Internal{C} = 1;
161 like( A::short(), qr/^Error at B/, "Short doesn't report Internal" );
165 local $Carp::Internal{D} = 1;
166 like( A::long(), qr/^Error at C/, "Long doesn't report Internal" );
169 # %Carp::CarpInternal
171 local $Carp::CarpInternal{D} = 1;
173 A::short(), qr/^Error at B/,
174 "Short doesn't report calls to CarpInternal"
179 local $Carp::CarpInternal{D} = 1;
180 like( A::long(), qr/^Error at C/, "Long doesn't report CarpInternal" );
183 # tests for global variables
190 qr/t at \S*(?i:carp.t) line \d+/,
191 qr/t at \S*(?i:carp.t) line \d+\n\s*main::x\('t'\) called at \S*(?i:carp.t) line \d+/
195 for my $re (@$aref) {
196 local $Carp::Verbose = $i++;
197 local $SIG{__WARN__} = sub {
198 like $_[0], $re, 'Verbose';
210 my $txt = "Carp::cluck($test_num)";
211 local $Carp::MaxEvalLen = $_;
212 local $SIG{__WARN__} = sub {
213 "@_" =~ /'(.+?)(?:\n|')/s;
215 length( $_ ? substr( $txt, 0, $_ ) : substr( $txt, 0 ) ),
226 my $arg = 'testtest';
227 local $Carp::MaxArgLen = $_;
228 local $SIG{__WARN__} = sub {
231 length( $_ ? substr( $arg, 0, $_ ) : substr( $arg, 0 ) ),
244 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+/,
245 qr/1234 at \S*(?i:carp.t) line \d+\n\s*main::w\(1, 2, \.\.\.\) called at \S*(?i:carp.t) line \d+/,
249 local $Carp::MaxArgNums = $i++;
250 local $SIG{__WARN__} = sub {
251 like "@_", $_, 'MaxArgNums';
263 qr/1 at \S*(?i:carp.t) line \d+\n\s*main::w\(1\) called at \S*(?i:carp.t) line \d+/,
264 qr/1 at \S*(?i:carp.t) line \d+$/,
268 local $Carp::CarpLevel = $i++;
269 local $SIG{__WARN__} = sub {
270 like "@_", $_, 'CarpLevel';
279 local $TODO = "VMS exit status semantics don't work this way" if $Is_VMS;
281 # Check that croak() and confess() don't clobber $!
283 prog => 'use Carp; $@=q{Phooey}; $!=42; croak(q{Dead})',
287 is( $? >> 8, 42, 'croak() doesn\'t clobber $!' );
290 prog => 'use Carp; $@=q{Phooey}; $!=42; confess(q{Dead})',
294 is( $? >> 8, 42, 'confess() doesn\'t clobber $!' );
297 # undef used to be incorrectly reported as the string "undef"
300 local $SIG{__WARN__} = sub {
302 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+$/,
303 "cluck doesn't quote undef";
310 cluck_undef( 0, "undef", 2, undef, 4 );
312 # check that Carp respects CORE::GLOBAL::caller override after Carp
314 for my $bodge_job ( 2, 1, 0 ) {
315 print '# ', ( $bodge_job ? 'Not ' : '' ),
316 "setting \@DB::args in caller override\n";
317 if ( $bodge_job == 1 ) {
319 print "# required B\n";
322 local *CORE::GLOBAL::caller = sub {
323 local *__ANON__ = "fakecaller";
324 my @c = CORE::caller(@_);
326 $accum .= "@c[0..3]\n";
327 if ( !$bodge_job && CORE::caller() eq 'DB' ) {
330 return CORE::caller( ( $_[0] || 0 ) + 1 );
333 return CORE::caller( ( $_[0] || 0 ) + 1 );
336 eval "scalar caller()";
337 like( $accum, qr/main::fakecaller/,
338 "test CORE::GLOBAL::caller override in eval" );
340 my $got = A::long(42);
341 like( $accum, qr/main::fakecaller/,
342 "test CORE::GLOBAL::caller override in Carp" );
344 my $where = $bodge_job == 1 ? ' in &main::__ANON__' : '';
347 ? "\Q** Incomplete caller override detected$where; \@DB::args were not set **\E"
351 my $previous_package = $package;
354 qr/${package}::long\($warning\) called at $previous_package line \d+/,
355 "Correct arguments for $package" );
357 my $arg = $bodge_job ? $warning : 42;
359 $got, qr!A::long\($arg\) called at.+\b(?i:carp\.t) line \d+!,
360 'Correct arguments for A'
365 no warnings 'redefine';
366 sub CORE::GLOBAL::caller {
369 return CORE::caller($height);
373 my $got = A::long(42);
377 qr!A::long\(\Q** Incomplete caller override detected; \E\@DB::args\Q were not set **\E\) called at.+\b(?i:carp\.t) line \d+!,
378 'Correct arguments for A'
381 # UTF8-flagged strings should not cause Carp to try to load modules (even
382 # implicitly via utf8_heavy.pl) after a syntax error [perl #82854].
385 use utf8; use strict; use Carp;
386 BEGIN { $SIG{__DIE__} = sub { Carp::croak "aaaaa$_[0]" } }
391 'Carp can handle UTF8-flagged strings after a syntax error',
396 skip("B:: always created when static", 1)
397 if $Config{static_ext} =~ /\bB\b/;
402 $SIG{__WARN__} = sub{};
403 carp ("A duck, but which duck?");
404 print "ok" unless exists $::{"B::"};
408 'Carp does not autovivify *B::'
451 eval { Carp::croak("Error") };
456 eval { Carp::confess("Error") };
460 # Put new tests at "new tests go here"