This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #10021] Fixing bareword usage under strict.pm
[perl5.git] / t / lib / strict / vars
1 Check strict vars functionality
2
3 __END__
4
5 # no strict, should build & run ok.
6 Fred ;
7 my $fred ;
8 $b = "fred" ;
9 $a = $$b ;
10 EXPECT
11
12 ########
13
14 use strict qw(subs refs) ;
15 $fred ;
16 EXPECT
17
18 ########
19
20 use strict ;
21 no strict 'vars' ;
22 $fred ;
23 EXPECT
24
25 ########
26
27 # strict vars - no error
28 use strict 'vars' ;
29 use vars qw( $freddy) ;
30 BEGIN { *freddy = \$joe::shmoe; }
31 $freddy = 2 ;
32 EXPECT
33
34 ########
35
36 # strict vars - no error
37 use strict 'vars' ;
38 use vars qw( $freddy) ;
39 local $abc::joe ;
40 my $fred ;
41 my $b = \$fred ;
42 $Fred::ABC = 1 ;
43 $freddy = 2 ;
44 EXPECT
45
46 ########
47
48 # strict vars - error
49 use strict ;
50 $fred ;
51 EXPECT
52 Global symbol "$fred" requires explicit package name at - line 4.
53 Execution of - aborted due to compilation errors.
54 ########
55
56 # strict vars - error
57 use strict 'vars' ;
58 <$fred> ;
59 EXPECT
60 Global symbol "$fred" requires explicit package name at - line 4.
61 Execution of - aborted due to compilation errors.
62 ########
63
64 # strict vars - error
65 use strict 'vars' ;
66 local $fred ;
67 EXPECT
68 Global symbol "$fred" requires explicit package name at - line 4.
69 Execution of - aborted due to compilation errors.
70 ########
71
72 # Check compile time scope of strict vars pragma
73 use strict 'vars' ;
74 {
75     no strict ;
76     $joe = 1 ;
77 }
78 $joe = 1 ;
79 EXPECT
80 Variable "$joe" is not imported at - line 8.
81 Global symbol "$joe" requires explicit package name at - line 8.
82 Execution of - aborted due to compilation errors.
83 ########
84
85 # Check compile time scope of strict vars pragma
86 no strict;
87 {
88     use strict 'vars' ;
89     $joe = 1 ;
90 }
91 $joe = 1 ;
92 EXPECT
93 Global symbol "$joe" requires explicit package name at - line 6.
94 Execution of - aborted due to compilation errors.
95 ########
96
97 --FILE-- abc
98 $joe = 1 ;
99 1;
100 --FILE-- 
101 use strict 'vars' ;
102 require "./abc";
103 EXPECT
104
105 ########
106
107 --FILE-- abc
108 use strict 'vars' ;
109 1;
110 --FILE-- 
111 require "./abc";
112 $joe = 1 ;
113 EXPECT
114
115 ########
116
117 --FILE-- abc
118 use strict 'vars' ;
119 $joe = 1 ;
120 1;
121 --FILE-- 
122 $joe = 1 ;
123 require "./abc";
124 EXPECT
125 Variable "$joe" is not imported at ./abc line 2.
126 Global symbol "$joe" requires explicit package name at ./abc line 2.
127 Compilation failed in require at - line 2.
128 ########
129
130 --FILE-- abc.pm
131 use strict 'vars' ;
132 $joe = 1 ;
133 1;
134 --FILE-- 
135 $joe = 1 ;
136 use abc;
137 EXPECT
138 Variable "$joe" is not imported at abc.pm line 2.
139 Global symbol "$joe" requires explicit package name at abc.pm line 2.
140 Compilation failed in require at - line 2.
141 BEGIN failed--compilation aborted at - line 2.
142 ########
143
144 --FILE-- abc.pm
145 package Burp;
146 use strict;
147 $a = 1;$f = 1;$k = 1; # just to get beyond the limit...
148 $b = 1;$g = 1;$l = 1;
149 $c = 1;$h = 1;$m = 1;
150 $d = 1;$i = 1;$n = 1;
151 $e = 1;$j = 1;$o = 1;
152 $p = 0b12;
153 --FILE-- 
154 use abc;
155 EXPECT
156 Global symbol "$f" requires explicit package name at abc.pm line 3.
157 Global symbol "$k" requires explicit package name at abc.pm line 3.
158 Global symbol "$g" requires explicit package name at abc.pm line 4.
159 Global symbol "$l" requires explicit package name at abc.pm line 4.
160 Global symbol "$c" requires explicit package name at abc.pm line 5.
161 Global symbol "$h" requires explicit package name at abc.pm line 5.
162 Global symbol "$m" requires explicit package name at abc.pm line 5.
163 Global symbol "$d" requires explicit package name at abc.pm line 6.
164 Global symbol "$i" requires explicit package name at abc.pm line 6.
165 Global symbol "$n" requires explicit package name at abc.pm line 6.
166 Global symbol "$e" requires explicit package name at abc.pm line 7.
167 Global symbol "$j" requires explicit package name at abc.pm line 7.
168 Global symbol "$o" requires explicit package name at abc.pm line 7.
169 Global symbol "$p" requires explicit package name at abc.pm line 8.
170 Illegal binary digit '2' at abc.pm line 8, at end of line
171 abc.pm has too many errors.
172 Compilation failed in require at - line 1.
173 BEGIN failed--compilation aborted at - line 1.
174 ########
175
176 # Check scope of pragma with eval
177 no strict ;
178 eval {
179     $joe = 1 ;
180 };
181 print STDERR $@;
182 $joe = 1 ;
183 EXPECT
184
185 ########
186
187 # Check scope of pragma with eval
188 no strict ;
189 eval {
190     use strict 'vars' ;
191     $joe = 1 ;
192 };
193 print STDERR $@;
194 $joe = 1 ;
195 EXPECT
196 Global symbol "$joe" requires explicit package name at - line 6.
197 Execution of - aborted due to compilation errors.
198 ########
199
200 # Check scope of pragma with eval
201 use strict 'vars' ;
202 eval {
203     $joe = 1 ;
204 };
205 print STDERR $@;
206 $joe = 1 ;
207 EXPECT
208 Global symbol "$joe" requires explicit package name at - line 5.
209 Global symbol "$joe" requires explicit package name at - line 8.
210 Execution of - aborted due to compilation errors.
211 ########
212
213 # Check scope of pragma with eval
214 use strict 'vars' ;
215 eval {
216     no strict ;
217     $joe = 1 ;
218 };
219 print STDERR $@;
220 $joe = 1 ;
221 EXPECT
222 Variable "$joe" is not imported at - line 9.
223 Global symbol "$joe" requires explicit package name at - line 9.
224 Execution of - aborted due to compilation errors.
225 ########
226
227 # Check scope of pragma with eval
228 no strict ;
229 eval '
230     $joe = 1 ;
231 '; print STDERR $@ ;
232 $joe = 1 ;
233 EXPECT
234
235 ########
236
237 # Check scope of pragma with eval
238 no strict ;
239 eval q[ 
240     use strict 'vars' ;
241     $joe = 1 ;
242 ]; print STDERR $@;
243 EXPECT
244 Global symbol "$joe" requires explicit package name at (eval 1) line 3.
245 ########
246
247 # Check scope of pragma with eval
248 use strict 'vars' ;
249 eval '
250     $joe = 1 ;
251 '; print STDERR $@ ;
252 EXPECT
253 Global symbol "$joe" requires explicit package name at (eval 1) line 2.
254 ########
255
256 # Check scope of pragma with eval
257 use strict 'vars' ;
258 eval '
259     no strict ;
260     $joe = 1 ;
261 '; print STDERR $@;
262 $joe = 1 ;
263 EXPECT
264 Global symbol "$joe" requires explicit package name at - line 8.
265 Execution of - aborted due to compilation errors.
266 ########
267
268 # Check if multiple evals produce same errors
269 use strict 'vars';
270 my $ret = eval q{ print $x; };
271 print $@;
272 print "ok 1\n" unless defined $ret;
273 $ret = eval q{ print $x; };
274 print $@;
275 print "ok 2\n" unless defined $ret;
276 EXPECT
277 Global symbol "$x" requires explicit package name at (eval 1) line 1.
278 ok 1
279 Global symbol "$x" requires explicit package name at (eval 2) line 1.
280 ok 2
281 ########
282
283 # strict vars with outer our - no error
284 use strict 'vars' ;
285 our $freddy;
286 local $abc::joe ;
287 my $fred ;
288 my $b = \$fred ;
289 $Fred::ABC = 1 ;
290 $freddy = 2 ;
291 EXPECT
292
293 ########
294
295 # strict vars with inner our - no error
296 use strict 'vars' ;
297 sub foo {
298     our $fred;
299     $fred;
300 }
301 EXPECT
302
303 ########
304
305 # strict vars with outer our, inner use - no error
306 use strict 'vars' ;
307 our $fred;
308 sub foo {
309     $fred;
310 }
311 EXPECT
312
313 ########
314
315 # strict vars with nested our - no error
316 use strict 'vars' ;
317 our $fred;
318 sub foo {
319     our $fred;
320     $fred;
321 }
322 $fred ;
323 EXPECT
324
325 ########
326
327 # strict vars with elapsed our - error
328 use strict 'vars' ;
329 sub foo {
330     our $fred;
331     $fred;
332 }
333 $fred ;
334 EXPECT
335 Variable "$fred" is not imported at - line 8.
336 Global symbol "$fred" requires explicit package name at - line 8.
337 Execution of - aborted due to compilation errors.
338 ########
339
340 # nested our with local - no error
341 $fred = 1;
342 use strict 'vars';
343 {
344     local our $fred = 2;
345     print $fred,"\n";
346 }
347 print our $fred,"\n";
348 EXPECT
349 2
350 1
351 ########
352
353 # "nailed" our declaration visibility across package boundaries
354 use strict 'vars';
355 our $foo;
356 $foo = 20;
357 package Foo;
358 print $foo, "\n";
359 EXPECT
360 20
361 ########
362
363 # multiple our declarations in same scope, different packages, no warning
364 use strict 'vars';
365 use warnings;
366 our $foo;
367 ${foo} = 10;
368 package Foo;
369 our $foo = 20;
370 print $foo, "\n";
371 EXPECT
372 20
373 ########
374
375 # multiple our declarations in same scope, same package, warning
376 use strict 'vars';
377 use warnings;
378 our $foo;
379 ${foo} = 10;
380 our $foo;
381 EXPECT
382 "our" variable $foo masks earlier declaration in same scope at - line 7.
383 ########
384
385 # multiple our declarations in same scope, same package, warning
386 use strict 'vars';
387 use warnings;
388 { our $x = 1 }
389 { our $x = 0 }
390 our $foo;
391 {
392     our $foo;
393     package Foo;
394     our $foo;
395 }
396 EXPECT
397 "our" variable $foo redeclared at - line 9.
398         (Did you mean "local" instead of "our"?)
399 ########
400
401 --FILE-- abc
402 ok
403 --FILE-- 
404 # check if our variables are introduced correctly in readline()
405 package Foo;
406 use strict 'vars';
407 our $FH;
408 open $FH, "abc" or die "Can't open 'abc': $!";
409 print <$FH>;
410 close $FH;
411 EXPECT
412 ok
413 ########
414
415 # Make sure the strict vars failure still occurs
416 # now that the `@i should be written as \@i' failure does not occur
417 # 20000522 mjd@plover.com (MJD)
418 use strict 'vars';
419 no warnings;
420 "@i_like_crackers";
421 EXPECT
422 Global symbol "@i_like_crackers" requires explicit package name at - line 7.
423 Execution of - aborted due to compilation errors.