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