Commit | Line | Data |
---|---|---|
8d063cd8 LW |
1 | #!./perl |
2 | ||
820475bd GS |
3 | BEGIN { |
4 | chdir 't' if -d 't'; | |
20822f61 | 5 | @INC = '../lib'; |
820475bd GS |
6 | } |
7 | ||
768fd157 | 8 | BEGIN { require "./test.pl"; } |
137352a2 | 9 | |
1e1d4b91 | 10 | plan(tests => 23); |
820475bd GS |
11 | |
12 | use File::Spec; | |
13 | ||
14 | my $devnull = File::Spec->devnull; | |
8d063cd8 | 15 | |
7d932aad MS |
16 | open(TRY, '>Io_argv1.tmp') || (die "Can't open temp file: $!"); |
17 | print TRY "a line\n"; | |
d1e4d418 | 18 | close 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 | 24 | is($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 | |
1e1d4b91 JJ |
41 | { |
42 | # 5.10 stopped autovivifying scalars in globs leading to a | |
43 | # segfault when $ARGV is written to. | |
44 | runperl( prog => 'eof()', stdin => "nothing\n" ); | |
45 | is( 0+$?, 0, q(eof() doesn't segfault) ); | |
46 | } | |
47 | ||
684bef36 | 48 | @ARGV = ('Io_argv1.tmp', 'Io_argv1.tmp', $devnull, 'Io_argv1.tmp'); |
8d063cd8 LW |
49 | while (<>) { |
50 | $y .= $. . $_; | |
378cc40b | 51 | if (eof()) { |
7d932aad | 52 | is($., 3, '$. counts <>'); |
8d063cd8 LW |
53 | } |
54 | } | |
55 | ||
7d932aad MS |
56 | is($y, "1a line\n2a line\n3a line\n", '<> from @ARGV'); |
57 | ||
8d063cd8 | 58 | |
7d932aad | 59 | open(TRY, '>Io_argv1.tmp') or die "Can't open temp file: $!"; |
d1e4d418 | 60 | close TRY or die "Could not close: $!"; |
7d932aad | 61 | open(TRY, '>Io_argv2.tmp') or die "Can't open temp file: $!"; |
d1e4d418 | 62 | close TRY or die "Could not close: $!"; |
684bef36 | 63 | @ARGV = ('Io_argv1.tmp', 'Io_argv2.tmp'); |
7d932aad | 64 | $^I = '_bak'; # not .bak which confuses VMS |
fbad3eb5 | 65 | $/ = undef; |
1e1d4b91 | 66 | my $i = 7; |
fbad3eb5 | 67 | while (<>) { |
684bef36 GS |
68 | s/^/ok $i\n/; |
69 | ++$i; | |
fbad3eb5 | 70 | print; |
7d932aad | 71 | next_test(); |
fbad3eb5 | 72 | } |
7d932aad MS |
73 | open(TRY, '<Io_argv1.tmp') or die "Can't open temp file: $!"; |
74 | print while <TRY>; | |
75 | open(TRY, '<Io_argv2.tmp') or die "Can't open temp file: $!"; | |
76 | print while <TRY>; | |
d1e4d418 | 77 | close TRY or die "Could not close: $!"; |
820475bd GS |
78 | undef $^I; |
79 | ||
7d932aad | 80 | ok( eof TRY ); |
820475bd | 81 | |
1031ca5c SP |
82 | { |
83 | no warnings 'once'; | |
84 | ok( eof NEVEROPENED, 'eof() true on unopened filehandle' ); | |
85 | } | |
820475bd | 86 | |
684bef36 | 87 | open STDIN, 'Io_argv1.tmp' or die $!; |
820475bd | 88 | @ARGV = (); |
7d932aad | 89 | ok( !eof(), 'STDIN has something' ); |
820475bd | 90 | |
1e1d4b91 | 91 | is( <>, "ok 7\n" ); |
820475bd GS |
92 | |
93 | open STDIN, $devnull or die $!; | |
94 | @ARGV = (); | |
7d932aad | 95 | ok( eof(), 'eof() true with empty @ARGV' ); |
820475bd | 96 | |
684bef36 | 97 | @ARGV = ('Io_argv1.tmp'); |
7d932aad | 98 | ok( !eof() ); |
820475bd GS |
99 | |
100 | @ARGV = ($devnull, $devnull); | |
7d932aad | 101 | ok( !eof() ); |
820475bd GS |
102 | |
103 | close ARGV or die $!; | |
7d932aad | 104 | ok( eof(), 'eof() true after closing ARGV' ); |
684bef36 GS |
105 | |
106 | { | |
107 | local $/; | |
d1e4d418 | 108 | open F, 'Io_argv1.tmp' or die "Could not open Io_argv1.tmp: $!"; |
684bef36 | 109 | <F>; # set $. = 1 |
7d932aad MS |
110 | is( <F>, undef ); |
111 | ||
684bef36 | 112 | open F, $devnull or die; |
7d932aad MS |
113 | ok( defined(<F>) ); |
114 | ||
115 | is( <F>, undef ); | |
116 | is( <F>, undef ); | |
117 | ||
684bef36 | 118 | open F, $devnull or die; # restart cycle again |
7d932aad MS |
119 | ok( defined(<F>) ); |
120 | is( <F>, undef ); | |
d1e4d418 | 121 | close F or die "Could not close: $!"; |
684bef36 | 122 | } |
fbad3eb5 | 123 | |
ed2c6b9b RGS |
124 | # This used to dump core |
125 | fresh_perl_is( <<'**PROG**', "foobar", {}, "ARGV aliasing and eof()" ); | |
126 | open OUT, ">Io_argv3.tmp" or die "Can't open temp file: $!"; | |
127 | print OUT "foo"; | |
128 | close OUT; | |
129 | open IN, "Io_argv3.tmp" or die "Can't open temp file: $!"; | |
130 | *ARGV = *IN; | |
131 | while (<>) { | |
132 | print; | |
133 | print "bar" if eof(); | |
134 | } | |
135 | close IN; | |
136 | unlink "Io_argv3.tmp"; | |
137 | **PROG** | |
138 | ||
139 | END { | |
98a392ec | 140 | 1 while unlink 'Io_argv1.tmp', 'Io_argv1.tmp_bak', |
ed2c6b9b RGS |
141 | 'Io_argv2.tmp', 'Io_argv2.tmp_bak', 'Io_argv3.tmp'; |
142 | } |