1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
|
use warnings;
no warnings "once";
use Config;
use IPC::Open3 1.0103 qw(open3);
use Test::More tests => 62;
sub runperl {
my(%args) = @_;
my($w, $r);
my $pid = open3($w, $r, undef, $^X, "-e", $args{prog});
close $w;
my $output = "";
while(<$r>) { $output .= $_; }
waitpid($pid, 0);
return $output;
}
my $Is_VMS = $^O eq 'VMS';
use Carp qw(carp cluck croak confess);
BEGIN {
# This test must be run at BEGIN time, because code later in this file
# sets CORE::GLOBAL::caller
ok !exists $CORE::GLOBAL::{caller},
"Loading doesn't create CORE::GLOBAL::caller";
}
{
my $str = Carp::longmess("foo");
is(
$str,
"foo at t/Carp.t line 31.\n",
"we don't overshoot the top stack frame",
);
}
{
local $SIG{__WARN__} = sub {
like $_[0], qr/ok (\d+)\n at.+\b(?i:carp\.t) line \d+\.$/, 'ok 2\n';
};
carp "ok 2\n";
}
{
local $SIG{__WARN__} = sub {
like $_[0], qr/(\d+) at.+\b(?i:carp\.t) line \d+\.$/, 'carp 3';
};
carp 3;
}
sub sub_4 {
local $SIG{__WARN__} = sub {
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';
};
cluck 4;
}
sub_4;
{
local $SIG{__DIE__} = sub {
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';
};
eval { croak 5 };
}
sub sub_6 {
local $SIG{__DIE__} = sub {
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';
};
eval { confess 6 };
}
sub_6;
ok(1);
# test for caller_info API
my $eval = "use Carp; return Carp::caller_info(0);";
my %info = eval($eval);
is( $info{sub_name}, "eval '$eval'", 'caller_info API' );
# test for '...::CARP_NOT used only once' warning from Carp
my $warning;
eval {
BEGIN {
local $SIG{__WARN__} = sub {
if ( defined $^S ) { warn $_[0] }
else { $warning = $_[0] }
}
}
package Z;
BEGIN {
eval { Carp::croak() };
}
};
ok !$warning, q/'...::CARP_NOT used only once' warning from Carp/;
# Test the location of error messages.
like( A::short(), qr/^Error at C/, "Short messages skip carped package" );
{
local @C::ISA = "D";
like( A::short(), qr/^Error at B/, "Short messages skip inheritance" );
}
{
local @D::ISA = "C";
like( A::short(), qr/^Error at B/, "Short messages skip inheritance" );
}
{
local @D::ISA = "B";
local @B::ISA = "C";
like( A::short(), qr/^Error at A/, "Inheritance is transitive" );
}
{
local @B::ISA = "D";
local @C::ISA = "B";
like( A::short(), qr/^Error at A/, "Inheritance is transitive" );
}
{
local @C::CARP_NOT = "D";
like( A::short(), qr/^Error at B/, "Short messages see \@CARP_NOT" );
}
{
local @D::CARP_NOT = "C";
like( A::short(), qr/^Error at B/, "Short messages see \@CARP_NOT" );
}
{
local @D::CARP_NOT = "B";
local @B::CARP_NOT = "C";
like( A::short(), qr/^Error at A/, "\@CARP_NOT is transitive" );
}
{
local @B::CARP_NOT = "D";
local @C::CARP_NOT = "B";
like( A::short(), qr/^Error at A/, "\@CARP_NOT is transitive" );
}
{
local @D::ISA = "C";
local @D::CARP_NOT = "B";
like( A::short(), qr/^Error at C/, "\@CARP_NOT overrides inheritance" );
}
{
local @D::ISA = "B";
local @D::CARP_NOT = "C";
like( A::short(), qr/^Error at B/, "\@CARP_NOT overrides inheritance" );
}
# %Carp::Internal
{
local $Carp::Internal{C} = 1;
like( A::short(), qr/^Error at B/, "Short doesn't report Internal" );
}
{
local $Carp::Internal{D} = 1;
like( A::long(), qr/^Error at C/, "Long doesn't report Internal" );
}
# %Carp::CarpInternal
{
local $Carp::CarpInternal{D} = 1;
like(
A::short(), qr/^Error at B/,
"Short doesn't report calls to CarpInternal"
);
}
{
local $Carp::CarpInternal{D} = 1;
like( A::long(), qr/^Error at C/, "Long doesn't report CarpInternal" );
}
# tests for global variables
sub x { carp @_ }
sub w { cluck @_ }
# $Carp::Verbose;
{
my $aref = [
qr/t at \S*(?i:carp.t) line \d+\./,
qr/t at \S*(?i:carp.t) line \d+\.\n\s*main::x\('t'\) called at \S*(?i:carp.t) line \d+/
];
my $i = 0;
for my $re (@$aref) {
local $Carp::Verbose = $i++;
local $SIG{__WARN__} = sub {
like $_[0], $re, 'Verbose';
};
package Z;
main::x('t');
}
}
# $Carp::MaxEvalLen
{
my $test_num = 1;
for ( 0, 4 ) {
my $txt = "Carp::cluck($test_num)";
local $Carp::MaxEvalLen = $_;
local $SIG{__WARN__} = sub {
"@_" =~ /'(.+?)(?:\n|')/s;
is length($1),
length( $_ ? substr( $txt, 0, $_ ) : substr( $txt, 0 ) ),
'MaxEvalLen';
};
eval "$txt";
$test_num++;
}
}
# $Carp::MaxArgLen
{
for ( 0, 4 ) {
my $arg = 'testtest';
local $Carp::MaxArgLen = $_;
local $SIG{__WARN__} = sub {
"@_" =~ /'(.+?)'/;
is length($1),
length( $_ ? substr( $arg, 0, $_ ) : substr( $arg, 0 ) ),
'MaxArgLen';
};
package Z;
main::w($arg);
}
}
# $Carp::MaxArgNums
{
my $i = 0;
my $aref = [
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+/,
qr/1234 at \S*(?i:carp.t) line \d+\.\n\s*main::w\(1, 2, \.\.\.\) called at \S*(?i:carp.t) line \d+/,
];
for (@$aref) {
local $Carp::MaxArgNums = $i++;
local $SIG{__WARN__} = sub {
like "@_", $_, 'MaxArgNums';
};
package Z;
main::w( 1 .. 4 );
}
}
# $Carp::CarpLevel
{
my $i = 0;
my $aref = [
qr/1 at \S*(?i:carp.t) line \d+\.\n\s*main::w\(1\) called at \S*(?i:carp.t) line \d+/,
qr/1 at \S*(?i:carp.t) line \d+\.$/,
];
for (@$aref) {
local $Carp::CarpLevel = $i++;
local $SIG{__WARN__} = sub {
like "@_", $_, 'CarpLevel';
};
package Z;
main::w(1);
}
}
SKIP:
{
skip "IPC::Open3::open3 needs porting", 2 if $Is_VMS;
# Check that croak() and confess() don't clobber $!
runperl(
prog => 'use Carp; $@=q{Phooey}; $!=42; croak(q{Dead})',
stderr => 1
);
is( $? >> 8, 42, 'croak() doesn\'t clobber $!' );
runperl(
prog => 'use Carp; $@=q{Phooey}; $!=42; confess(q{Dead})',
stderr => 1
);
is( $? >> 8, 42, 'confess() doesn\'t clobber $!' );
}
# undef used to be incorrectly reported as the string "undef"
sub cluck_undef {
local $SIG{__WARN__} = sub {
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";
};
cluck "Bang!"
}
cluck_undef( 0, "undef", 2, undef, 4 );
# check that Carp respects CORE::GLOBAL::caller override after Carp
# has been compiled
for my $bodge_job ( 2, 1, 0 ) { SKIP: {
skip "can't safely detect incomplete caller override on perl $]", 6
if $bodge_job && !Carp::CALLER_OVERRIDE_CHECK_OK;
print '# ', ( $bodge_job ? 'Not ' : '' ),
"setting \@DB::args in caller override\n";
if ( $bodge_job == 1 ) {
require B;
print "# required B\n";
}
my $accum = '';
local *CORE::GLOBAL::caller = sub {
local *__ANON__ = "fakecaller";
my @c = CORE::caller(@_);
$c[0] ||= 'undef';
$accum .= "@c[0..3]\n";
if ( !$bodge_job && CORE::caller() eq 'DB' ) {
package DB;
return CORE::caller( ( $_[0] || 0 ) + 1 );
}
else {
return CORE::caller( ( $_[0] || 0 ) + 1 );
}
};
eval "scalar caller()";
like( $accum, qr/main::fakecaller/,
"test CORE::GLOBAL::caller override in eval" );
$accum = '';
my $got = A::long(42);
like( $accum, qr/main::fakecaller/,
"test CORE::GLOBAL::caller override in Carp" );
my $package = 'A';
my $where = $bodge_job == 1 ? ' in &main::__ANON__' : '';
my $warning
= $bodge_job
? "\Q** Incomplete caller override detected$where; \@DB::args were not set **\E"
: '';
for ( 0 .. 2 ) {
my $previous_package = $package;
++$package;
like( $got,
qr/${package}::long\($warning\) called at $previous_package line \d+/,
"Correct arguments for $package" );
}
my $arg = $bodge_job ? $warning : 42;
like(
$got, qr!A::long\($arg\) called at.+\b(?i:carp\.t) line \d+!,
'Correct arguments for A'
);
} }
SKIP: {
skip "can't safely detect incomplete caller override on perl $]", 1
unless Carp::CALLER_OVERRIDE_CHECK_OK;
eval q{
no warnings 'redefine';
sub CORE::GLOBAL::caller {
my $height = $_[0];
$height++;
return CORE::caller($height);
}
};
my $got = A::long(42);
like(
$got,
qr!A::long\(\Q** Incomplete caller override detected; \E\@DB::args\Q were not set **\E\) called at.+\b(?i:carp\.t) line \d+!,
'Correct arguments for A'
);
}
# UTF8-flagged strings should not cause Carp to try to load modules (even
# implicitly via utf8_heavy.pl) after a syntax error [perl #82854].
SKIP:
{
skip "IPC::Open3::open3 needs porting", 1 if $Is_VMS;
like(
runperl(
prog => q<
use utf8; use strict; use Carp;
BEGIN { $SIG{__DIE__} = sub { Carp::croak qq(aaaaa$_[0]) } }
$c
>,
stderr=>1,
),
qr/aaaaa/,
'Carp can handle UTF8-flagged strings after a syntax error',
);
}
SKIP:
{
skip "IPC::Open3::open3 needs porting", 1 if $Is_VMS;
skip("B:: always created when static", 1)
if $Config{static_ext} =~ /\bB\b/;
is(
runperl(
prog => q<
use Carp;
$SIG{__WARN__} = sub{};
carp (qq(A duck, but which duck?));
print q(ok) unless exists $::{q(B::)};
>,
),
'ok',
'Carp does not autovivify *B::',
);
}
# [perl #96672]
<D::DATA> for 1..2;
eval { croak 'heek' };
$@ =~ s/\n.*//; # just check first line
is $@, "heek at ".__FILE__." line ".(__LINE__-2).", <DATA> line 2.\n",
'last handle line num is mentioned';
SKIP:
{
skip "IPC::Open3::open3 needs porting", 1 if $Is_VMS;
like(
runperl(
prog => q<
open FH, q-Makefile.PL-;
<FH>; # set PL_last_in_gv
BEGIN { *CORE::GLOBAL::die = sub { die Carp::longmess(@_) } };
use Carp;
die fumpts;
>,
),
qr 'fumpts',
'Carp::longmess works inside CORE::GLOBAL::die',
);
}
{
package Foo::No::CARP_NOT;
eval { Carp::croak(1) };
::is_deeply(
[ keys %Foo::No::CARP_NOT:: ],
[],
"Carp doesn't create CARP_NOT or ISA in the caller if they don't exist"
);
}
# New tests go here
# line 1 "A"
package A;
sub short {
B::short();
}
sub long {
B::long();
}
# line 1 "B"
package B;
sub short {
C::short();
}
sub long {
C::long();
}
# line 1 "C"
package C;
sub short {
D::short();
}
sub long {
D::long();
}
# line 1 "D"
package D;
sub short {
eval { Carp::croak("Error") };
return $@;
}
sub long {
eval { Carp::confess("Error") };
return $@;
}
# Put new tests at "new tests go here"
__DATA__
1
2
3
|