This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add note() to put informational notes into the TAP (from Test::More)
[perl5.git] / t / test.pl
CommitLineData
69026470 1#
f69d9fdf
KW
2# t/test.pl - most of Test::More functionality without the fuss, plus
3# has mappings native_to_latin1 and latin1_to_native so that fewer tests
4# on non ASCII-ish platforms need to be skipped
485f531e
DL
5
6
7# NOTE:
8#
9# Increment ($x++) has a certain amount of cleverness for things like
10#
11# $x = 'zz';
12# $x++; # $x eq 'aaa';
69026470 13#
485f531e
DL
14# stands more chance of breaking than just a simple
15#
16# $x = $x + 1
17#
18# In this file, we use the latter "Baby Perl" approach, and increment
19# will be worked over by t/op/inc.t
69026470 20
dcc7f481 21$Level = 1;
69026470
JH
22my $test = 1;
23my $planned;
6137113d 24my $noplan;
5fe9b82b 25my $Perl; # Safer version of $^X set by which_perl()
69026470 26
7d932aad 27$TODO = 0;
b6345914 28$NO_ENDING = 0;
7d932aad 29
3d66076a
MS
30# Use this instead of print to avoid interference while testing globals.
31sub _print {
32 local($\, $", $,) = (undef, ' ', '');
33 print STDOUT @_;
34}
35
36sub _print_stderr {
37 local($\, $", $,) = (undef, ' ', '');
38 print STDERR @_;
39}
40
69026470
JH
41sub plan {
42 my $n;
43 if (@_ == 1) {
44 $n = shift;
6137113d
NC
45 if ($n eq 'no_plan') {
46 undef $n;
47 $noplan = 1;
48 }
69026470
JH
49 } else {
50 my %plan = @_;
8210c8d3 51 $n = $plan{tests};
69026470 52 }
3d66076a 53 _print "1..$n\n" unless $noplan;
69026470
JH
54 $planned = $n;
55}
56
c4ef7183
MS
57
58# Set the plan at the end. See Test::More::done_testing.
59sub done_testing {
60 my $n = $test - 1;
61 $n = shift if @_;
62
63 _print "1..$n\n";
64 $planned = $n;
65}
66
67
69026470
JH
68END {
69 my $ran = $test - 1;
6137113d
NC
70 if (!$NO_ENDING) {
71 if (defined $planned && $planned != $ran) {
3d66076a 72 _print_stderr
6137113d
NC
73 "# Looks like you planned $planned tests but ran $ran.\n";
74 } elsif ($noplan) {
3d66076a 75 _print "1..$ran\n";
6137113d 76 }
69026470
JH
77 }
78}
79
de522f7a 80sub _diag {
cf8feb78 81 return unless @_;
92c9394b 82 my @mess = _comment(@_);
44826442 83 $TODO ? _print(@mess) : _print_stderr(@mess);
de522f7a
MS
84}
85
92c9394b
MS
86# Use this instead of "print STDERR" when outputing failure diagnostic
87# messages
485f531e
DL
88sub diag {
89 _diag(@_);
90}
91
92c9394b
MS
92# Use this instead of "print" when outputing informational messages
93sub note {
94 return unless @_;
95 _print( _comment(@_) );
96}
97
98sub _comment {
99 return map { /^#/ ? "$_\n" : "# $_\n" }
100 map { split /\n/ } @_;
101}
102
69026470
JH
103sub skip_all {
104 if (@_) {
7bb7fa38 105 _print "1..0 # Skip @_\n";
69026470 106 } else {
3d66076a 107 _print "1..0\n";
69026470
JH
108 }
109 exit(0);
110}
111
112sub _ok {
7d932aad 113 my ($pass, $where, $name, @mess) = @_;
69026470
JH
114 # Do not try to microoptimize by factoring out the "not ".
115 # VMS will avenge.
7d932aad
MS
116 my $out;
117 if ($name) {
b734d6c9
MS
118 # escape out '#' or it will interfere with '# skip' and such
119 $name =~ s/#/\\#/g;
7d932aad 120 $out = $pass ? "ok $test - $name" : "not ok $test - $name";
69026470 121 } else {
7d932aad 122 $out = $pass ? "ok $test" : "not ok $test";
69026470 123 }
7d932aad 124
11ea18f2 125 $out = $out . " # TODO $TODO" if $TODO;
3d66076a 126 _print "$out\n";
7d932aad 127
69026470 128 unless ($pass) {
de522f7a 129 _diag "# Failed $where\n";
69026470 130 }
7d932aad
MS
131
132 # Ensure that the message is properly escaped.
cf8feb78 133 _diag @mess;
7d932aad 134
485f531e 135 $test = $test + 1; # don't use ++
1577bb16
MS
136
137 return $pass;
69026470
JH
138}
139
140sub _where {
dcc7f481 141 my @caller = caller($Level);
69026470
JH
142 return "at $caller[1] line $caller[2]";
143}
144
1d662fb6 145# DON'T use this for matches. Use like() instead.
c3029c66 146sub ok ($@) {
7d932aad
MS
147 my ($pass, $name, @mess) = @_;
148 _ok($pass, _where(), $name, @mess);
69026470
JH
149}
150
b3c72391
JH
151sub _q {
152 my $x = shift;
153 return 'undef' unless defined $x;
154 my $q = $x;
d279d8f8
NC
155 $q =~ s/\\/\\\\/g;
156 $q =~ s/'/\\'/g;
b3c72391
JH
157 return "'$q'";
158}
159
677fb045
NC
160sub _qq {
161 my $x = shift;
162 return defined $x ? '"' . display ($x) . '"' : 'undef';
163};
164
165# keys are the codes \n etc map to, values are 2 char strings such as \n
166my %backslash_escape;
167foreach my $x (split //, 'nrtfa\\\'"') {
168 $backslash_escape{ord eval "\"\\$x\""} = "\\$x";
169}
170# A way to display scalars containing control characters and Unicode.
171# Trying to avoid setting $_, or relying on local $_ to work.
172sub display {
173 my @result;
174 foreach my $x (@_) {
175 if (defined $x and not ref $x) {
176 my $y = '';
177 foreach my $c (unpack("U*", $x)) {
178 if ($c > 255) {
11ea18f2 179 $y = $y . sprintf "\\x{%x}", $c;
677fb045 180 } elsif ($backslash_escape{$c}) {
11ea18f2 181 $y = $y . $backslash_escape{$c};
677fb045
NC
182 } else {
183 my $z = chr $c; # Maybe we can get away with a literal...
1cfccccd
KW
184 if ($z =~ /[[:^print:]]/) {
185
186 # Use octal for characters traditionally expressed as
187 # such: the low controls
188 if ($c <= 037) {
189 $z = sprintf "\\%03o", $c;
190 } else {
191 $z = sprintf "\\x{%x}", $c;
192 }
193 }
11ea18f2 194 $y = $y . $z;
677fb045
NC
195 }
196 }
197 $x = $y;
198 }
199 return $x unless wantarray;
200 push @result, $x;
201 }
202 return @result;
203}
204
c3029c66 205sub is ($$@) {
7d932aad 206 my ($got, $expected, $name, @mess) = @_;
c831d34f
MS
207
208 my $pass;
209 if( !defined $got || !defined $expected ) {
210 # undef only matches undef
211 $pass = !defined $got && !defined $expected;
212 }
213 else {
214 $pass = $got eq $expected;
215 }
216
69026470 217 unless ($pass) {
d5f8084a
KW
218 unshift(@mess, "# got "._qq($got)."\n",
219 "# expected "._qq($expected)."\n");
69026470 220 }
7d932aad 221 _ok($pass, _where(), $name, @mess);
69026470
JH
222}
223
c3029c66 224sub isnt ($$@) {
3e90d5a3 225 my ($got, $isnt, $name, @mess) = @_;
c831d34f
MS
226
227 my $pass;
228 if( !defined $got || !defined $isnt ) {
229 # undef only matches undef
230 $pass = defined $got || defined $isnt;
231 }
232 else {
233 $pass = $got ne $isnt;
234 }
235
3e90d5a3 236 unless( $pass ) {
d5f8084a 237 unshift(@mess, "# it should not be "._qq($got)."\n",
3e90d5a3
MS
238 "# but it is.\n");
239 }
240 _ok($pass, _where(), $name, @mess);
241}
242
c3029c66 243sub cmp_ok ($$$@) {
58d76dfd
JH
244 my($got, $type, $expected, $name, @mess) = @_;
245
246 my $pass;
247 {
248 local $^W = 0;
249 local($@,$!); # don't interfere with $@
250 # eval() sometimes resets $!
251 $pass = eval "\$got $type \$expected";
252 }
253 unless ($pass) {
254 # It seems Irix long doubles can have 2147483648 and 2147483648
255 # that stringify to the same thing but are acutally numerically
256 # different. Display the numbers if $type isn't a string operator,
257 # and the numbers are stringwise the same.
258 # (all string operators have alphabetic names, so tr/a-z// is true)
259 # This will also show numbers for some uneeded cases, but will
260 # definately be helpful for things such as == and <= that fail
261 if ($got eq $expected and $type !~ tr/a-z//) {
262 unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n";
263 }
d5f8084a
KW
264 unshift(@mess, "# got "._qq($got)."\n",
265 "# expected $type "._qq($expected)."\n");
58d76dfd
JH
266 }
267 _ok($pass, _where(), $name, @mess);
268}
269
270# Check that $got is within $range of $expected
271# if $range is 0, then check it's exact
272# else if $expected is 0, then $range is an absolute value
273# otherwise $range is a fractional error.
274# Here $range must be numeric, >= 0
275# Non numeric ranges might be a useful future extension. (eg %)
c3029c66 276sub within ($$$@) {
58d76dfd
JH
277 my ($got, $expected, $range, $name, @mess) = @_;
278 my $pass;
279 if (!defined $got or !defined $expected or !defined $range) {
280 # This is a fail, but doesn't need extra diagnostics
281 } elsif ($got !~ tr/0-9// or $expected !~ tr/0-9// or $range !~ tr/0-9//) {
282 # This is a fail
283 unshift @mess, "# got, expected and range must be numeric\n";
284 } elsif ($range < 0) {
285 # This is also a fail
286 unshift @mess, "# range must not be negative\n";
287 } elsif ($range == 0) {
288 # Within 0 is ==
289 $pass = $got == $expected;
290 } elsif ($expected == 0) {
291 # If expected is 0, treat range as absolute
292 $pass = ($got <= $range) && ($got >= - $range);
293 } else {
294 my $diff = $got - $expected;
295 $pass = abs ($diff / $expected) < $range;
296 }
297 unless ($pass) {
298 if ($got eq $expected) {
299 unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n";
300 }
d5f8084a
KW
301 unshift@mess, "# got "._qq($got)."\n",
302 "# expected "._qq($expected)." (within "._qq($range).")\n";
58d76dfd
JH
303 }
304 _ok($pass, _where(), $name, @mess);
305}
306
69026470 307# Note: this isn't quite as fancy as Test::More::like().
724aa791
JC
308
309sub like ($$@) { like_yn (0,@_) }; # 0 for -
310sub unlike ($$@) { like_yn (1,@_) }; # 1 for un-
311
312sub like_yn ($$$@) {
313 my ($flip, $got, $expected, $name, @mess) = @_;
69026470 314 my $pass;
724aa791
JC
315 $pass = $got =~ /$expected/ if !$flip;
316 $pass = $got !~ /$expected/ if $flip;
317 unless ($pass) {
318 unshift(@mess, "# got '$got'\n",
5a4a8c8b
NC
319 $flip
320 ? "# expected !~ /$expected/\n" : "# expected /$expected/\n");
69026470 321 }
5693d826 322 local $Level = $Level + 1;
7d932aad 323 _ok($pass, _where(), $name, @mess);
69026470
JH
324}
325
326sub pass {
327 _ok(1, '', @_);
328}
329
330sub fail {
331 _ok(0, _where(), @_);
332}
333
ad20d923 334sub curr_test {
cf8feb78 335 $test = shift if @_;
ad20d923
MS
336 return $test;
337}
338
3e90d5a3 339sub next_test {
178eff92 340 my $retval = $test;
485f531e 341 $test = $test + 1; # don't use ++
178eff92 342 $retval;
3e90d5a3
MS
343}
344
69026470
JH
345# Note: can't pass multipart messages since we try to
346# be compatible with Test::More::skip().
347sub skip {
7d932aad 348 my $why = shift;
982b7cb7 349 my $n = @_ ? shift : 1;
69026470 350 for (1..$n) {
7bb7fa38 351 _print "ok $test # skip $why\n";
485f531e 352 $test = $test + 1;
69026470
JH
353 }
354 local $^W = 0;
355 last SKIP;
356}
357
09f04786
MS
358sub todo_skip {
359 my $why = shift;
360 my $n = @_ ? shift : 1;
361
362 for (1..$n) {
7bb7fa38 363 _print "not ok $test # TODO & SKIP $why\n";
485f531e 364 $test = $test + 1;
09f04786
MS
365 }
366 local $^W = 0;
367 last TODO;
368}
369
69026470
JH
370sub eq_array {
371 my ($ra, $rb) = @_;
372 return 0 unless $#$ra == $#$rb;
373 for my $i (0..$#$ra) {
8210c8d3 374 next if !defined $ra->[$i] && !defined $rb->[$i];
135d199b
DM
375 return 0 if !defined $ra->[$i];
376 return 0 if !defined $rb->[$i];
69026470
JH
377 return 0 unless $ra->[$i] eq $rb->[$i];
378 }
379 return 1;
380}
381
677fb045
NC
382sub eq_hash {
383 my ($orig, $suspect) = @_;
384 my $fail;
385 while (my ($key, $value) = each %$suspect) {
386 # Force a hash recompute if this perl's internals can cache the hash key.
387 $key = "" . $key;
388 if (exists $orig->{$key}) {
389 if ($orig->{$key} ne $value) {
3d66076a 390 _print "# key ", _qq($key), " was ", _qq($orig->{$key}),
de522f7a 391 " now ", _qq($value), "\n";
677fb045
NC
392 $fail = 1;
393 }
394 } else {
3d66076a 395 _print "# key ", _qq($key), " is ", _qq($value),
75385f53 396 ", not in original.\n";
677fb045
NC
397 $fail = 1;
398 }
399 }
400 foreach (keys %$orig) {
401 # Force a hash recompute if this perl's internals can cache the hash key.
402 $_ = "" . $_;
403 next if (exists $suspect->{$_});
3d66076a 404 _print "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n";
677fb045
NC
405 $fail = 1;
406 }
407 !$fail;
408}
409
c3029c66 410sub require_ok ($) {
69026470
JH
411 my ($require) = @_;
412 eval <<REQUIRE_OK;
413require $require;
414REQUIRE_OK
1577bb16 415 _ok(!$@, _where(), "require $require");
69026470
JH
416}
417
c3029c66 418sub use_ok ($) {
69026470
JH
419 my ($use) = @_;
420 eval <<USE_OK;
421use $use;
422USE_OK
1577bb16 423 _ok(!$@, _where(), "use $use");
69026470
JH
424}
425
137352a2
RGS
426# runperl - Runs a separate perl interpreter.
427# Arguments :
428# switches => [ command-line switches ]
429# nolib => 1 # don't use -I../lib (included by default)
430# prog => one-liner (avoid quotes)
d83945bc 431# progs => [ multi-liner (avoid quotes) ]
137352a2
RGS
432# progfile => perl script
433# stdin => string to feed the stdin
434# stderr => redirect stderr to stdout
435# args => [ command-line arguments to the perl program ]
cb9c5e20 436# verbose => print the command line
137352a2
RGS
437
438my $is_mswin = $^O eq 'MSWin32';
439my $is_netware = $^O eq 'NetWare';
137352a2 440my $is_vms = $^O eq 'VMS';
e67ed694 441my $is_cygwin = $^O eq 'cygwin';
137352a2 442
cb9c5e20
JH
443sub _quote_args {
444 my ($runperl, $args) = @_;
445
446 foreach (@$args) {
447 # In VMS protect with doublequotes because otherwise
448 # DCL will lowercase -- unless already doublequoted.
ea9ac5ad 449 $_ = q(").$_.q(") if $is_vms && !/^\"/ && length($_) > 0;
1cce9906 450 $runperl = $runperl . ' ' . $_;
cb9c5e20 451 }
1cce9906 452 return $runperl;
cb9c5e20
JH
453}
454
4cd2bd1f 455sub _create_runperl { # Create the string to qx in runperl().
137352a2 456 my %args = @_;
5fe9b82b
JH
457 my $runperl = which_perl();
458 if ($runperl =~ m/\s/) {
459 $runperl = qq{"$runperl"};
460 }
6cf707aa
RGS
461 #- this allows, for example, to set PERL_RUNPERL_DEBUG=/usr/bin/valgrind
462 if ($ENV{PERL_RUNPERL_DEBUG}) {
463 $runperl = "$ENV{PERL_RUNPERL_DEBUG} $runperl";
464 }
f93a5f07 465 unless ($args{nolib}) {
11ea18f2 466 $runperl = $runperl . ' "-I../lib"'; # doublequotes because of VMS
137352a2 467 }
d83945bc 468 if ($args{switches}) {
343d4a7b
JH
469 local $Level = 2;
470 die "test.pl:runperl(): 'switches' must be an ARRAYREF " . _where()
471 unless ref $args{switches} eq "ARRAY";
1cce9906 472 $runperl = _quote_args($runperl, $args{switches});
d83945bc 473 }
137352a2 474 if (defined $args{prog}) {
21820af6
JH
475 die "test.pl:runperl(): both 'prog' and 'progs' cannot be used " . _where()
476 if defined $args{progs};
d83945bc
A
477 $args{progs} = [$args{prog}]
478 }
479 if (defined $args{progs}) {
21820af6
JH
480 die "test.pl:runperl(): 'progs' must be an ARRAYREF " . _where()
481 unless ref $args{progs} eq "ARRAY";
d83945bc
A
482 foreach my $prog (@{$args{progs}}) {
483 if ($is_mswin || $is_netware || $is_vms) {
11ea18f2 484 $runperl = $runperl . qq ( -e "$prog" );
d83945bc
A
485 }
486 else {
11ea18f2 487 $runperl = $runperl . qq ( -e '$prog' );
d83945bc
A
488 }
489 }
137352a2 490 } elsif (defined $args{progfile}) {
11ea18f2 491 $runperl = $runperl . qq( "$args{progfile}");
9a731dbd
NC
492 } else {
493 # You probaby didn't want to be sucking in from the upstream stdin
494 die "test.pl:runperl(): none of prog, progs, progfile, args, "
495 . " switches or stdin specified"
496 unless defined $args{args} or defined $args{switches}
497 or defined $args{stdin};
137352a2
RGS
498 }
499 if (defined $args{stdin}) {
dc459aad
JH
500 # so we don't try to put literal newlines and crs onto the
501 # command line.
502 $args{stdin} =~ s/\n/\\n/g;
503 $args{stdin} =~ s/\r/\\r/g;
5ae09a77 504
137352a2 505 if ($is_mswin || $is_netware || $is_vms) {
5fe9b82b 506 $runperl = qq{$Perl -e "print qq(} .
137352a2
RGS
507 $args{stdin} . q{)" | } . $runperl;
508 }
509 else {
5fe9b82b 510 $runperl = qq{$Perl -e 'print qq(} .
137352a2
RGS
511 $args{stdin} . q{)' | } . $runperl;
512 }
513 }
514 if (defined $args{args}) {
1cce9906 515 $runperl = _quote_args($runperl, $args{args});
cb9c5e20 516 }
11ea18f2 517 $runperl = $runperl . ' 2>&1' if $args{stderr};
cb9c5e20
JH
518 if ($args{verbose}) {
519 my $runperldisplay = $runperl;
520 $runperldisplay =~ s/\n/\n\#/g;
3d66076a 521 _print_stderr "# $runperldisplay\n";
137352a2 522 }
4cd2bd1f
JH
523 return $runperl;
524}
525
526sub runperl {
9a731dbd
NC
527 die "test.pl:runperl() does not take a hashref"
528 if ref $_[0] and ref $_[0] eq 'HASH';
4cd2bd1f 529 my $runperl = &_create_runperl;
613de57f
NC
530 my $result;
531
8210c8d3
MB
532 my $tainted = ${^TAINT};
533 my %args = @_;
485f531e 534 exists $args{switches} && grep m/^-T$/, @{$args{switches}} and $tainted = $tainted + 1;
8210c8d3
MB
535
536 if ($tainted) {
613de57f
NC
537 # We will assume that if you're running under -T, you really mean to
538 # run a fresh perl, so we'll brute force launder everything for you
539 my $sep;
540
afe79e7b 541 if (! eval 'require Config; 1') {
613de57f
NC
542 warn "test.pl had problems loading Config: $@";
543 $sep = ':';
544 } else {
afe79e7b 545 $sep = $Config::Config{path_sep};
a70a1627 546 }
613de57f
NC
547
548 my @keys = grep {exists $ENV{$_}} qw(CDPATH IFS ENV BASH_ENV);
549 local @ENV{@keys} = ();
550 # Untaint, plus take out . and empty string:
02bb3106 551 local $ENV{'DCL$PATH'} = $1 if $is_vms && exists($ENV{'DCL$PATH'}) && ($ENV{'DCL$PATH'} =~ /(.*)/s);
613de57f 552 $ENV{PATH} =~ /(.*)/s;
8210c8d3 553 local $ENV{PATH} =
3b6d8381 554 join $sep, grep { $_ ne "" and $_ ne "." and -d $_ and
326b5008 555 ($is_mswin or $is_vms or !(stat && (stat _)[2]&0022)) }
8210c8d3 556 split quotemeta ($sep), $1;
11ea18f2 557 $ENV{PATH} = $ENV{PATH} . "$sep/bin" if $is_cygwin; # Must have /bin under Cygwin
613de57f
NC
558
559 $runperl =~ /(.*)/s;
560 $runperl = $1;
561
562 $result = `$runperl`;
563 } else {
564 $result = `$runperl`;
a70a1627 565 }
137352a2
RGS
566 $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
567 return $result;
568}
569
140f5369
MS
570# Nice alias
571*run_perl = *run_perl = \&runperl; # shut up "used only once" warning
8799135f 572
c4fbe247 573sub DIE {
3d66076a 574 _print_stderr "# @_\n";
c4fbe247 575 exit 1;
8799135f
MS
576}
577
b5fe401b 578# A somewhat safer version of the sometimes wrong $^X.
17a740d5
JH
579sub which_perl {
580 unless (defined $Perl) {
581 $Perl = $^X;
8210c8d3 582
73421c4a
CB
583 # VMS should have 'perl' aliased properly
584 return $Perl if $^O eq 'VMS';
585
17a740d5 586 my $exe;
afe79e7b 587 if (! eval 'require Config; 1') {
17a740d5
JH
588 warn "test.pl had problems loading Config: $@";
589 $exe = '';
85363d30 590 } else {
afe79e7b 591 $exe = $Config::Config{_exe};
85363d30 592 }
da405c16 593 $exe = '' unless defined $exe;
8210c8d3 594
17a740d5
JH
595 # This doesn't absolutize the path: beware of future chdirs().
596 # We could do File::Spec->abs2rel() but that does getcwd()s,
597 # which is a bit heavyweight to do here.
8210c8d3 598
17a740d5 599 if ($Perl =~ /^perl\Q$exe\E$/i) {
8db06b02 600 my $perl = "perl$exe";
afe79e7b 601 if (! eval 'require File::Spec; 1') {
17a740d5 602 warn "test.pl had problems loading File::Spec: $@";
8db06b02 603 $Perl = "./$perl";
17a740d5 604 } else {
8db06b02 605 $Perl = File::Spec->catfile(File::Spec->curdir(), $perl);
17a740d5
JH
606 }
607 }
196918b0
PG
608
609 # Build up the name of the executable file from the name of
610 # the command.
611
612 if ($Perl !~ /\Q$exe\E$/i) {
11ea18f2 613 $Perl = $Perl . $exe;
196918b0 614 }
c880be78 615
8db06b02 616 warn "which_perl: cannot find $Perl from $^X" unless -f $Perl;
8210c8d3 617
17a740d5
JH
618 # For subcommands to use.
619 $ENV{PERLEXE} = $Perl;
85363d30 620 }
17a740d5 621 return $Perl;
b5fe401b
MS
622}
623
435e7af6
NC
624sub unlink_all {
625 foreach my $file (@_) {
626 1 while unlink $file;
3d66076a 627 _print_stderr "# Couldn't unlink '$file': $!\n" if -f $file;
435e7af6
NC
628 }
629}
eeabcb2d 630
748a4b20
NC
631my %tmpfiles;
632END { unlink_all keys %tmpfiles }
633
634# A regexp that matches the tempfile names
635$::tempfile_regexp = 'tmp\d+[A-Z][A-Z]?';
c1ddc35c 636
7a7e4936
NC
637# Avoid ++, avoid ranges, avoid split //
638my @letters = qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z);
639sub tempfile {
640 my $count = 0;
641 do {
642 my $temp = $count;
643 my $try = "tmp$$";
644 do {
11ea18f2 645 $try = $try . $letters[$temp % 26];
748a4b20 646 $temp = int ($temp / 26);
7a7e4936 647 } while $temp;
748a4b20
NC
648 # Need to note all the file names we allocated, as a second request may
649 # come before the first is created.
650 if (!-e $try && !$tmpfiles{$try}) {
c1ddc35c 651 # We have a winner
11ea18f2 652 $tmpfiles{$try} = 1;
c1ddc35c
NC
653 return $try;
654 }
7a7e4936
NC
655 $count = $count + 1;
656 } while $count < 26 * 26;
657 die "Can't find temporary file name starting 'tmp$$'";
658}
659
c1ddc35c 660# This is the temporary file for _fresh_perl
7a7e4936 661my $tmpfile = tempfile();
eeabcb2d 662
f5cda331
JH
663#
664# _fresh_perl
665#
666# The $resolve must be a subref that tests the first argument
667# for success, or returns the definition of success (e.g. the
668# expected scalar) if given no arguments.
669#
670
671sub _fresh_perl {
672 my($prog, $resolve, $runperl_args, $name) = @_;
eeabcb2d 673
11ea18f2
NC
674 # Given the choice of the mis-parsable {}
675 # (we want an anon hash, but a borked lexer might think that it's a block)
676 # or relying on taking a reference to a lexical
677 # (\ might be mis-parsed, and the reference counting on the pad may go
678 # awry)
679 # it feels like the least-worse thing is to assume that auto-vivification
680 # works. At least, this is only going to be a run-time failure, so won't
681 # affect tests using this file but not this function.
eeabcb2d
MS
682 $runperl_args->{progfile} = $tmpfile;
683 $runperl_args->{stderr} = 1;
684
685 open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!";
686
687 # VMS adjustments
688 if( $^O eq 'VMS' ) {
689 $prog =~ s#/dev/null#NL:#;
690
8210c8d3 691 # VMS file locking
eeabcb2d
MS
692 $prog =~ s{if \(-e _ and -f _ and -r _\)}
693 {if (-e _ and -f _)}
694 }
695
0d65d7d5 696 print TEST $prog;
eeabcb2d
MS
697 close TEST or die "Cannot close $tmpfile: $!";
698
699 my $results = runperl(%$runperl_args);
700 my $status = $?;
701
702 # Clean up the results into something a bit more predictable.
50f17f89 703 $results =~ s/\n+$//;
748a4b20
NC
704 $results =~ s/at\s+$::tempfile_regexp\s+line/at - line/g;
705 $results =~ s/of\s+$::tempfile_regexp\s+aborted/of - aborted/g;
eeabcb2d
MS
706
707 # bison says 'parse error' instead of 'syntax error',
708 # various yaccs may or may not capitalize 'syntax'.
709 $results =~ s/^(syntax|parse) error/syntax error/mig;
710
711 if ($^O eq 'VMS') {
712 # some tests will trigger VMS messages that won't be expected
713 $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
714
715 # pipes double these sometimes
716 $results =~ s/\n\n/\n/g;
717 }
718
f5cda331 719 my $pass = $resolve->($results);
eeabcb2d 720 unless ($pass) {
cf8feb78
MJD
721 _diag "# PROG: \n$prog\n";
722 _diag "# EXPECTED:\n", $resolve->(), "\n";
723 _diag "# GOT:\n$results\n";
724 _diag "# STATUS: $status\n";
eeabcb2d
MS
725 }
726
e2c38acd
JH
727 # Use the first line of the program as a name if none was given
728 unless( $name ) {
729 ($first_line, $name) = $prog =~ /^((.{1,50}).*)/;
11ea18f2 730 $name = $name . '...' if length $first_line > length $name;
e2c38acd 731 }
eeabcb2d 732
f5cda331
JH
733 _ok($pass, _where(), "fresh_perl - $name");
734}
735
736#
141f445b 737# fresh_perl_is
f5cda331
JH
738#
739# Combination of run_perl() and is().
740#
741
742sub fresh_perl_is {
743 my($prog, $expected, $runperl_args, $name) = @_;
50f17f89
MS
744
745 # _fresh_perl() is going to clip the trailing newlines off the result.
746 # This will make it so the test author doesn't have to know that.
747 $expected =~ s/\n+$//;
748
dcc7f481 749 local $Level = 2;
f5cda331
JH
750 _fresh_perl($prog,
751 sub { @_ ? $_[0] eq $expected : $expected },
752 $runperl_args, $name);
753}
754
755#
141f445b 756# fresh_perl_like
f5cda331
JH
757#
758# Combination of run_perl() and like().
759#
760
761sub fresh_perl_like {
762 my($prog, $expected, $runperl_args, $name) = @_;
dcc7f481 763 local $Level = 2;
f5cda331 764 _fresh_perl($prog,
077f8342 765 sub { @_ ? $_[0] =~ $expected : $expected },
f5cda331 766 $runperl_args, $name);
eeabcb2d
MS
767}
768
35a60386
RGS
769sub can_ok ($@) {
770 my($proto, @methods) = @_;
771 my $class = ref $proto || $proto;
772
773 unless( @methods ) {
774 return _ok( 0, _where(), "$class->can(...)" );
775 }
776
777 my @nok = ();
778 foreach my $method (@methods) {
779 local($!, $@); # don't interfere with caller's $@
780 # eval sometimes resets $!
781 eval { $proto->can($method) } || push @nok, $method;
782 }
783
784 my $name;
8210c8d3 785 $name = @methods == 1 ? "$class->can('$methods[0]')"
35a60386 786 : "$class->can(...)";
8210c8d3 787
35a60386
RGS
788 _ok( !@nok, _where(), $name );
789}
790
791sub isa_ok ($$;$) {
792 my($object, $class, $obj_name) = @_;
793
794 my $diag;
795 $obj_name = 'The object' unless defined $obj_name;
796 my $name = "$obj_name isa $class";
797 if( !defined $object ) {
798 $diag = "$obj_name isn't defined";
799 }
800 elsif( !ref $object ) {
801 $diag = "$obj_name isn't a reference";
802 }
803 else {
804 # We can't use UNIVERSAL::isa because we want to honor isa() overrides
805 local($@, $!); # eval sometimes resets $!
806 my $rslt = eval { $object->isa($class) };
807 if( $@ ) {
808 if( $@ =~ /^Can't call method "isa" on unblessed reference/ ) {
809 if( !UNIVERSAL::isa($object, $class) ) {
810 my $ref = ref $object;
811 $diag = "$obj_name isn't a '$class' it's a '$ref'";
812 }
813 } else {
814 die <<WHOA;
815WHOA! I tried to call ->isa on your object and got some weird error.
816This should never happen. Please contact the author immediately.
817Here's the error.
818$@
819WHOA
820 }
821 }
822 elsif( !$rslt ) {
823 my $ref = ref $object;
824 $diag = "$obj_name isn't a '$class' it's a '$ref'";
825 }
826 }
827
828 _ok( !$diag, _where(), $name );
829}
830
087986a7 831# Set a watchdog to timeout the entire test file
5fe9b82b
JH
832# NOTE: If the test file uses 'threads', then call the watchdog() function
833# _AFTER_ the 'threads' module is loaded.
5732108f 834sub watchdog ($;$)
087986a7
JH
835{
836 my $timeout = shift;
36436324 837 my $method = shift || "";
087986a7
JH
838 my $timeout_msg = 'Test process timed out - terminating';
839
e07ce2e4
GG
840 # Valgrind slows perl way down so give it more time before dying.
841 $timeout *= 10 if $ENV{PERL_VALGRIND};
842
087986a7
JH
843 my $pid_to_kill = $$; # PID for this process
844
5732108f
GG
845 if ($method eq "alarm") {
846 goto WATCHDOG_VIA_ALARM;
847 }
848
140f5369
MS
849 # shut up use only once warning
850 my $threads_on = $threads::threads && $threads::threads;
851
5fe9b82b
JH
852 # Don't use a watchdog process if 'threads' is loaded -
853 # use a watchdog thread instead
140f5369 854 if (!$threads_on) {
5fe9b82b
JH
855
856 # On Windows and VMS, try launching a watchdog process
857 # using system(1, ...) (see perlport.pod)
858 if (($^O eq 'MSWin32') || ($^O eq 'VMS')) {
859 # On Windows, try to get the 'real' PID
860 if ($^O eq 'MSWin32') {
861 eval { require Win32; };
862 if (defined(&Win32::GetCurrentProcessId)) {
863 $pid_to_kill = Win32::GetCurrentProcessId();
864 }
087986a7 865 }
087986a7 866
5fe9b82b
JH
867 # If we still have a fake PID, we can't use this method at all
868 return if ($pid_to_kill <= 0);
869
870 # Launch watchdog process
871 my $watchdog;
872 eval {
873 local $SIG{'__WARN__'} = sub {
874 _diag("Watchdog warning: $_[0]");
875 };
c1c45e36 876 my $sig = $^O eq 'VMS' ? 'TERM' : 'KILL';
9b7a5066
CB
877 my $cmd = _create_runperl( prog => "sleep($timeout);" .
878 "warn qq/# $timeout_msg" . '\n/;' .
c1c45e36 879 "kill($sig, $pid_to_kill);");
9b7a5066 880 $watchdog = system(1, $cmd);
5fe9b82b
JH
881 };
882 if ($@ || ($watchdog <= 0)) {
883 _diag('Failed to start watchdog');
884 _diag($@) if $@;
885 undef($watchdog);
886 return;
887 }
087986a7 888
5fe9b82b
JH
889 # Add END block to parent to terminate and
890 # clean up watchdog process
7e1027b9
JH
891 eval "END { local \$! = 0; local \$? = 0;
892 wait() if kill('KILL', $watchdog); };";
5fe9b82b 893 return;
087986a7 894 }
087986a7 895
5fe9b82b
JH
896 # Try using fork() to generate a watchdog process
897 my $watchdog;
898 eval { $watchdog = fork() };
899 if (defined($watchdog)) {
900 if ($watchdog) { # Parent process
901 # Add END block to parent to terminate and
902 # clean up watchdog process
7e1027b9
JH
903 eval "END { local \$! = 0; local \$? = 0;
904 wait() if kill('KILL', $watchdog); };";
5fe9b82b
JH
905 return;
906 }
907
908 ### Watchdog process code
087986a7 909
5fe9b82b
JH
910 # Load POSIX if available
911 eval { require POSIX; };
087986a7 912
5fe9b82b
JH
913 # Execute the timeout
914 sleep($timeout - 2) if ($timeout > 2); # Workaround for perlbug #49073
915 sleep(2);
087986a7 916
5fe9b82b
JH
917 # Kill test process if still running
918 if (kill(0, $pid_to_kill)) {
919 _diag($timeout_msg);
920 kill('KILL', $pid_to_kill);
921 }
087986a7 922
5fe9b82b
JH
923 # Don't execute END block (added at beginning of this file)
924 $NO_ENDING = 1;
087986a7 925
5fe9b82b
JH
926 # Terminate ourself (i.e., the watchdog)
927 POSIX::_exit(1) if (defined(&POSIX::_exit));
928 exit(1);
087986a7
JH
929 }
930
5fe9b82b 931 # fork() failed - fall through and try using a thread
087986a7
JH
932 }
933
5fe9b82b
JH
934 # Use a watchdog thread because either 'threads' is loaded,
935 # or fork() failed
afe79e7b 936 if (eval 'require threads; 1') {
087986a7
JH
937 threads->create(sub {
938 # Load POSIX if available
939 eval { require POSIX; };
940
941 # Execute the timeout
c1c45e36 942 my $time_left = $timeout;
a6c9a815 943 do {
11ea18f2 944 $time_left = $time_left - sleep($time_left);
a6c9a815 945 } while ($time_left > 0);
087986a7
JH
946
947 # Kill the parent (and ourself)
5fe9b82b 948 select(STDERR); $| = 1;
087986a7
JH
949 _diag($timeout_msg);
950 POSIX::_exit(1) if (defined(&POSIX::_exit));
c1c45e36
CB
951 my $sig = $^O eq 'VMS' ? 'TERM' : 'KILL';
952 kill($sig, $pid_to_kill);
087986a7
JH
953 })->detach();
954 return;
955 }
956
5fe9b82b 957 # If everything above fails, then just use an alarm timeout
5732108f 958WATCHDOG_VIA_ALARM:
087986a7
JH
959 if (eval { alarm($timeout); 1; }) {
960 # Load POSIX if available
961 eval { require POSIX; };
962
963 # Alarm handler will do the actual 'killing'
964 $SIG{'ALRM'} = sub {
5fe9b82b 965 select(STDERR); $| = 1;
087986a7
JH
966 _diag($timeout_msg);
967 POSIX::_exit(1) if (defined(&POSIX::_exit));
c1c45e36
CB
968 my $sig = $^O eq 'VMS' ? 'TERM' : 'KILL';
969 kill($sig, $pid_to_kill);
087986a7
JH
970 };
971 }
972}
973
f69d9fdf
KW
974my $cp_0037 = # EBCDIC code page 0037
975 '\x00\x01\x02\x03\x37\x2D\x2E\x2F\x16\x05\x25\x0B\x0C\x0D\x0E\x0F' .
976 '\x10\x11\x12\x13\x3C\x3D\x32\x26\x18\x19\x3F\x27\x1C\x1D\x1E\x1F' .
977 '\x40\x5A\x7F\x7B\x5B\x6C\x50\x7D\x4D\x5D\x5C\x4E\x6B\x60\x4B\x61' .
978 '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\x7A\x5E\x4C\x7E\x6E\x6F' .
979 '\x7C\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xD1\xD2\xD3\xD4\xD5\xD6' .
980 '\xD7\xD8\xD9\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xBA\xE0\xBB\xB0\x6D' .
981 '\x79\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96' .
982 '\x97\x98\x99\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xC0\x4F\xD0\xA1\x07' .
983 '\x20\x21\x22\x23\x24\x15\x06\x17\x28\x29\x2A\x2B\x2C\x09\x0A\x1B' .
984 '\x30\x31\x1A\x33\x34\x35\x36\x08\x38\x39\x3A\x3B\x04\x14\x3E\xFF' .
985 '\x41\xAA\x4A\xB1\x9F\xB2\x6A\xB5\xBD\xB4\x9A\x8A\x5F\xCA\xAF\xBC' .
986 '\x90\x8F\xEA\xFA\xBE\xA0\xB6\xB3\x9D\xDA\x9B\x8B\xB7\xB8\xB9\xAB' .
987 '\x64\x65\x62\x66\x63\x67\x9E\x68\x74\x71\x72\x73\x78\x75\x76\x77' .
988 '\xAC\x69\xED\xEE\xEB\xEF\xEC\xBF\x80\xFD\xFE\xFB\xFC\xAD\xAE\x59' .
989 '\x44\x45\x42\x46\x43\x47\x9C\x48\x54\x51\x52\x53\x58\x55\x56\x57' .
990 '\x8C\x49\xCD\xCE\xCB\xCF\xCC\xE1\x70\xDD\xDE\xDB\xDC\x8D\x8E\xDF';
991
992my $cp_1047 = # EBCDIC code page 1047
993 '\x00\x01\x02\x03\x37\x2D\x2E\x2F\x16\x05\x15\x0B\x0C\x0D\x0E\x0F' .
994 '\x10\x11\x12\x13\x3C\x3D\x32\x26\x18\x19\x3F\x27\x1C\x1D\x1E\x1F' .
995 '\x40\x5A\x7F\x7B\x5B\x6C\x50\x7D\x4D\x5D\x5C\x4E\x6B\x60\x4B\x61' .
996 '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\x7A\x5E\x4C\x7E\x6E\x6F' .
997 '\x7C\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xD1\xD2\xD3\xD4\xD5\xD6' .
998 '\xD7\xD8\xD9\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xAD\xE0\xBD\x5F\x6D' .
999 '\x79\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96' .
1000 '\x97\x98\x99\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xC0\x4F\xD0\xA1\x07' .
1001 '\x20\x21\x22\x23\x24\x25\x06\x17\x28\x29\x2A\x2B\x2C\x09\x0A\x1B' .
1002 '\x30\x31\x1A\x33\x34\x35\x36\x08\x38\x39\x3A\x3B\x04\x14\x3E\xFF' .
1003 '\x41\xAA\x4A\xB1\x9F\xB2\x6A\xB5\xBB\xB4\x9A\x8A\xB0\xCA\xAF\xBC' .
1004 '\x90\x8F\xEA\xFA\xBE\xA0\xB6\xB3\x9D\xDA\x9B\x8B\xB7\xB8\xB9\xAB' .
1005 '\x64\x65\x62\x66\x63\x67\x9E\x68\x74\x71\x72\x73\x78\x75\x76\x77' .
1006 '\xAC\x69\xED\xEE\xEB\xEF\xEC\xBF\x80\xFD\xFE\xFB\xFC\xBA\xAE\x59' .
1007 '\x44\x45\x42\x46\x43\x47\x9C\x48\x54\x51\x52\x53\x58\x55\x56\x57' .
1008 '\x8C\x49\xCD\xCE\xCB\xCF\xCC\xE1\x70\xDD\xDE\xDB\xDC\x8D\x8E\xDF';
1009
1010my $cp_bc = # EBCDIC code page POSiX-BC
1011 '\x00\x01\x02\x03\x37\x2D\x2E\x2F\x16\x05\x15\x0B\x0C\x0D\x0E\x0F' .
1012 '\x10\x11\x12\x13\x3C\x3D\x32\x26\x18\x19\x3F\x27\x1C\x1D\x1E\x1F' .
1013 '\x40\x5A\x7F\x7B\x5B\x6C\x50\x7D\x4D\x5D\x5C\x4E\x6B\x60\x4B\x61' .
1014 '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\x7A\x5E\x4C\x7E\x6E\x6F' .
1015 '\x7C\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xD1\xD2\xD3\xD4\xD5\xD6' .
1016 '\xD7\xD8\xD9\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xBB\xBC\xBD\x6A\x6D' .
1017 '\x4A\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96' .
1018 '\x97\x98\x99\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xFB\x4F\xFD\xFF\x07' .
1019 '\x20\x21\x22\x23\x24\x25\x06\x17\x28\x29\x2A\x2B\x2C\x09\x0A\x1B' .
1020 '\x30\x31\x1A\x33\x34\x35\x36\x08\x38\x39\x3A\x3B\x04\x14\x3E\x5F' .
1021 '\x41\xAA\xB0\xB1\x9F\xB2\xD0\xB5\x79\xB4\x9A\x8A\xBA\xCA\xAF\xA1' .
1022 '\x90\x8F\xEA\xFA\xBE\xA0\xB6\xB3\x9D\xDA\x9B\x8B\xB7\xB8\xB9\xAB' .
1023 '\x64\x65\x62\x66\x63\x67\x9E\x68\x74\x71\x72\x73\x78\x75\x76\x77' .
1024 '\xAC\x69\xED\xEE\xEB\xEF\xEC\xBF\x80\xE0\xFE\xDD\xFC\xAD\xAE\x59' .
1025 '\x44\x45\x42\x46\x43\x47\x9C\x48\x54\x51\x52\x53\x58\x55\x56\x57' .
1026 '\x8C\x49\xCD\xCE\xCB\xCF\xCC\xE1\x70\xC0\xDE\xDB\xDC\x8D\x8E\xDF';
1027
1028my $straight = # Avoid ranges
1029 '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F' .
1030 '\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F' .
1031 '\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F' .
1032 '\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F' .
1033 '\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F' .
1034 '\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F' .
1035 '\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F' .
1036 '\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F' .
1037 '\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F' .
1038 '\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F' .
1039 '\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF' .
1040 '\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF' .
1041 '\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF' .
1042 '\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF' .
1043 '\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF' .
1044 '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF';
1045
1046# The following 2 functions allow tests to work on both EBCDIC and
1047# ASCII-ish platforms. They convert string scalars between the native
1048# character set and the set of 256 characters which is usually called
1049# Latin1.
1050#
1051# These routines don't work on UTF-EBCDIC and UTF-8.
1052
1053sub native_to_latin1($) {
1054 my $string = shift;
1055
1056 return $string if ord('^') == 94; # ASCII, Latin1
1057 my $cp;
1058 if (ord('^') == 95) { # EBCDIC 1047
1059 $cp = \$cp_1047;
1060 }
1061 elsif (ord('^') == 106) { # EBCDIC POSIX-BC
1062 $cp = \$cp_bc;
1063 }
1064 elsif (ord('^') == 176) { # EBCDIC 037 */
1065 $cp = \$cp_0037;
1066 }
1067 else {
1068 die "Unknown native character set";
1069 }
1070
1071 eval '$string =~ tr/' . $$cp . '/' . $straight . '/';
1072 return $string;
1073}
1074
1075sub latin1_to_native($) {
1076 my $string = shift;
1077
1078 return $string if ord('^') == 94; # ASCII, Latin1
1079 my $cp;
1080 if (ord('^') == 95) { # EBCDIC 1047
1081 $cp = \$cp_1047;
1082 }
1083 elsif (ord('^') == 106) { # EBCDIC POSIX-BC
1084 $cp = \$cp_bc;
1085 }
1086 elsif (ord('^') == 176) { # EBCDIC 037 */
1087 $cp = \$cp_0037;
1088 }
1089 else {
1090 die "Unknown native character set";
1091 }
1092
1093 eval '$string =~ tr/' . $straight . '/' . $$cp . '/';
1094 return $string;
1095}
1096
69026470 10971;