5 use IPC::Open3 1.0103 qw(open3);
6 use Test::More tests => 58;
11 my $pid = open3($w, $r, undef, $^X, "-e", $args{prog});
14 while(<$r>) { $output .= $_; }
19 my $Is_VMS = $^O eq 'VMS';
21 use Carp qw(carp cluck croak confess);
24 # This test must be run at BEGIN time, because code later in this file
25 # sets CORE::GLOBAL::caller
26 ok !exists $CORE::GLOBAL::{caller},
27 "Loading doesn't create CORE::GLOBAL::caller";
31 local $SIG{__WARN__} = sub {
32 like $_[0], qr/ok (\d+)\n at.+\b(?i:carp\.t) line \d+$/, 'ok 2\n';
39 local $SIG{__WARN__} = sub {
40 like $_[0], qr/(\d+) at.+\b(?i:carp\.t) line \d+$/, 'carp 3';
47 local $SIG{__WARN__} = sub {
49 qr/^(\d+) at.+\b(?i:carp\.t) line \d+\n\tmain::sub_4\(\) called at.+\b(?i:carp\.t) line \d+$/,
59 local $SIG{__DIE__} = sub {
61 qr/^(\d+) at.+\b(?i:carp\.t) line \d+\n\teval \Q{...}\E called at.+\b(?i:carp\.t) line \d+$/,
69 local $SIG{__DIE__} = sub {
71 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+$/,
82 # test for caller_info API
83 my $eval = "use Carp; return Carp::caller_info(0);";
84 my %info = eval($eval);
85 is( $info{sub_name}, "eval '$eval'", 'caller_info API' );
87 # test for '...::CARP_NOT used only once' warning from Carp
91 local $SIG{__WARN__} = sub {
92 if ( defined $^S ) { warn $_[0] }
93 else { $warning = $_[0] }
100 eval { Carp::croak() };
103 ok !$warning, q/'...::CARP_NOT used only once' warning from Carp/;
105 # Test the location of error messages.
106 like( A::short(), qr/^Error at C/, "Short messages skip carped package" );
110 like( A::short(), qr/^Error at B/, "Short messages skip inheritance" );
115 like( A::short(), qr/^Error at B/, "Short messages skip inheritance" );
121 like( A::short(), qr/^Error at A/, "Inheritance is transitive" );
127 like( A::short(), qr/^Error at A/, "Inheritance is transitive" );
131 local @C::CARP_NOT = "D";
132 like( A::short(), qr/^Error at B/, "Short messages see \@CARP_NOT" );
136 local @D::CARP_NOT = "C";
137 like( A::short(), qr/^Error at B/, "Short messages see \@CARP_NOT" );
141 local @D::CARP_NOT = "B";
142 local @B::CARP_NOT = "C";
143 like( A::short(), qr/^Error at A/, "\@CARP_NOT is transitive" );
147 local @B::CARP_NOT = "D";
148 local @C::CARP_NOT = "B";
149 like( A::short(), qr/^Error at A/, "\@CARP_NOT is transitive" );
154 local @D::CARP_NOT = "B";
155 like( A::short(), qr/^Error at C/, "\@CARP_NOT overrides inheritance" );
160 local @D::CARP_NOT = "C";
161 like( A::short(), qr/^Error at B/, "\@CARP_NOT overrides inheritance" );
166 local $Carp::Internal{C} = 1;
167 like( A::short(), qr/^Error at B/, "Short doesn't report Internal" );
171 local $Carp::Internal{D} = 1;
172 like( A::long(), qr/^Error at C/, "Long doesn't report Internal" );
175 # %Carp::CarpInternal
177 local $Carp::CarpInternal{D} = 1;
179 A::short(), qr/^Error at B/,
180 "Short doesn't report calls to CarpInternal"
185 local $Carp::CarpInternal{D} = 1;
186 like( A::long(), qr/^Error at C/, "Long doesn't report CarpInternal" );
189 # tests for global variables
196 qr/t at \S*(?i:carp.t) line \d+/,
197 qr/t at \S*(?i:carp.t) line \d+\n\s*main::x\('t'\) called at \S*(?i:carp.t) line \d+/
201 for my $re (@$aref) {
202 local $Carp::Verbose = $i++;
203 local $SIG{__WARN__} = sub {
204 like $_[0], $re, 'Verbose';
216 my $txt = "Carp::cluck($test_num)";
217 local $Carp::MaxEvalLen = $_;
218 local $SIG{__WARN__} = sub {
219 "@_" =~ /'(.+?)(?:\n|')/s;
221 length( $_ ? substr( $txt, 0, $_ ) : substr( $txt, 0 ) ),
232 my $arg = 'testtest';
233 local $Carp::MaxArgLen = $_;
234 local $SIG{__WARN__} = sub {
237 length( $_ ? substr( $arg, 0, $_ ) : substr( $arg, 0 ) ),
250 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+/,
251 qr/1234 at \S*(?i:carp.t) line \d+\n\s*main::w\(1, 2, \.\.\.\) called at \S*(?i:carp.t) line \d+/,
255 local $Carp::MaxArgNums = $i++;
256 local $SIG{__WARN__} = sub {
257 like "@_", $_, 'MaxArgNums';
269 qr/1 at \S*(?i:carp.t) line \d+\n\s*main::w\(1\) called at \S*(?i:carp.t) line \d+/,
270 qr/1 at \S*(?i:carp.t) line \d+$/,
274 local $Carp::CarpLevel = $i++;
275 local $SIG{__WARN__} = sub {
276 like "@_", $_, 'CarpLevel';
285 local $TODO = "VMS exit status semantics don't work this way" if $Is_VMS;
287 # Check that croak() and confess() don't clobber $!
289 prog => 'use Carp; $@=q{Phooey}; $!=42; croak(q{Dead})',
293 is( $? >> 8, 42, 'croak() doesn\'t clobber $!' );
296 prog => 'use Carp; $@=q{Phooey}; $!=42; confess(q{Dead})',
300 is( $? >> 8, 42, 'confess() doesn\'t clobber $!' );
303 # undef used to be incorrectly reported as the string "undef"
306 local $SIG{__WARN__} = sub {
308 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+$/,
309 "cluck doesn't quote undef";
316 cluck_undef( 0, "undef", 2, undef, 4 );
318 # check that Carp respects CORE::GLOBAL::caller override after Carp
320 for my $bodge_job ( 2, 1, 0 ) { SKIP: {
321 skip "can't safely detect incomplete caller override on perl $]", 6
322 if $bodge_job && !Carp::CALLER_OVERRIDE_CHECK_OK;
323 print '# ', ( $bodge_job ? 'Not ' : '' ),
324 "setting \@DB::args in caller override\n";
325 if ( $bodge_job == 1 ) {
327 print "# required B\n";
330 local *CORE::GLOBAL::caller = sub {
331 local *__ANON__ = "fakecaller";
332 my @c = CORE::caller(@_);
334 $accum .= "@c[0..3]\n";
335 if ( !$bodge_job && CORE::caller() eq 'DB' ) {
338 return CORE::caller( ( $_[0] || 0 ) + 1 );
341 return CORE::caller( ( $_[0] || 0 ) + 1 );
344 eval "scalar caller()";
345 like( $accum, qr/main::fakecaller/,
346 "test CORE::GLOBAL::caller override in eval" );
348 my $got = A::long(42);
349 like( $accum, qr/main::fakecaller/,
350 "test CORE::GLOBAL::caller override in Carp" );
352 my $where = $bodge_job == 1 ? ' in &main::__ANON__' : '';
355 ? "\Q** Incomplete caller override detected$where; \@DB::args were not set **\E"
359 my $previous_package = $package;
362 qr/${package}::long\($warning\) called at $previous_package line \d+/,
363 "Correct arguments for $package" );
365 my $arg = $bodge_job ? $warning : 42;
367 $got, qr!A::long\($arg\) called at.+\b(?i:carp\.t) line \d+!,
368 'Correct arguments for A'
373 skip "can't safely detect incomplete caller override on perl $]", 1
374 unless Carp::CALLER_OVERRIDE_CHECK_OK;
376 no warnings 'redefine';
377 sub CORE::GLOBAL::caller {
380 return CORE::caller($height);
384 my $got = A::long(42);
388 qr!A::long\(\Q** Incomplete caller override detected; \E\@DB::args\Q were not set **\E\) called at.+\b(?i:carp\.t) line \d+!,
389 'Correct arguments for A'
393 # UTF8-flagged strings should not cause Carp to try to load modules (even
394 # implicitly via utf8_heavy.pl) after a syntax error [perl #82854].
398 use utf8; use strict; use Carp;
399 BEGIN { $SIG{__DIE__} = sub { Carp::croak qq(aaaaa$_[0]) } }
405 'Carp can handle UTF8-flagged strings after a syntax error',
410 skip("B:: always created when static", 1)
411 if $Config{static_ext} =~ /\bB\b/;
416 $SIG{__WARN__} = sub{};
417 carp (qq(A duck, but which duck?));
418 print q(ok) unless exists $::{q(B::)};
422 'Carp does not autovivify *B::',
465 eval { Carp::croak("Error") };
470 eval { Carp::confess("Error") };
474 # Put new tests at "new tests go here"