This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Skip over state declarations at run time
[perl5.git] / t / op / require_37033.t
1 #!perl -w
2 use strict;
3
4 # Check that require doesn't leave the handle it uses open, if it happens that
5 # the handle it opens gets file descriptor 0. RT #37033.
6
7 chdir 't' if -d 't';
8 require './test.pl';
9 @INC = 'lib';
10
11 sub test_require {
12     my ($state, $want) = @_;
13     delete $INC{'test_use_14937.pm'};
14     open my $fh, '<', 'README' or die "Can't open README: $!";
15     my $fileno = fileno $fh;
16     if (defined $want) {
17         is($fileno, $want,
18            "file handle has correct numeric file descriptor $state");
19     } else {
20         like($fileno, qr/\A\d+\z/,
21              "file handle has a numeric file descriptor $state");
22     }
23     close $fh or die;
24
25     is($INC{'test_use_14937.pm'}, undef, "test_use_14937 isn't loaded $state");
26     require test_use_14937;
27     isnt($INC{'test_use_14937.pm'}, undef, "test_use_14937 is loaded $state");
28
29     open $fh, '<', 'README' or die "Can't open README: $!";
30     is(fileno $fh, $fileno,
31        "file handle has the same numeric file descriptor $state");
32     close $fh or die;
33 }
34
35 is(fileno STDIN, 0, 'STDIN is open on file descriptor 0');
36 test_require('(STDIN open)');
37
38 close STDIN or die "Can't close STDIN: $!";
39
40 is(fileno STDIN, undef, 'STDIN is closed');
41 test_require('(STDIN closed)', 0);
42
43 done_testing();