This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
defins.t: Suppress uninit warning
[perl5.git] / t / op / defins.t
CommitLineData
55d729e4
GS
1#!./perl -w
2
3#
4# test auto defined() test insertion
5#
6
7BEGIN {
8 chdir 't' if -d 't';
7e35a6a8 9 @INC = qw(. ../lib);
55d729e4 10 $SIG{__WARN__} = sub { $warns++; warn $_[0] };
55d729e4 11}
7e35a6a8 12require 'test.pl';
6a6a13d6 13plan( tests => 26 );
b2b3adea 14
6d74d930
JM
15my $unix_mode = 1;
16
17if ($^O eq 'VMS') {
18 # We have to know if VMS is in UNIX mode. In UNIX mode, trailing dots
19 # should not be present. There are actually two settings that control this.
20
21 $unix_mode = 0;
22 my $unix_rpt = 0;
23 my $drop_dot = 0;
24 if (eval 'require VMS::Feature') {
25 $unix_rpt = VMS::Feature::current('filename_unix_report');
26 $drop_dot = VMS::Feature::current('readdir_dropdotnotype');
27 } else {
28 my $unix_report = $ENV{'DECC$FILENAME_UNIX_REPORT'} || '';
29 $unix_rpt = $unix_report =~ /^[ET1]/i;
30 my $drop_dot_notype = $ENV{'DECC$READDIR_DROPDOTNOTYPE'} || '';
31 $drop_dot = $drop_dot_notype =~ /^[ET1]/i;
32 }
33 $unix_mode = 1 if $drop_dot && unix_rpt;
34}
35
6a6a13d6
BG
36# $wanted_filename should be 0 for readdir() and glob() tests.
37# This is because it is the only valid filename that is false in a boolean test.
38
39# $filename = '0';
40# print "hi\n" if $filename; # doesn't print
41
42# In the case of VMS, '0' isn't always the filename that you get.
43# Which makes those particular tests pointless.
44
6d74d930 45$wanted_filename = $unix_mode ? '0' : '0.';
7b903762 46$saved_filename = './0';
7e35a6a8
DL
47
48cmp_ok($warns,'==',0,'no warns at start');
55d729e4 49
f912a474 50ok(open(FILE,">$saved_filename"),'created work file');
55d729e4
GS
51print FILE "1\n";
52print FILE "0";
53close(FILE);
54
95e8664e 55open(FILE,"<$saved_filename");
7e35a6a8 56ok(defined(FILE),'opened work file');
55d729e4
GS
57my $seen = 0;
58my $dummy;
59while (my $name = <FILE>)
60 {
61 $seen++ if $name eq '0';
7e35a6a8
DL
62 }
63cmp_ok($seen,'==',1,'seen in while()');
55d729e4
GS
64
65seek(FILE,0,0);
66$seen = 0;
67my $line = '';
7e35a6a8 68do
55d729e4
GS
69 {
70 $seen++ if $line eq '0';
71 } while ($line = <FILE>);
7e35a6a8 72cmp_ok($seen,'==',1,'seen in do/while');
55d729e4
GS
73
74seek(FILE,0,0);
7e35a6a8
DL
75$seen = 0;
76while (($seen ? $dummy : $name) = <FILE> )
55d729e4
GS
77 {
78 $seen++ if $name eq '0';
79 }
7e35a6a8 80cmp_ok($seen,'==',1,'seen in while() ternary');
55d729e4
GS
81
82seek(FILE,0,0);
7e35a6a8
DL
83$seen = 0;
84my %where;
55d729e4
GS
85while ($where{$seen} = <FILE>)
86 {
87 $seen++ if $where{$seen} eq '0';
88 }
7e35a6a8 89cmp_ok($seen,'==',1,'seen in hash while()');
7120fed6 90close FILE;
55d729e4 91
7b903762 92opendir(DIR,'.');
7e35a6a8 93ok(defined(DIR),'opened current directory');
55d729e4
GS
94$seen = 0;
95while (my $name = readdir(DIR))
96 {
b2b3adea 97 $seen++ if $name eq $wanted_filename;
7e35a6a8
DL
98 }
99cmp_ok($seen,'==',1,'saw work file once');
55d729e4
GS
100
101rewinddir(DIR);
7e35a6a8 102$seen = 0;
55d729e4
GS
103$dummy = '';
104while (($seen ? $dummy : $name) = readdir(DIR))
105 {
b2b3adea 106 $seen++ if $name eq $wanted_filename;
55d729e4 107 }
7e35a6a8 108cmp_ok($seen,'>',0,'saw file in while() ternary');
55d729e4
GS
109
110rewinddir(DIR);
7e35a6a8 111$seen = 0;
55d729e4
GS
112while ($where{$seen} = readdir(DIR))
113 {
b2b3adea 114 $seen++ if $where{$seen} eq $wanted_filename;
55d729e4 115 }
7e35a6a8 116cmp_ok($seen,'==',1,'saw file in hash while()');
55d729e4 117
6a6a13d6
BG
118rewinddir(DIR);
119$seen = 0;
120$_ = 'not 0';
121while (readdir(DIR))
122 {
123 $seen++ if $_ eq $wanted_filename;
124 }
125cmp_ok($seen,'==',1,'saw file in bare while(readdir){...}');
126
127rewinddir(DIR);
128$seen = 0;
129$_ = 'not 0';
130
131$_ eq $wanted_filename && $seen++ while readdir(DIR);
132cmp_ok($seen,'==',1,'saw file in bare "... while readdir"');
133
134rewinddir(DIR);
135$seen = 0;
b29368b8 136$_ = ""; # suppress uninit warning
6a6a13d6
BG
137do
138 {
139 $seen++ if $_ eq $wanted_filename;
140 } while (readdir(DIR));
141cmp_ok($seen,'==',1,'saw file in bare do{...}while(readdir)');
142
55d729e4
GS
143$seen = 0;
144while (my $name = glob('*'))
145 {
b2b3adea 146 $seen++ if $name eq $wanted_filename;
7e35a6a8
DL
147 }
148cmp_ok($seen,'==',1,'saw file in glob while()');
55d729e4 149
7e35a6a8 150$seen = 0;
55d729e4
GS
151$dummy = '';
152while (($seen ? $dummy : $name) = glob('*'))
153 {
b2b3adea 154 $seen++ if $name eq $wanted_filename;
55d729e4 155 }
7e35a6a8 156cmp_ok($seen,'>',0,'saw file in glob hash while() ternary');
55d729e4 157
7e35a6a8 158$seen = 0;
55d729e4
GS
159while ($where{$seen} = glob('*'))
160 {
b2b3adea 161 $seen++ if $where{$seen} eq $wanted_filename;
55d729e4 162 }
7e35a6a8 163cmp_ok($seen,'==',1,'seen in glob hash while()');
55d729e4 164
95e8664e 165unlink($saved_filename);
7e35a6a8 166ok(!(-f $saved_filename),'work file unlinked');
55d729e4
GS
167
168my %hash = (0 => 1, 1 => 2);
8ae39f60 169my @array = 1;
55d729e4
GS
170
171$seen = 0;
172while (my $name = each %hash)
173 {
174 $seen++ if $name eq '0';
7e35a6a8
DL
175 }
176cmp_ok($seen,'==',1,'seen in each');
55d729e4 177
7e35a6a8 178$seen = 0;
55d729e4
GS
179$dummy = '';
180while (($seen ? $dummy : $name) = each %hash)
181 {
182 $seen++ if $name eq '0';
183 }
7e35a6a8 184cmp_ok($seen,'==',1,'seen in each ternary');
55d729e4 185
7e35a6a8 186$seen = 0;
55d729e4
GS
187while ($where{$seen} = each %hash)
188 {
189 $seen++ if $where{$seen} eq '0';
190 }
7e35a6a8 191cmp_ok($seen,'==',1,'seen in each hash');
55d729e4 192
8ae39f60
FC
193$seen = 0;
194undef $_;
195while (each %hash)
196 {
197 $seen++ if $_ eq '0';
198 }
199cmp_ok($seen,'==',1,'0 seen in $_ in while(each %hash)');
200
201$seen = 0;
202undef $_;
203while (each @array)
204 {
205 $seen++ if $_ eq '0';
206 }
207cmp_ok($seen,'==',1,'0 seen in $_ in while(each @array)');
208
209$seen = 0;
210undef $_;
211$_ eq '0' and $seen++ while each %hash;
212cmp_ok($seen,'==',1,'0 seen in $_ in while(each %hash) as stm mod');
213
214$seen = 0;
215undef $_;
216$_ eq '0' and $seen++ while each @array;
217cmp_ok($seen,'==',1,'0 seen in $_ in while(each @array) as stm mod');
218
7e35a6a8 219cmp_ok($warns,'==',0,'no warns at finish');