This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #72724] explicit return doesn’t work with lvalue subs
[perl5.git] / t / op / defins.t
1 #!./perl -w
2
3 #
4 # test auto defined() test insertion
5 #
6
7 BEGIN {
8     chdir 't' if -d 't';
9     @INC = qw(. ../lib);
10     $SIG{__WARN__} = sub { $warns++; warn $_[0] };
11 }
12 require 'test.pl';
13 plan( tests => 19 );
14
15 my $unix_mode = 1;
16
17 if ($^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.';
37 $saved_filename = './0';
38
39 cmp_ok($warns,'==',0,'no warns at start');
40
41 open(FILE,">$saved_filename");
42 ok(defined(FILE),'created work file');
43 print FILE "1\n";
44 print FILE "0";
45 close(FILE);
46
47 open(FILE,"<$saved_filename");
48 ok(defined(FILE),'opened work file');
49 my $seen = 0;
50 my $dummy;
51 while (my $name = <FILE>)
52  {
53   $seen++ if $name eq '0';
54  }
55 cmp_ok($seen,'==',1,'seen in while()');
56
57 seek(FILE,0,0);
58 $seen = 0;
59 my $line = '';
60 do
61  {
62   $seen++ if $line eq '0';
63  } while ($line = <FILE>);
64 cmp_ok($seen,'==',1,'seen in do/while');
65
66 seek(FILE,0,0);
67 $seen = 0;
68 while (($seen ? $dummy : $name) = <FILE> )
69  {
70   $seen++ if $name eq '0';
71  }
72 cmp_ok($seen,'==',1,'seen in while() ternary');
73
74 seek(FILE,0,0);
75 $seen = 0;
76 my %where;
77 while ($where{$seen} = <FILE>)
78  {
79   $seen++ if $where{$seen} eq '0';
80  }
81 cmp_ok($seen,'==',1,'seen in hash while()');
82 close FILE;
83
84 opendir(DIR,'.');
85 ok(defined(DIR),'opened current directory');
86 $seen = 0;
87 while (my $name = readdir(DIR))
88  {
89   $seen++ if $name eq $wanted_filename;
90  }
91 cmp_ok($seen,'==',1,'saw work file once');
92
93 rewinddir(DIR);
94 $seen = 0;
95 $dummy = '';
96 while (($seen ? $dummy : $name) = readdir(DIR))
97  {
98   $seen++ if $name eq $wanted_filename;
99  }
100 cmp_ok($seen,'>',0,'saw file in while() ternary');
101
102 rewinddir(DIR);
103 $seen = 0;
104 while ($where{$seen} = readdir(DIR))
105  {
106   $seen++ if $where{$seen} eq $wanted_filename;
107  }
108 cmp_ok($seen,'==',1,'saw file in hash while()');
109
110 $seen = 0;
111 while (my $name = glob('*'))
112  {
113   $seen++ if $name eq $wanted_filename;
114  }
115 cmp_ok($seen,'==',1,'saw file in glob while()');
116
117 $seen = 0;
118 $dummy = '';
119 while (($seen ? $dummy : $name) = glob('*'))
120  {
121   $seen++ if $name eq $wanted_filename;
122  }
123 cmp_ok($seen,'>',0,'saw file in glob hash while() ternary');
124
125 $seen = 0;
126 while ($where{$seen} = glob('*'))
127  {
128   $seen++ if $where{$seen} eq $wanted_filename;
129  }
130 cmp_ok($seen,'==',1,'seen in glob hash while()');
131
132 unlink($saved_filename);
133 ok(!(-f $saved_filename),'work file unlinked');
134
135 my %hash = (0 => 1, 1 => 2);
136
137 $seen = 0;
138 while (my $name = each %hash)
139  {
140   $seen++ if $name eq '0';
141  }
142 cmp_ok($seen,'==',1,'seen in each');
143
144 $seen = 0;
145 $dummy = '';
146 while (($seen ? $dummy : $name) = each %hash)
147  {
148   $seen++ if $name eq '0';
149  }
150 cmp_ok($seen,'==',1,'seen in each ternary');
151
152 $seen = 0;
153 while ($where{$seen} = each %hash)
154  {
155   $seen++ if $where{$seen} eq '0';
156  }
157 cmp_ok($seen,'==',1,'seen in each hash');
158
159 cmp_ok($warns,'==',0,'no warns at finish');