This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Unescaped left braces in regular expressions will be fatal in 5.30.
[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 (did you forget to declare "my $fred"?) 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 (did you forget to declare "my $fred"?) 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 (did you forget to declare "my $fred"?) 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 (did you forget to declare "my $joe"?) at - line 8.
82 Execution of - aborted due to compilation errors.
83 ########
84
85 # Check compile time scope of strict vars pragma
86 use strict 'vars' ;
87 use utf8;
88 use open qw( :utf8 :std );
89 {
90     no strict ;
91     $jòè = 1 ;
92 }
93 $jòè = 1 ;
94 EXPECT
95 Variable "$jòè" is not imported at - line 10.
96 Global symbol "$jòè" requires explicit package name (did you forget to declare "my $jòè"?) at - line 10.
97 Execution of - aborted due to compilation errors.
98 ########
99
100 # Check compile time scope of strict vars pragma
101 no strict;
102 {
103     use strict 'vars' ;
104     $joe = 1 ;
105 }
106 $joe = 1 ;
107 EXPECT
108 Global symbol "$joe" requires explicit package name (did you forget to declare "my $joe"?) at - line 6.
109 Execution of - aborted due to compilation errors.
110 ########
111
112 --FILE-- abc
113 $joe = 1 ;
114 1;
115 --FILE-- 
116 use strict 'vars' ;
117 require "./abc";
118 EXPECT
119
120 ########
121
122 --FILE-- abc
123 use strict 'vars' ;
124 1;
125 --FILE-- 
126 require "./abc";
127 $joe = 1 ;
128 EXPECT
129
130 ########
131
132 --FILE-- abc
133 use strict 'vars' ;
134 $joe = 1 ;
135 1;
136 --FILE-- 
137 $joe = 1 ;
138 require "./abc";
139 EXPECT
140 Variable "$joe" is not imported at ./abc line 2.
141 Global symbol "$joe" requires explicit package name (did you forget to declare "my $joe"?) at ./abc line 2.
142 Compilation failed in require at - line 2.
143 ########
144
145 --FILE-- abc
146 use strict 'vars' ;
147 use utf8;
148 use open qw( :utf8 :std );
149 $jòè = 1 ;
150 1;
151 --FILE-- 
152 use utf8;
153 use open qw( :utf8 :std );
154 $jòè = 1 ;
155 require "./abc";
156 EXPECT
157 Variable "$jòè" is not imported at ./abc line 4.
158 Global symbol "$jòè" requires explicit package name (did you forget to declare "my $jòè"?) at ./abc line 4.
159 Compilation failed in require at - line 4.
160 ########
161
162 --FILE-- abc.pm
163 use strict 'vars' ;
164 $joe = 1 ;
165 1;
166 --FILE-- 
167 $joe = 1 ;
168 use abc;
169 EXPECT
170 Variable "$joe" is not imported at abc.pm line 2.
171 Global symbol "$joe" requires explicit package name (did you forget to declare "my $joe"?) at abc.pm line 2.
172 Compilation failed in require at - line 2.
173 BEGIN failed--compilation aborted at - line 2.
174 ########
175
176 --FILE-- abc.pm
177 use strict 'vars' ;
178 use utf8;
179 use open qw( :utf8 :std );
180 $jòè = 1 ;
181 1;
182 --FILE-- 
183 use utf8;
184 use open qw( :utf8 :std );
185 $jòè = 1 ;
186 use abc;
187 EXPECT
188 Variable "$jòè" is not imported at abc.pm line 4.
189 Global symbol "$jòè" requires explicit package name (did you forget to declare "my $jòè"?) at abc.pm line 4.
190 Compilation failed in require at - line 4.
191 BEGIN failed--compilation aborted at - line 4.
192 ########
193
194 --FILE-- abc.pm
195 package Burp;
196 use strict;
197 $a = 1;$f = 1;$k = 1; # just to get beyond the limit...
198 $b = 1;$g = 1;$l = 1;
199 $c = 1;$h = 1;$m = 1;
200 $d = 1;$i = 1;$n = 1;
201 $e = 1;$j = 1;$o = 1;
202 $p = 0b12;
203 --FILE-- 
204 use abc;
205 EXPECT
206 Global symbol "$f" requires explicit package name (did you forget to declare "my $f"?) at abc.pm line 3.
207 Global symbol "$k" requires explicit package name (did you forget to declare "my $k"?) at abc.pm line 3.
208 Global symbol "$g" requires explicit package name (did you forget to declare "my $g"?) at abc.pm line 4.
209 Global symbol "$l" requires explicit package name (did you forget to declare "my $l"?) at abc.pm line 4.
210 Global symbol "$c" requires explicit package name (did you forget to declare "my $c"?) at abc.pm line 5.
211 Global symbol "$h" requires explicit package name (did you forget to declare "my $h"?) at abc.pm line 5.
212 Global symbol "$m" requires explicit package name (did you forget to declare "my $m"?) at abc.pm line 5.
213 Global symbol "$d" requires explicit package name (did you forget to declare "my $d"?) at abc.pm line 6.
214 Global symbol "$i" requires explicit package name (did you forget to declare "my $i"?) at abc.pm line 6.
215 Global symbol "$n" requires explicit package name (did you forget to declare "my $n"?) at abc.pm line 6.
216 Global symbol "$e" requires explicit package name (did you forget to declare "my $e"?) at abc.pm line 7.
217 Global symbol "$j" requires explicit package name (did you forget to declare "my $j"?) at abc.pm line 7.
218 Global symbol "$o" requires explicit package name (did you forget to declare "my $o"?) at abc.pm line 7.
219 Global symbol "$p" requires explicit package name (did you forget to declare "my $p"?) at abc.pm line 8.
220 Illegal binary digit '2' at abc.pm line 8, at end of line
221 abc.pm has too many errors.
222 Compilation failed in require at - line 1.
223 BEGIN failed--compilation aborted at - line 1.
224 ########
225
226 # Check scope of pragma with eval
227 no strict ;
228 eval {
229     $joe = 1 ;
230 };
231 print STDERR $@;
232 $joe = 1 ;
233 EXPECT
234
235 ########
236
237 # Check scope of pragma with eval
238 no strict ;
239 eval {
240     use strict 'vars' ;
241     $joe = 1 ;
242 };
243 print STDERR $@;
244 $joe = 1 ;
245 EXPECT
246 Global symbol "$joe" requires explicit package name (did you forget to declare "my $joe"?) at - line 6.
247 Execution of - aborted due to compilation errors.
248 ########
249
250 # Check scope of pragma with eval
251 use strict 'vars' ;
252 eval {
253     $joe = 1 ;
254 };
255 print STDERR $@;
256 $joe = 1 ;
257 EXPECT
258 Global symbol "$joe" requires explicit package name (did you forget to declare "my $joe"?) at - line 5.
259 Global symbol "$joe" requires explicit package name (did you forget to declare "my $joe"?) at - line 8.
260 Execution of - aborted due to compilation errors.
261 ########
262
263 # Check scope of pragma with eval
264 use strict 'vars' ;
265 eval {
266     no strict ;
267     $joe = 1 ;
268 };
269 print STDERR $@;
270 $joe = 1 ;
271 EXPECT
272 Variable "$joe" is not imported at - line 9.
273 Global symbol "$joe" requires explicit package name (did you forget to declare "my $joe"?) at - line 9.
274 Execution of - aborted due to compilation errors.
275 ########
276
277 # Check scope of pragma with eval
278 use strict 'vars' ;
279 use utf8;
280 use open qw( :utf8 :std );
281 eval {
282     no strict ;
283     $jòè = 1 ;
284 };
285 print STDERR $@;
286 $jòè = 1 ;
287 EXPECT
288 Variable "$jòè" is not imported at - line 11.
289 Global symbol "$jòè" requires explicit package name (did you forget to declare "my $jòè"?) at - line 11.
290 Execution of - aborted due to compilation errors.
291 ########
292
293 # Check scope of pragma with eval
294 no strict ;
295 eval '
296     $joe = 1 ;
297 '; print STDERR $@ ;
298 $joe = 1 ;
299 EXPECT
300
301 ########
302
303 # Check scope of pragma with eval
304 no strict ;
305 eval q[ 
306     use strict 'vars' ;
307     $joe = 1 ;
308 ]; print STDERR $@;
309 EXPECT
310 Global symbol "$joe" requires explicit package name (did you forget to declare "my $joe"?) at (eval 1) line 3.
311 ########
312
313 # Check scope of pragma with eval
314 use strict 'vars' ;
315 eval '
316     $joe = 1 ;
317 '; print STDERR $@ ;
318 EXPECT
319 Global symbol "$joe" requires explicit package name (did you forget to declare "my $joe"?) at (eval 1) line 2.
320 ########
321
322 # Check scope of pragma with eval
323 use strict 'vars' ;
324 eval '
325     no strict ;
326     $joe = 1 ;
327 '; print STDERR $@;
328 $joe = 1 ;
329 EXPECT
330 Global symbol "$joe" requires explicit package name (did you forget to declare "my $joe"?) at - line 8.
331 Execution of - aborted due to compilation errors.
332 ########
333
334 # Check if multiple evals produce same errors
335 use strict 'vars';
336 my $ret = eval q{ print $x; };
337 print $@;
338 print "ok 1\n" unless defined $ret;
339 $ret = eval q{ print $x; };
340 print $@;
341 print "ok 2\n" unless defined $ret;
342 EXPECT
343 Global symbol "$x" requires explicit package name (did you forget to declare "my $x"?) at (eval 1) line 1.
344 ok 1
345 Global symbol "$x" requires explicit package name (did you forget to declare "my $x"?) at (eval 2) line 1.
346 ok 2
347 ########
348
349 # strict vars with outer our - no error
350 use strict 'vars' ;
351 our $freddy;
352 local $abc::joe ;
353 my $fred ;
354 my $b = \$fred ;
355 $Fred::ABC = 1 ;
356 $freddy = 2 ;
357 EXPECT
358
359 ########
360
361 # strict vars with inner our - no error
362 use strict 'vars' ;
363 sub foo {
364     our $fred;
365     $fred;
366 }
367 EXPECT
368
369 ########
370
371 # strict vars with outer our, inner use - no error
372 use strict 'vars' ;
373 our $fred;
374 sub foo {
375     $fred;
376 }
377 EXPECT
378
379 ########
380
381 # strict vars with nested our - no error
382 use strict 'vars' ;
383 our $fred;
384 sub foo {
385     our $fred;
386     $fred;
387 }
388 $fred ;
389 EXPECT
390
391 ########
392
393 # strict vars with elapsed our - error
394 use strict 'vars' ;
395 sub foo {
396     our $fred;
397     $fred;
398 }
399 $fred ;
400 EXPECT
401 Variable "$fred" is not imported at - line 8.
402 Global symbol "$fred" requires explicit package name (did you forget to declare "my $fred"?) at - line 8.
403 Execution of - aborted due to compilation errors.
404 ########
405
406 # strict vars with elapsed our - error
407 use strict 'vars' ;
408 use utf8;
409 use open qw( :utf8 :std );
410 sub fòò {
411     our $frèd;
412     $frèd;
413 }
414 $frèd ;
415 EXPECT
416 Variable "$frèd" is not imported at - line 10.
417 Global symbol "$frèd" requires explicit package name (did you forget to declare "my $frèd"?) at - line 10.
418 Execution of - aborted due to compilation errors.
419 ########
420
421 # nested our with local - no error
422 $fred = 1;
423 use strict 'vars';
424 {
425     local our $fred = 2;
426     print $fred,"\n";
427 }
428 print our $fred,"\n";
429 EXPECT
430 2
431 1
432 ########
433
434 # "nailed" our declaration visibility across package boundaries
435 use strict 'vars';
436 our $foo;
437 $foo = 20;
438 package Foo;
439 print $foo, "\n";
440 EXPECT
441 20
442 ########
443
444 # multiple our declarations in same scope, different packages, no warning
445 use strict 'vars';
446 use warnings;
447 our $foo;
448 ${foo} = 10;
449 package Foo;
450 our $foo = 20;
451 print $foo, "\n";
452 EXPECT
453 20
454 ########
455
456 # multiple our declarations in same scope, same package, warning
457 use strict 'vars';
458 use warnings;
459 our $foo;
460 ${foo} = 10;
461 our $foo;
462 EXPECT
463 "our" variable $foo redeclared at - line 7.
464 ########
465
466 # multiple our declarations in same scope, same package, warning
467 use strict 'vars';
468 use warnings;
469 { our $x = 1 }
470 { our $x = 0 }
471 our $foo;
472 {
473     our $foo;
474     our $foo;
475     package Foo;
476     our $foo;
477 }
478 EXPECT
479 "our" variable $foo redeclared at - line 9.
480         (Did you mean "local" instead of "our"?)
481 "our" variable $foo redeclared at - line 10.
482 ########
483
484 --FILE-- abc
485 ok
486 --FILE-- 
487 # check if our variables are introduced correctly in readline()
488 package Foo;
489 use strict 'vars';
490 our $FH;
491 open $FH, "abc" or die "Can't open 'abc': $!";
492 print <$FH>;
493 close $FH;
494 EXPECT
495 ok
496 ########
497
498 # Make sure the strict vars failure still occurs
499 # now that the '@i should be written as \@i' failure does not occur
500 # 20000522 mjd@plover.com (MJD)
501 use strict 'vars';
502 no warnings;
503 "@i_like_crackers";
504 EXPECT
505 Global symbol "@i_like_crackers" requires explicit package name (did you forget to declare "my @i_like_crackers"?) at - line 7.
506 Execution of - aborted due to compilation errors.
507 ########
508
509 # [perl #21914] New bug > 5.8.0. Used to dump core.
510 use strict 'vars';
511 @k = <$k>;
512 EXPECT
513 Global symbol "@k" requires explicit package name (did you forget to declare "my @k"?) at - line 4.
514 Global symbol "$k" requires explicit package name (did you forget to declare "my $k"?) at - line 4.
515 Execution of - aborted due to compilation errors.
516 ########
517 # [perl #26910] hints not propagated into (?{...})
518 use strict 'vars';
519 qr/(?{$foo++})/;
520 EXPECT
521 Global symbol "$foo" requires explicit package name (did you forget to declare "my $foo"?) at - line 3.
522 Execution of - aborted due to compilation errors.
523 ########
524 # Regex compilation errors weren't UTF-8 clean.
525 use strict 'vars';
526 use utf8;
527 use open qw( :utf8 :std );
528 qr/(?{$fòò++})/;
529 EXPECT
530 Global symbol "$fòò" requires explicit package name (did you forget to declare "my $fòò"?) at - line 5.
531 Execution of - aborted due to compilation errors.
532 ########
533 # [perl #73712] 'Variable is not imported' should be suppressible
534 $dweck;
535 use strict 'vars';
536 no warnings;
537 eval q/$dweck/;
538 EXPECT
539 ########
540 # [perl #112316] strict vars getting confused by nulls
541 # Assigning to a package whose name contains a null
542 BEGIN { *Foo:: = *{"foo\0bar::"} }
543 package foo;
544 *Foo::bar = [];
545 use strict;
546 eval 'package Foo; @bar = 1' or die;
547 EXPECT
548 ########
549 # [perl #112316] strict vars getting confused by nulls
550 # Assigning from within a package whose name contains a null
551 BEGIN { *Foo:: = *{"foo\0bar::"} }
552 package Foo;
553 *foo::bar = [];
554 use strict;
555 eval 'package foo; @bar = 1' or die;
556 EXPECT
557 ########
558 # [perl #112316] strict vars getting confused by nulls
559 # Assigning from one null package to another, with a common prefix
560 BEGIN { *Foo:: = *{"foo\0foo::"};
561         *Bar:: = *{"foo\0bar::"} }
562 package Foo;
563 *Bar::bar = [];
564 use strict;
565 eval 'package Bar; @bar = 1' or die;
566 EXPECT
567 ########
568 # UTF8 and Latin1 package names equivalent at the byte level
569 use utf8;
570 # ĵ in UTF-8 is the same as Äµ in Latin-1
571 package ĵ;
572 *ĵ::bar = [];
573 use strict;
574 eval 'package Äµ; @bar = 1' or die;
575 EXPECT