This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
introduce illegal symbols into null package so that gv_fetchpv(...,TRUE)
[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' ;
f180df80 58<$fred> ;
8ebc5c01 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--
8f58c2e7
GS
154# known scalar leak
155BEGIN { $ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3; }
c7d6bfb2
GS
156use abc;
157EXPECT
158Global symbol "$f" requires explicit package name at abc.pm line 3.
159Global symbol "$k" requires explicit package name at abc.pm line 3.
160Global symbol "$g" requires explicit package name at abc.pm line 4.
161Global symbol "$l" requires explicit package name at abc.pm line 4.
162Global symbol "$c" requires explicit package name at abc.pm line 5.
163Global symbol "$h" requires explicit package name at abc.pm line 5.
164Global symbol "$m" requires explicit package name at abc.pm line 5.
165Global symbol "$d" requires explicit package name at abc.pm line 6.
166Global symbol "$i" requires explicit package name at abc.pm line 6.
167Global symbol "$n" requires explicit package name at abc.pm line 6.
168Global symbol "$e" requires explicit package name at abc.pm line 7.
169Global symbol "$j" requires explicit package name at abc.pm line 7.
170Global symbol "$o" requires explicit package name at abc.pm line 7.
171Global symbol "$p" requires explicit package name at abc.pm line 8.
172Illegal binary digit '2' at abc.pm line 8, at end of line
173abc.pm has too many errors.
8f58c2e7
GS
174Compilation failed in require at - line 3.
175BEGIN failed--compilation aborted at - line 3.
c7d6bfb2
GS
176########
177
8ebc5c01
PP
178# Check scope of pragma with eval
179no strict ;
180eval {
181 $joe = 1 ;
182};
183print STDERR $@;
184$joe = 1 ;
185EXPECT
186
187########
188
189# Check scope of pragma with eval
190no strict ;
191eval {
192 use strict 'vars' ;
193 $joe = 1 ;
194};
195print STDERR $@;
196$joe = 1 ;
197EXPECT
2c4aebbd 198Global symbol "$joe" requires explicit package name at - line 6.
8ebc5c01
PP
199Execution of - aborted due to compilation errors.
200########
201
202# Check scope of pragma with eval
203use strict 'vars' ;
204eval {
205 $joe = 1 ;
206};
207print STDERR $@;
208$joe = 1 ;
209EXPECT
2c4aebbd 210Global symbol "$joe" requires explicit package name at - line 5.
5a844595 211Global symbol "$joe" requires explicit package name at - line 8.
8ebc5c01
PP
212Execution of - aborted due to compilation errors.
213########
214
215# Check scope of pragma with eval
216use strict 'vars' ;
217eval {
218 no strict ;
219 $joe = 1 ;
220};
221print STDERR $@;
222$joe = 1 ;
223EXPECT
224Variable "$joe" is not imported at - line 9.
2c4aebbd 225Global symbol "$joe" requires explicit package name at - line 9.
8ebc5c01
PP
226Execution of - aborted due to compilation errors.
227########
228
229# Check scope of pragma with eval
230no strict ;
231eval '
232 $joe = 1 ;
233'; print STDERR $@ ;
234$joe = 1 ;
235EXPECT
236
237########
238
239# Check scope of pragma with eval
240no strict ;
241eval q[
242 use strict 'vars' ;
243 $joe = 1 ;
244]; print STDERR $@;
245EXPECT
2c4aebbd 246Global symbol "$joe" requires explicit package name at (eval 1) line 3.
8ebc5c01
PP
247########
248
249# Check scope of pragma with eval
250use strict 'vars' ;
251eval '
252 $joe = 1 ;
253'; print STDERR $@ ;
254EXPECT
2c4aebbd 255Global symbol "$joe" requires explicit package name at (eval 1) line 2.
8ebc5c01
PP
256########
257
258# Check scope of pragma with eval
259use strict 'vars' ;
260eval '
261 no strict ;
262 $joe = 1 ;
263'; print STDERR $@;
264$joe = 1 ;
265EXPECT
2c4aebbd 266Global symbol "$joe" requires explicit package name at - line 8.
8ebc5c01 267Execution of - aborted due to compilation errors.
5a844595
GS
268########
269
270# Check if multiple evals produce same errors
271use strict 'vars';
272my $ret = eval q{ print $x; };
273print $@;
274print "ok 1\n" unless defined $ret;
275$ret = eval q{ print $x; };
276print $@;
277print "ok 2\n" unless defined $ret;
278EXPECT
279Global symbol "$x" requires explicit package name at (eval 1) line 1.
280ok 1
281Global symbol "$x" requires explicit package name at (eval 2) line 1.
282ok 2
77ca0c92
LW
283########
284
285# strict vars with outer our - no error
286use strict 'vars' ;
287our $freddy;
288local $abc::joe ;
289my $fred ;
290my $b = \$fred ;
291$Fred::ABC = 1 ;
292$freddy = 2 ;
293EXPECT
294
295########
296
297# strict vars with inner our - no error
298use strict 'vars' ;
299sub foo {
300 our $fred;
301 $fred;
302}
303EXPECT
304
305########
306
307# strict vars with outer our, inner use - no error
308use strict 'vars' ;
309our $fred;
310sub foo {
311 $fred;
312}
313EXPECT
314
315########
316
317# strict vars with nested our - no error
318use strict 'vars' ;
319our $fred;
320sub foo {
321 our $fred;
322 $fred;
323}
324$fred ;
325EXPECT
326
327########
328
329# strict vars with elapsed our - error
330use strict 'vars' ;
331sub foo {
332 our $fred;
333 $fred;
334}
335$fred ;
336EXPECT
337Variable "$fred" is not imported at - line 8.
338Global symbol "$fred" requires explicit package name at - line 8.
339Execution of - aborted due to compilation errors.
340########
341
342# nested our with local - no error
343$fred = 1;
344use strict 'vars';
345{
346 local our $fred = 2;
347 print $fred,"\n";
348}
349print our $fred,"\n";
350EXPECT
3512
3521
f472eb5c
GS
353########
354
355# "nailed" our declaration visibility across package boundaries
356use strict 'vars';
357our $foo;
358$foo = 20;
359package Foo;
360print $foo, "\n";
361EXPECT
36220
363########
364
365# multiple our declarations in same scope, different packages, no warning
366use strict 'vars';
367use warnings;
368our $foo;
369${foo} = 10;
370package Foo;
371our $foo = 20;
372print $foo, "\n";
373EXPECT
37420
375########
376
377# multiple our declarations in same scope, same package, warning
378use strict 'vars';
379use warnings;
380our $foo;
381${foo} = 10;
382our $foo;
383EXPECT
384"our" variable $foo masks earlier declaration in same scope at - line 7.
33633739
GS
385########
386
387# multiple our declarations in same scope, same package, warning
388use strict 'vars';
389use warnings;
390our $foo;
391{
392 our $foo;
393 package Foo;
394 our $foo;
395}
396EXPECT
397"our" variable $foo redeclared at - line 7.
cc507455 398 (Did you mean "local" instead of "our"?)
33633739 399Name "Foo::foo" used only once: possible typo at - line 9.