xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/t/io/argv.t (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!./perl
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateBEGIN {
4*0Sstevel@tonic-gate    chdir 't' if -d 't';
5*0Sstevel@tonic-gate    @INC = '../lib';
6*0Sstevel@tonic-gate}
7*0Sstevel@tonic-gate
8*0Sstevel@tonic-gaterequire "./test.pl";
9*0Sstevel@tonic-gate
10*0Sstevel@tonic-gateplan(tests => 22);
11*0Sstevel@tonic-gate
12*0Sstevel@tonic-gateuse File::Spec;
13*0Sstevel@tonic-gate
14*0Sstevel@tonic-gatemy $devnull = File::Spec->devnull;
15*0Sstevel@tonic-gate
16*0Sstevel@tonic-gateopen(TRY, '>Io_argv1.tmp') || (die "Can't open temp file: $!");
17*0Sstevel@tonic-gateprint TRY "a line\n";
18*0Sstevel@tonic-gateclose TRY or die "Could not close: $!";
19*0Sstevel@tonic-gate
20*0Sstevel@tonic-gate$x = runperl(
21*0Sstevel@tonic-gate    prog	=> 'while (<>) { print $., $_; }',
22*0Sstevel@tonic-gate    args	=> [ 'Io_argv1.tmp', 'Io_argv1.tmp' ],
23*0Sstevel@tonic-gate);
24*0Sstevel@tonic-gateis($x, "1a line\n2a line\n", '<> from two files');
25*0Sstevel@tonic-gate
26*0Sstevel@tonic-gate{
27*0Sstevel@tonic-gate    $x = runperl(
28*0Sstevel@tonic-gate	prog	=> 'while (<>) { print $_; }',
29*0Sstevel@tonic-gate	stdin	=> "foo\n",
30*0Sstevel@tonic-gate	args	=> [ 'Io_argv1.tmp', '-' ],
31*0Sstevel@tonic-gate    );
32*0Sstevel@tonic-gate    is($x, "a line\nfoo\n", '   from a file and STDIN');
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate    $x = runperl(
35*0Sstevel@tonic-gate	prog	=> 'while (<>) { print $_; }',
36*0Sstevel@tonic-gate	stdin	=> "foo\n",
37*0Sstevel@tonic-gate    );
38*0Sstevel@tonic-gate    is($x, "foo\n", '   from just STDIN');
39*0Sstevel@tonic-gate}
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate@ARGV = ('Io_argv1.tmp', 'Io_argv1.tmp', $devnull, 'Io_argv1.tmp');
42*0Sstevel@tonic-gatewhile (<>) {
43*0Sstevel@tonic-gate    $y .= $. . $_;
44*0Sstevel@tonic-gate    if (eof()) {
45*0Sstevel@tonic-gate	is($., 3, '$. counts <>');
46*0Sstevel@tonic-gate    }
47*0Sstevel@tonic-gate}
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gateis($y, "1a line\n2a line\n3a line\n", '<> from @ARGV');
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gate
52*0Sstevel@tonic-gateopen(TRY, '>Io_argv1.tmp') or die "Can't open temp file: $!";
53*0Sstevel@tonic-gateclose TRY or die "Could not close: $!";
54*0Sstevel@tonic-gateopen(TRY, '>Io_argv2.tmp') or die "Can't open temp file: $!";
55*0Sstevel@tonic-gateclose TRY or die "Could not close: $!";
56*0Sstevel@tonic-gate@ARGV = ('Io_argv1.tmp', 'Io_argv2.tmp');
57*0Sstevel@tonic-gate$^I = '_bak';   # not .bak which confuses VMS
58*0Sstevel@tonic-gate$/ = undef;
59*0Sstevel@tonic-gatemy $i = 6;
60*0Sstevel@tonic-gatewhile (<>) {
61*0Sstevel@tonic-gate    s/^/ok $i\n/;
62*0Sstevel@tonic-gate    ++$i;
63*0Sstevel@tonic-gate    print;
64*0Sstevel@tonic-gate    next_test();
65*0Sstevel@tonic-gate}
66*0Sstevel@tonic-gateopen(TRY, '<Io_argv1.tmp') or die "Can't open temp file: $!";
67*0Sstevel@tonic-gateprint while <TRY>;
68*0Sstevel@tonic-gateopen(TRY, '<Io_argv2.tmp') or die "Can't open temp file: $!";
69*0Sstevel@tonic-gateprint while <TRY>;
70*0Sstevel@tonic-gateclose TRY or die "Could not close: $!";
71*0Sstevel@tonic-gateundef $^I;
72*0Sstevel@tonic-gate
73*0Sstevel@tonic-gateok( eof TRY );
74*0Sstevel@tonic-gate
75*0Sstevel@tonic-gateok( eof NEVEROPENED,    'eof() true on unopened filehandle' );
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gateopen STDIN, 'Io_argv1.tmp' or die $!;
78*0Sstevel@tonic-gate@ARGV = ();
79*0Sstevel@tonic-gateok( !eof(),     'STDIN has something' );
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gateis( <>, "ok 6\n" );
82*0Sstevel@tonic-gate
83*0Sstevel@tonic-gateopen STDIN, $devnull or die $!;
84*0Sstevel@tonic-gate@ARGV = ();
85*0Sstevel@tonic-gateok( eof(),      'eof() true with empty @ARGV' );
86*0Sstevel@tonic-gate
87*0Sstevel@tonic-gate@ARGV = ('Io_argv1.tmp');
88*0Sstevel@tonic-gateok( !eof() );
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate@ARGV = ($devnull, $devnull);
91*0Sstevel@tonic-gateok( !eof() );
92*0Sstevel@tonic-gate
93*0Sstevel@tonic-gateclose ARGV or die $!;
94*0Sstevel@tonic-gateok( eof(),      'eof() true after closing ARGV' );
95*0Sstevel@tonic-gate
96*0Sstevel@tonic-gate{
97*0Sstevel@tonic-gate    local $/;
98*0Sstevel@tonic-gate    open F, 'Io_argv1.tmp' or die "Could not open Io_argv1.tmp: $!";
99*0Sstevel@tonic-gate    <F>;	# set $. = 1
100*0Sstevel@tonic-gate    is( <F>, undef );
101*0Sstevel@tonic-gate
102*0Sstevel@tonic-gate    open F, $devnull or die;
103*0Sstevel@tonic-gate    ok( defined(<F>) );
104*0Sstevel@tonic-gate
105*0Sstevel@tonic-gate    is( <F>, undef );
106*0Sstevel@tonic-gate    is( <F>, undef );
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate    open F, $devnull or die;	# restart cycle again
109*0Sstevel@tonic-gate    ok( defined(<F>) );
110*0Sstevel@tonic-gate    is( <F>, undef );
111*0Sstevel@tonic-gate    close F or die "Could not close: $!";
112*0Sstevel@tonic-gate}
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gate# This used to dump core
115*0Sstevel@tonic-gatefresh_perl_is( <<'**PROG**', "foobar", {}, "ARGV aliasing and eof()" );
116*0Sstevel@tonic-gateopen OUT, ">Io_argv3.tmp" or die "Can't open temp file: $!";
117*0Sstevel@tonic-gateprint OUT "foo";
118*0Sstevel@tonic-gateclose OUT;
119*0Sstevel@tonic-gateopen IN, "Io_argv3.tmp" or die "Can't open temp file: $!";
120*0Sstevel@tonic-gate*ARGV = *IN;
121*0Sstevel@tonic-gatewhile (<>) {
122*0Sstevel@tonic-gate    print;
123*0Sstevel@tonic-gate    print "bar" if eof();
124*0Sstevel@tonic-gate}
125*0Sstevel@tonic-gateclose IN;
126*0Sstevel@tonic-gateunlink "Io_argv3.tmp";
127*0Sstevel@tonic-gate**PROG**
128*0Sstevel@tonic-gate
129*0Sstevel@tonic-gateEND {
130*0Sstevel@tonic-gate    1 while unlink 'Io_argv1.tmp', 'Io_argv1.tmp_bak',
131*0Sstevel@tonic-gate	'Io_argv2.tmp', 'Io_argv2.tmp_bak', 'Io_argv3.tmp';
132*0Sstevel@tonic-gate}
133