This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
mention how to look up perllocal.pod (from Michael G Schwern)
[perl5.git] / t / pragma / strict-vars
CommitLineData
8ebc5c01
PP
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) ;
39bac7f7
GS
30BEGIN { *freddy = \$joe::shmoe; }
31$freddy = 2 ;
32EXPECT
33
34########
35
36# strict vars - no error
37use strict 'vars' ;
38use vars qw( $freddy) ;
8ebc5c01
PP
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
2c4aebbd 52Global symbol "$fred" requires explicit package name at - line 4.
8ebc5c01
PP
53Execution of - aborted due to compilation errors.
54########
55
56# strict vars - error
57use strict 'vars' ;
58$fred ;
59EXPECT
2c4aebbd 60Global symbol "$fred" requires explicit package name at - line 4.
8ebc5c01
PP
61Execution of - aborted due to compilation errors.
62########
63
64# strict vars - error
65use strict 'vars' ;
66local $fred ;
67EXPECT
2c4aebbd 68Global symbol "$fred" requires explicit package name at - line 4.
8ebc5c01
PP
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.
2c4aebbd 81Global symbol "$joe" requires explicit package name at - line 8.
8ebc5c01
PP
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
2c4aebbd 93Global symbol "$joe" requires explicit package name at - line 6.
8ebc5c01
PP
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.
2c4aebbd 126Global symbol "$joe" requires explicit package name at ./abc line 2.
7a2e2cd6 127Compilation failed in require at - line 2.
8ebc5c01
PP
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.
2c4aebbd 139Global symbol "$joe" requires explicit package name at abc.pm line 2.
7a2e2cd6 140Compilation failed in require at - line 2.
8ebc5c01
PP
141BEGIN failed--compilation aborted at - line 2.
142########
143
c7d6bfb2
GS
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
8ebc5c01
PP
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
2c4aebbd 196Global symbol "$joe" requires explicit package name at - line 6.
8ebc5c01
PP
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
2c4aebbd 208Global symbol "$joe" requires explicit package name at - line 5.
5a844595 209Global symbol "$joe" requires explicit package name at - line 8.
8ebc5c01
PP
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.
2c4aebbd 223Global symbol "$joe" requires explicit package name at - line 9.
8ebc5c01
PP
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
2c4aebbd 244Global symbol "$joe" requires explicit package name at (eval 1) line 3.
8ebc5c01
PP
245########
246
247# Check scope of pragma with eval
248use strict 'vars' ;
249eval '
250 $joe = 1 ;
251'; print STDERR $@ ;
252EXPECT
2c4aebbd 253Global symbol "$joe" requires explicit package name at (eval 1) line 2.
8ebc5c01
PP
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
2c4aebbd 264Global symbol "$joe" requires explicit package name at - line 8.
8ebc5c01 265Execution of - aborted due to compilation errors.
5a844595
GS
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
77ca0c92
LW
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
f472eb5c
GS
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.
33633739
GS
383########
384
385# multiple our declarations in same scope, same package, warning
386use strict 'vars';
387use warnings;
388our $foo;
389{
390 our $foo;
391 package Foo;
392 our $foo;
393}
394EXPECT
395"our" variable $foo redeclared at - line 7.
396(Did you mean "local" instead of "our"?)
397Name "Foo::foo" used only once: possible typo at - line 9.