This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [PATCH] eof() coredumps when ARGV is aliased to another filehandle
[perl5.git] / t / io / argv.t
CommitLineData
8d063cd8
LW
1#!./perl
2
820475bd
GS
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
820475bd
GS
6}
7
7d932aad 8require "./test.pl";
137352a2 9
ed2c6b9b 10plan(tests => 22);
820475bd
GS
11
12use File::Spec;
13
14my $devnull = File::Spec->devnull;
8d063cd8 15
7d932aad
MS
16open(TRY, '>Io_argv1.tmp') || (die "Can't open temp file: $!");
17print TRY "a line\n";
d1e4d418 18close TRY or die "Could not close: $!";
8d063cd8 19
137352a2
RGS
20$x = runperl(
21 prog => 'while (<>) { print $., $_; }',
22 args => [ 'Io_argv1.tmp', 'Io_argv1.tmp' ],
23);
7d932aad 24is($x, "1a line\n2a line\n", '<> from two files');
8d063cd8 25
7d932aad 26{
137352a2
RGS
27 $x = runperl(
28 prog => 'while (<>) { print $_; }',
29 stdin => "foo\n",
30 args => [ 'Io_argv1.tmp', '-' ],
31 );
7d932aad 32 is($x, "a line\nfoo\n", ' from a file and STDIN');
8d063cd8 33
137352a2
RGS
34 $x = runperl(
35 prog => 'while (<>) { print $_; }',
36 stdin => "foo\n",
37 );
7d932aad 38 is($x, "foo\n", ' from just STDIN');
2986a63f 39}
8d063cd8 40
684bef36 41@ARGV = ('Io_argv1.tmp', 'Io_argv1.tmp', $devnull, 'Io_argv1.tmp');
8d063cd8
LW
42while (<>) {
43 $y .= $. . $_;
378cc40b 44 if (eof()) {
7d932aad 45 is($., 3, '$. counts <>');
8d063cd8
LW
46 }
47}
48
7d932aad
MS
49is($y, "1a line\n2a line\n3a line\n", '<> from @ARGV');
50
8d063cd8 51
7d932aad 52open(TRY, '>Io_argv1.tmp') or die "Can't open temp file: $!";
d1e4d418 53close TRY or die "Could not close: $!";
7d932aad 54open(TRY, '>Io_argv2.tmp') or die "Can't open temp file: $!";
d1e4d418 55close TRY or die "Could not close: $!";
684bef36 56@ARGV = ('Io_argv1.tmp', 'Io_argv2.tmp');
7d932aad 57$^I = '_bak'; # not .bak which confuses VMS
fbad3eb5 58$/ = undef;
684bef36 59my $i = 6;
fbad3eb5 60while (<>) {
684bef36
GS
61 s/^/ok $i\n/;
62 ++$i;
fbad3eb5 63 print;
7d932aad 64 next_test();
fbad3eb5 65}
7d932aad
MS
66open(TRY, '<Io_argv1.tmp') or die "Can't open temp file: $!";
67print while <TRY>;
68open(TRY, '<Io_argv2.tmp') or die "Can't open temp file: $!";
69print while <TRY>;
d1e4d418 70close TRY or die "Could not close: $!";
820475bd
GS
71undef $^I;
72
7d932aad 73ok( eof TRY );
820475bd 74
7d932aad 75ok( eof NEVEROPENED, 'eof() true on unopened filehandle' );
820475bd 76
684bef36 77open STDIN, 'Io_argv1.tmp' or die $!;
820475bd 78@ARGV = ();
7d932aad 79ok( !eof(), 'STDIN has something' );
820475bd 80
7d932aad 81is( <>, "ok 6\n" );
820475bd
GS
82
83open STDIN, $devnull or die $!;
84@ARGV = ();
7d932aad 85ok( eof(), 'eof() true with empty @ARGV' );
820475bd 86
684bef36 87@ARGV = ('Io_argv1.tmp');
7d932aad 88ok( !eof() );
820475bd
GS
89
90@ARGV = ($devnull, $devnull);
7d932aad 91ok( !eof() );
820475bd
GS
92
93close ARGV or die $!;
7d932aad 94ok( eof(), 'eof() true after closing ARGV' );
684bef36
GS
95
96{
97 local $/;
d1e4d418 98 open F, 'Io_argv1.tmp' or die "Could not open Io_argv1.tmp: $!";
684bef36 99 <F>; # set $. = 1
7d932aad
MS
100 is( <F>, undef );
101
684bef36 102 open F, $devnull or die;
7d932aad
MS
103 ok( defined(<F>) );
104
105 is( <F>, undef );
106 is( <F>, undef );
107
684bef36 108 open F, $devnull or die; # restart cycle again
7d932aad
MS
109 ok( defined(<F>) );
110 is( <F>, undef );
d1e4d418 111 close F or die "Could not close: $!";
684bef36 112}
fbad3eb5 113
ed2c6b9b
RGS
114# This used to dump core
115fresh_perl_is( <<'**PROG**', "foobar", {}, "ARGV aliasing and eof()" );
116open OUT, ">Io_argv3.tmp" or die "Can't open temp file: $!";
117print OUT "foo";
118close OUT;
119open IN, "Io_argv3.tmp" or die "Can't open temp file: $!";
120*ARGV = *IN;
121while (<>) {
122 print;
123 print "bar" if eof();
124}
125close IN;
126unlink "Io_argv3.tmp";
127**PROG**
128
129END {
130 unlink 'Io_argv1.tmp', 'Io_argv1.tmp_bak',
131 'Io_argv2.tmp', 'Io_argv2.tmp_bak', 'Io_argv3.tmp';
132}