This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make utf8::encode respect magic
[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
768fd157 8BEGIN { require "./test.pl"; }
137352a2 9
1e1d4b91 10plan(tests => 23);
820475bd 11
8594e3bf 12my ($devnull, $no_devnull);
820475bd 13
8594e3bf
NC
14if (is_miniperl()) {
15 $no_devnull = "no dynamic loading on miniperl, File::Spec not built, so can't determine /dev/null";
16} else {
17 require File::Spec;
18 $devnull = File::Spec->devnull;
19}
8d063cd8 20
7d932aad
MS
21open(TRY, '>Io_argv1.tmp') || (die "Can't open temp file: $!");
22print TRY "a line\n";
d1e4d418 23close TRY or die "Could not close: $!";
8d063cd8 24
137352a2
RGS
25$x = runperl(
26 prog => 'while (<>) { print $., $_; }',
27 args => [ 'Io_argv1.tmp', 'Io_argv1.tmp' ],
28);
7d932aad 29is($x, "1a line\n2a line\n", '<> from two files');
8d063cd8 30
7d932aad 31{
137352a2
RGS
32 $x = runperl(
33 prog => 'while (<>) { print $_; }',
34 stdin => "foo\n",
35 args => [ 'Io_argv1.tmp', '-' ],
36 );
7d932aad 37 is($x, "a line\nfoo\n", ' from a file and STDIN');
8d063cd8 38
137352a2
RGS
39 $x = runperl(
40 prog => 'while (<>) { print $_; }',
41 stdin => "foo\n",
42 );
7d932aad 43 is($x, "foo\n", ' from just STDIN');
2986a63f 44}
8d063cd8 45
1e1d4b91 46{
47 # 5.10 stopped autovivifying scalars in globs leading to a
48 # segfault when $ARGV is written to.
49 runperl( prog => 'eof()', stdin => "nothing\n" );
50 is( 0+$?, 0, q(eof() doesn't segfault) );
51}
52
8594e3bf
NC
53@ARGV = is_miniperl() ? ('Io_argv1.tmp', 'Io_argv1.tmp', 'Io_argv1.tmp')
54 : ('Io_argv1.tmp', 'Io_argv1.tmp', $devnull, 'Io_argv1.tmp');
8d063cd8
LW
55while (<>) {
56 $y .= $. . $_;
378cc40b 57 if (eof()) {
7d932aad 58 is($., 3, '$. counts <>');
8d063cd8
LW
59 }
60}
61
7d932aad
MS
62is($y, "1a line\n2a line\n3a line\n", '<> from @ARGV');
63
8d063cd8 64
7d932aad 65open(TRY, '>Io_argv1.tmp') or die "Can't open temp file: $!";
d1e4d418 66close TRY or die "Could not close: $!";
7d932aad 67open(TRY, '>Io_argv2.tmp') or die "Can't open temp file: $!";
d1e4d418 68close TRY or die "Could not close: $!";
684bef36 69@ARGV = ('Io_argv1.tmp', 'Io_argv2.tmp');
7d932aad 70$^I = '_bak'; # not .bak which confuses VMS
fbad3eb5 71$/ = undef;
1e1d4b91 72my $i = 7;
fbad3eb5 73while (<>) {
684bef36
GS
74 s/^/ok $i\n/;
75 ++$i;
fbad3eb5 76 print;
7d932aad 77 next_test();
fbad3eb5 78}
7d932aad
MS
79open(TRY, '<Io_argv1.tmp') or die "Can't open temp file: $!";
80print while <TRY>;
81open(TRY, '<Io_argv2.tmp') or die "Can't open temp file: $!";
82print while <TRY>;
d1e4d418 83close TRY or die "Could not close: $!";
820475bd
GS
84undef $^I;
85
7d932aad 86ok( eof TRY );
820475bd 87
1031ca5c
SP
88{
89 no warnings 'once';
90 ok( eof NEVEROPENED, 'eof() true on unopened filehandle' );
91}
820475bd 92
684bef36 93open STDIN, 'Io_argv1.tmp' or die $!;
820475bd 94@ARGV = ();
7d932aad 95ok( !eof(), 'STDIN has something' );
820475bd 96
1e1d4b91 97is( <>, "ok 7\n" );
820475bd 98
8594e3bf
NC
99SKIP: {
100 skip_if_miniperl($no_devnull, 4);
101 open STDIN, $devnull or die $!;
102 @ARGV = ();
103 ok( eof(), 'eof() true with empty @ARGV' );
820475bd 104
8594e3bf
NC
105 @ARGV = ('Io_argv1.tmp');
106 ok( !eof() );
820475bd 107
8594e3bf
NC
108 @ARGV = ($devnull, $devnull);
109 ok( !eof() );
820475bd 110
8594e3bf
NC
111 close ARGV or die $!;
112 ok( eof(), 'eof() true after closing ARGV' );
113}
684bef36 114
8594e3bf 115SKIP: {
684bef36 116 local $/;
8594e3bf
NC
117 open my $fh, 'Io_argv1.tmp' or die "Could not open Io_argv1.tmp: $!";
118 <$fh>; # set $. = 1
119 is( <$fh>, undef );
120
121 skip_if_miniperl($no_devnull, 5);
7d932aad 122
8594e3bf
NC
123 open $fh, $devnull or die;
124 ok( defined(<$fh>) );
7d932aad 125
8594e3bf
NC
126 is( <$fh>, undef );
127 is( <$fh>, undef );
7d932aad 128
8594e3bf
NC
129 open $fh, $devnull or die; # restart cycle again
130 ok( defined(<$fh>) );
131 is( <$fh>, undef );
132 close $fh or die "Could not close: $!";
684bef36 133}
fbad3eb5 134
ed2c6b9b
RGS
135# This used to dump core
136fresh_perl_is( <<'**PROG**', "foobar", {}, "ARGV aliasing and eof()" );
137open OUT, ">Io_argv3.tmp" or die "Can't open temp file: $!";
138print OUT "foo";
139close OUT;
140open IN, "Io_argv3.tmp" or die "Can't open temp file: $!";
141*ARGV = *IN;
142while (<>) {
143 print;
144 print "bar" if eof();
145}
146close IN;
147unlink "Io_argv3.tmp";
148**PROG**
149
150END {
4813d64b 151 unlink_all 'Io_argv1.tmp', 'Io_argv1.tmp_bak',
ed2c6b9b
RGS
152 'Io_argv2.tmp', 'Io_argv2.tmp_bak', 'Io_argv3.tmp';
153}