This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Use const repl optimisation with s///e where possible
[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';
8ae39f60 13plan( tests => 23 );
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
36$wanted_filename = $unix_mode ? '0' : '0.';
7b903762 37$saved_filename = './0';
7e35a6a8
DL
38
39cmp_ok($warns,'==',0,'no warns at start');
55d729e4 40
f912a474 41ok(open(FILE,">$saved_filename"),'created work file');
55d729e4
GS
42print FILE "1\n";
43print FILE "0";
44close(FILE);
45
95e8664e 46open(FILE,"<$saved_filename");
7e35a6a8 47ok(defined(FILE),'opened work file');
55d729e4
GS
48my $seen = 0;
49my $dummy;
50while (my $name = <FILE>)
51 {
52 $seen++ if $name eq '0';
7e35a6a8
DL
53 }
54cmp_ok($seen,'==',1,'seen in while()');
55d729e4
GS
55
56seek(FILE,0,0);
57$seen = 0;
58my $line = '';
7e35a6a8 59do
55d729e4
GS
60 {
61 $seen++ if $line eq '0';
62 } while ($line = <FILE>);
7e35a6a8 63cmp_ok($seen,'==',1,'seen in do/while');
55d729e4
GS
64
65seek(FILE,0,0);
7e35a6a8
DL
66$seen = 0;
67while (($seen ? $dummy : $name) = <FILE> )
55d729e4
GS
68 {
69 $seen++ if $name eq '0';
70 }
7e35a6a8 71cmp_ok($seen,'==',1,'seen in while() ternary');
55d729e4
GS
72
73seek(FILE,0,0);
7e35a6a8
DL
74$seen = 0;
75my %where;
55d729e4
GS
76while ($where{$seen} = <FILE>)
77 {
78 $seen++ if $where{$seen} eq '0';
79 }
7e35a6a8 80cmp_ok($seen,'==',1,'seen in hash while()');
7120fed6 81close FILE;
55d729e4 82
7b903762 83opendir(DIR,'.');
7e35a6a8 84ok(defined(DIR),'opened current directory');
55d729e4
GS
85$seen = 0;
86while (my $name = readdir(DIR))
87 {
b2b3adea 88 $seen++ if $name eq $wanted_filename;
7e35a6a8
DL
89 }
90cmp_ok($seen,'==',1,'saw work file once');
55d729e4
GS
91
92rewinddir(DIR);
7e35a6a8 93$seen = 0;
55d729e4
GS
94$dummy = '';
95while (($seen ? $dummy : $name) = readdir(DIR))
96 {
b2b3adea 97 $seen++ if $name eq $wanted_filename;
55d729e4 98 }
7e35a6a8 99cmp_ok($seen,'>',0,'saw file in while() ternary');
55d729e4
GS
100
101rewinddir(DIR);
7e35a6a8 102$seen = 0;
55d729e4
GS
103while ($where{$seen} = readdir(DIR))
104 {
b2b3adea 105 $seen++ if $where{$seen} eq $wanted_filename;
55d729e4 106 }
7e35a6a8 107cmp_ok($seen,'==',1,'saw file in hash while()');
55d729e4
GS
108
109$seen = 0;
110while (my $name = glob('*'))
111 {
b2b3adea 112 $seen++ if $name eq $wanted_filename;
7e35a6a8
DL
113 }
114cmp_ok($seen,'==',1,'saw file in glob while()');
55d729e4 115
7e35a6a8 116$seen = 0;
55d729e4
GS
117$dummy = '';
118while (($seen ? $dummy : $name) = glob('*'))
119 {
b2b3adea 120 $seen++ if $name eq $wanted_filename;
55d729e4 121 }
7e35a6a8 122cmp_ok($seen,'>',0,'saw file in glob hash while() ternary');
55d729e4 123
7e35a6a8 124$seen = 0;
55d729e4
GS
125while ($where{$seen} = glob('*'))
126 {
b2b3adea 127 $seen++ if $where{$seen} eq $wanted_filename;
55d729e4 128 }
7e35a6a8 129cmp_ok($seen,'==',1,'seen in glob hash while()');
55d729e4 130
95e8664e 131unlink($saved_filename);
7e35a6a8 132ok(!(-f $saved_filename),'work file unlinked');
55d729e4
GS
133
134my %hash = (0 => 1, 1 => 2);
8ae39f60 135my @array = 1;
55d729e4
GS
136
137$seen = 0;
138while (my $name = each %hash)
139 {
140 $seen++ if $name eq '0';
7e35a6a8
DL
141 }
142cmp_ok($seen,'==',1,'seen in each');
55d729e4 143
7e35a6a8 144$seen = 0;
55d729e4
GS
145$dummy = '';
146while (($seen ? $dummy : $name) = each %hash)
147 {
148 $seen++ if $name eq '0';
149 }
7e35a6a8 150cmp_ok($seen,'==',1,'seen in each ternary');
55d729e4 151
7e35a6a8 152$seen = 0;
55d729e4
GS
153while ($where{$seen} = each %hash)
154 {
155 $seen++ if $where{$seen} eq '0';
156 }
7e35a6a8 157cmp_ok($seen,'==',1,'seen in each hash');
55d729e4 158
8ae39f60
FC
159$seen = 0;
160undef $_;
161while (each %hash)
162 {
163 $seen++ if $_ eq '0';
164 }
165cmp_ok($seen,'==',1,'0 seen in $_ in while(each %hash)');
166
167$seen = 0;
168undef $_;
169while (each @array)
170 {
171 $seen++ if $_ eq '0';
172 }
173cmp_ok($seen,'==',1,'0 seen in $_ in while(each @array)');
174
175$seen = 0;
176undef $_;
177$_ eq '0' and $seen++ while each %hash;
178cmp_ok($seen,'==',1,'0 seen in $_ in while(each %hash) as stm mod');
179
180$seen = 0;
181undef $_;
182$_ eq '0' and $seen++ while each @array;
183cmp_ok($seen,'==',1,'0 seen in $_ in while(each @array) as stm mod');
184
7e35a6a8 185cmp_ok($warns,'==',0,'no warns at finish');