10 my $Is_VMS = $^O eq 'VMS';
12 use Carp qw(carp cluck croak confess);
17 # This test must be run at BEGIN time, because code later in this file
18 # sets CORE::GLOBAL::caller
19 ok !exists $CORE::GLOBAL::{caller},
20 "Loading doesn't create CORE::GLOBAL::caller";
24 local $SIG{__WARN__} = sub {
25 like $_[0], qr/ok (\d+)\n at.+\b(?i:carp\.t) line \d+$/, 'ok 2\n';
32 local $SIG{__WARN__} = sub {
33 like $_[0], qr/(\d+) at.+\b(?i:carp\.t) line \d+$/, 'carp 3';
40 local $SIG{__WARN__} = sub {
42 qr/^(\d+) at.+\b(?i:carp\.t) line \d+\n\tmain::sub_4\(\) called at.+\b(?i:carp\.t) line \d+$/,
52 local $SIG{__DIE__} = sub {
54 qr/^(\d+) at.+\b(?i:carp\.t) line \d+\n\teval \Q{...}\E called at.+\b(?i:carp\.t) line \d+$/,
62 local $SIG{__DIE__} = sub {
64 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+$/,
75 # test for caller_info API
76 my $eval = "use Carp; return Carp::caller_info(0);";
77 my %info = eval($eval);
78 is( $info{sub_name}, "eval '$eval'", 'caller_info API' );
80 # test for '...::CARP_NOT used only once' warning from Carp
84 local $SIG{__WARN__} = sub {
85 if ( defined $^S ) { warn $_[0] }
86 else { $warning = $_[0] }
93 eval { Carp::croak() };
96 ok !$warning, q/'...::CARP_NOT used only once' warning from Carp/;
98 # Test the location of error messages.
99 like( A::short(), qr/^Error at C/, "Short messages skip carped package" );
103 like( A::short(), qr/^Error at B/, "Short messages skip inheritance" );
108 like( A::short(), qr/^Error at B/, "Short messages skip inheritance" );
114 like( A::short(), qr/^Error at A/, "Inheritance is transitive" );
120 like( A::short(), qr/^Error at A/, "Inheritance is transitive" );
124 local @C::CARP_NOT = "D";
125 like( A::short(), qr/^Error at B/, "Short messages see \@CARP_NOT" );
129 local @D::CARP_NOT = "C";
130 like( A::short(), qr/^Error at B/, "Short messages see \@CARP_NOT" );
134 local @D::CARP_NOT = "B";
135 local @B::CARP_NOT = "C";
136 like( A::short(), qr/^Error at A/, "\@CARP_NOT is transitive" );
140 local @B::CARP_NOT = "D";
141 local @C::CARP_NOT = "B";
142 like( A::short(), qr/^Error at A/, "\@CARP_NOT is transitive" );
147 local @D::CARP_NOT = "B";
148 like( A::short(), qr/^Error at C/, "\@CARP_NOT overrides inheritance" );
153 local @D::CARP_NOT = "C";
154 like( A::short(), qr/^Error at B/, "\@CARP_NOT overrides inheritance" );
159 local $Carp::Internal{C} = 1;
160 like( A::short(), qr/^Error at B/, "Short doesn't report Internal" );
164 local $Carp::Internal{D} = 1;
165 like( A::long(), qr/^Error at C/, "Long doesn't report Internal" );
168 # %Carp::CarpInternal
170 local $Carp::CarpInternal{D} = 1;
172 A::short(), qr/^Error at B/,
173 "Short doesn't report calls to CarpInternal"
178 local $Carp::CarpInternal{D} = 1;
179 like( A::long(), qr/^Error at C/, "Long doesn't report CarpInternal" );
182 # tests for global variables
189 qr/t at \S*(?i:carp.t) line \d+/,
190 qr/t at \S*(?i:carp.t) line \d+\n\s*main::x\('t'\) called at \S*(?i:carp.t) line \d+/
194 for my $re (@$aref) {
195 local $Carp::Verbose = $i++;
196 local $SIG{__WARN__} = sub {
197 like $_[0], $re, 'Verbose';
209 my $txt = "Carp::cluck($test_num)";
210 local $Carp::MaxEvalLen = $_;
211 local $SIG{__WARN__} = sub {
212 "@_" =~ /'(.+?)(?:\n|')/s;
214 length( $_ ? substr( $txt, 0, $_ ) : substr( $txt, 0 ) ),
225 my $arg = 'testtest';
226 local $Carp::MaxArgLen = $_;
227 local $SIG{__WARN__} = sub {
230 length( $_ ? substr( $arg, 0, $_ ) : substr( $arg, 0 ) ),
243 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+/,
244 qr/1234 at \S*(?i:carp.t) line \d+\n\s*main::w\(1, 2, \.\.\.\) called at \S*(?i:carp.t) line \d+/,
248 local $Carp::MaxArgNums = $i++;
249 local $SIG{__WARN__} = sub {
250 like "@_", $_, 'MaxArgNums';
262 qr/1 at \S*(?i:carp.t) line \d+\n\s*main::w\(1\) called at \S*(?i:carp.t) line \d+/,
263 qr/1 at \S*(?i:carp.t) line \d+$/,
267 local $Carp::CarpLevel = $i++;
268 local $SIG{__WARN__} = sub {
269 like "@_", $_, 'CarpLevel';
278 local $TODO = "VMS exit status semantics don't work this way" if $Is_VMS;
280 # Check that croak() and confess() don't clobber $!
282 prog => 'use Carp; $@=q{Phooey}; $!=42; croak(q{Dead})',
286 is( $? >> 8, 42, 'croak() doesn\'t clobber $!' );
289 prog => 'use Carp; $@=q{Phooey}; $!=42; confess(q{Dead})',
293 is( $? >> 8, 42, 'confess() doesn\'t clobber $!' );
296 # undef used to be incorrectly reported as the string "undef"
299 local $SIG{__WARN__} = sub {
301 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+$/,
302 "cluck doesn't quote undef";
309 cluck_undef( 0, "undef", 2, undef, 4 );
311 # check that Carp respects CORE::GLOBAL::caller override after Carp
313 for my $bodge_job ( 2, 1, 0 ) {
314 print '# ', ( $bodge_job ? 'Not ' : '' ),
315 "setting \@DB::args in caller override\n";
316 if ( $bodge_job == 1 ) {
318 print "# required B\n";
321 local *CORE::GLOBAL::caller = sub {
322 local *__ANON__ = "fakecaller";
323 my @c = CORE::caller(@_);
325 $accum .= "@c[0..3]\n";
326 if ( !$bodge_job && CORE::caller() eq 'DB' ) {
329 return CORE::caller( ( $_[0] || 0 ) + 1 );
332 return CORE::caller( ( $_[0] || 0 ) + 1 );
335 eval "scalar caller()";
336 like( $accum, qr/main::fakecaller/,
337 "test CORE::GLOBAL::caller override in eval" );
339 my $got = A::long(42);
340 like( $accum, qr/main::fakecaller/,
341 "test CORE::GLOBAL::caller override in Carp" );
343 my $where = $bodge_job == 1 ? ' in &main::__ANON__' : '';
346 ? "\Q** Incomplete caller override detected$where; \@DB::args were not set **\E"
350 my $previous_package = $package;
353 qr/${package}::long\($warning\) called at $previous_package line \d+/,
354 "Correct arguments for $package" );
356 my $arg = $bodge_job ? $warning : 42;
358 $got, qr!A::long\($arg\) called at.+\b(?i:carp\.t) line \d+!,
359 'Correct arguments for A'
364 no warnings 'redefine';
365 sub CORE::GLOBAL::caller {
368 return CORE::caller($height);
372 my $got = A::long(42);
376 qr!A::long\(\Q** Incomplete caller override detected; \E\@DB::args\Q were not set **\E\) called at.+\b(?i:carp\.t) line \d+!,
377 'Correct arguments for A'
380 # UTF8-flagged strings should not cause Carp to try to load modules (even
381 # implicitly via utf8_heavy.pl) after a syntax error [perl #82854].
384 use utf8; use strict; use Carp;
385 BEGIN { $SIG{__DIE__} = sub { Carp::croak "aaaaa$_[0]" } }
390 'Carp can handle UTF8-flagged strings after a syntax error',
396 $SIG{__WARN__} = sub{};
397 carp ("A duck, but which duck?");
398 print "ok" unless exists $::{"B::"};
402 'Carp does not autovivify *B::'
444 eval { Carp::croak("Error") };
449 eval { Carp::confess("Error") };
453 # Put new tests at "new tests go here"