Commit | Line | Data |
8d063cd8 |
1 | #!./perl |
2 | |
820475bd |
3 | BEGIN { |
4 | chdir 't' if -d 't'; |
20822f61 |
5 | @INC = '../lib'; |
820475bd |
6 | } |
7 | |
7d932aad |
8 | require "./test.pl"; |
137352a2 |
9 | |
ed2c6b9b |
10 | plan(tests => 22); |
820475bd |
11 | |
12 | use File::Spec; |
13 | |
14 | my $devnull = File::Spec->devnull; |
8d063cd8 |
15 | |
7d932aad |
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 |
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 |
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 |
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 |
42 | while (<>) { |
43 | $y .= $. . $_; |
378cc40b |
44 | if (eof()) { |
7d932aad |
45 | is($., 3, '$. counts <>'); |
8d063cd8 |
46 | } |
47 | } |
48 | |
7d932aad |
49 | is($y, "1a line\n2a line\n3a line\n", '<> from @ARGV'); |
50 | |
8d063cd8 |
51 | |
7d932aad |
52 | open(TRY, '>Io_argv1.tmp') or die "Can't open temp file: $!"; |
d1e4d418 |
53 | close TRY or die "Could not close: $!"; |
7d932aad |
54 | open(TRY, '>Io_argv2.tmp') or die "Can't open temp file: $!"; |
d1e4d418 |
55 | close 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 |
59 | my $i = 6; |
fbad3eb5 |
60 | while (<>) { |
684bef36 |
61 | s/^/ok $i\n/; |
62 | ++$i; |
fbad3eb5 |
63 | print; |
7d932aad |
64 | next_test(); |
fbad3eb5 |
65 | } |
7d932aad |
66 | open(TRY, '<Io_argv1.tmp') or die "Can't open temp file: $!"; |
67 | print while <TRY>; |
68 | open(TRY, '<Io_argv2.tmp') or die "Can't open temp file: $!"; |
69 | print while <TRY>; |
d1e4d418 |
70 | close TRY or die "Could not close: $!"; |
820475bd |
71 | undef $^I; |
72 | |
7d932aad |
73 | ok( eof TRY ); |
820475bd |
74 | |
7d932aad |
75 | ok( eof NEVEROPENED, 'eof() true on unopened filehandle' ); |
820475bd |
76 | |
684bef36 |
77 | open STDIN, 'Io_argv1.tmp' or die $!; |
820475bd |
78 | @ARGV = (); |
7d932aad |
79 | ok( !eof(), 'STDIN has something' ); |
820475bd |
80 | |
7d932aad |
81 | is( <>, "ok 6\n" ); |
820475bd |
82 | |
83 | open STDIN, $devnull or die $!; |
84 | @ARGV = (); |
7d932aad |
85 | ok( eof(), 'eof() true with empty @ARGV' ); |
820475bd |
86 | |
684bef36 |
87 | @ARGV = ('Io_argv1.tmp'); |
7d932aad |
88 | ok( !eof() ); |
820475bd |
89 | |
90 | @ARGV = ($devnull, $devnull); |
7d932aad |
91 | ok( !eof() ); |
820475bd |
92 | |
93 | close ARGV or die $!; |
7d932aad |
94 | ok( eof(), 'eof() true after closing ARGV' ); |
684bef36 |
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 |
100 | is( <F>, undef ); |
101 | |
684bef36 |
102 | open F, $devnull or die; |
7d932aad |
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 |
109 | ok( defined(<F>) ); |
110 | is( <F>, undef ); |
d1e4d418 |
111 | close F or die "Could not close: $!"; |
684bef36 |
112 | } |
fbad3eb5 |
113 | |
ed2c6b9b |
114 | # This used to dump core |
115 | fresh_perl_is( <<'**PROG**', "foobar", {}, "ARGV aliasing and eof()" ); |
116 | open OUT, ">Io_argv3.tmp" or die "Can't open temp file: $!"; |
117 | print OUT "foo"; |
118 | close OUT; |
119 | open IN, "Io_argv3.tmp" or die "Can't open temp file: $!"; |
120 | *ARGV = *IN; |
121 | while (<>) { |
122 | print; |
123 | print "bar" if eof(); |
124 | } |
125 | close IN; |
126 | unlink "Io_argv3.tmp"; |
127 | **PROG** |
128 | |
129 | END { |
130 | unlink 'Io_argv1.tmp', 'Io_argv1.tmp_bak', |
131 | 'Io_argv2.tmp', 'Io_argv2.tmp_bak', 'Io_argv3.tmp'; |
132 | } |