This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regen win32/config*, up version numbers &c.
[perl5.git] / t / pragma / strict-vars
CommitLineData
8ebc5c01 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) ;
30local $abc::joe ;
31my $fred ;
32my $b = \$fred ;
33$Fred::ABC = 1 ;
34$freddy = 2 ;
35EXPECT
36
37########
38
39# strict vars - error
40use strict ;
41$fred ;
42EXPECT
2c4aebbd 43Global symbol "$fred" requires explicit package name at - line 4.
8ebc5c01 44Execution of - aborted due to compilation errors.
45########
46
47# strict vars - error
48use strict 'vars' ;
49$fred ;
50EXPECT
2c4aebbd 51Global symbol "$fred" requires explicit package name at - line 4.
8ebc5c01 52Execution of - aborted due to compilation errors.
53########
54
55# strict vars - error
56use strict 'vars' ;
57local $fred ;
58EXPECT
2c4aebbd 59Global symbol "$fred" requires explicit package name at - line 4.
8ebc5c01 60Execution of - aborted due to compilation errors.
61########
62
63# Check compile time scope of strict vars pragma
64use strict 'vars' ;
65{
66 no strict ;
67 $joe = 1 ;
68}
69$joe = 1 ;
70EXPECT
71Variable "$joe" is not imported at - line 8.
2c4aebbd 72Global symbol "$joe" requires explicit package name at - line 8.
8ebc5c01 73Execution of - aborted due to compilation errors.
74########
75
76# Check compile time scope of strict vars pragma
77no strict;
78{
79 use strict 'vars' ;
80 $joe = 1 ;
81}
82$joe = 1 ;
83EXPECT
2c4aebbd 84Global symbol "$joe" requires explicit package name at - line 6.
8ebc5c01 85Execution of - aborted due to compilation errors.
86########
87
88--FILE-- abc
89$joe = 1 ;
901;
91--FILE--
92use strict 'vars' ;
93require "./abc";
94EXPECT
95
96########
97
98--FILE-- abc
99use strict 'vars' ;
1001;
101--FILE--
102require "./abc";
103$joe = 1 ;
104EXPECT
105
106########
107
108--FILE-- abc
109use strict 'vars' ;
110$joe = 1 ;
1111;
112--FILE--
113$joe = 1 ;
114require "./abc";
115EXPECT
116Variable "$joe" is not imported at ./abc line 2.
2c4aebbd 117Global symbol "$joe" requires explicit package name at ./abc line 2.
7a2e2cd6 118Compilation failed in require at - line 2.
8ebc5c01 119########
120
121--FILE-- abc.pm
122use strict 'vars' ;
123$joe = 1 ;
1241;
125--FILE--
126$joe = 1 ;
127use abc;
128EXPECT
129Variable "$joe" is not imported at abc.pm line 2.
2c4aebbd 130Global symbol "$joe" requires explicit package name at abc.pm line 2.
7a2e2cd6 131Compilation failed in require at - line 2.
8ebc5c01 132BEGIN failed--compilation aborted at - line 2.
133########
134
135# Check scope of pragma with eval
136no strict ;
137eval {
138 $joe = 1 ;
139};
140print STDERR $@;
141$joe = 1 ;
142EXPECT
143
144########
145
146# Check scope of pragma with eval
147no strict ;
148eval {
149 use strict 'vars' ;
150 $joe = 1 ;
151};
152print STDERR $@;
153$joe = 1 ;
154EXPECT
2c4aebbd 155Global symbol "$joe" requires explicit package name at - line 6.
8ebc5c01 156Execution of - aborted due to compilation errors.
157########
158
159# Check scope of pragma with eval
160use strict 'vars' ;
161eval {
162 $joe = 1 ;
163};
164print STDERR $@;
165$joe = 1 ;
166EXPECT
2c4aebbd 167Global symbol "$joe" requires explicit package name at - line 5.
5a844595 168Global symbol "$joe" requires explicit package name at - line 8.
8ebc5c01 169Execution of - aborted due to compilation errors.
170########
171
172# Check scope of pragma with eval
173use strict 'vars' ;
174eval {
175 no strict ;
176 $joe = 1 ;
177};
178print STDERR $@;
179$joe = 1 ;
180EXPECT
181Variable "$joe" is not imported at - line 9.
2c4aebbd 182Global symbol "$joe" requires explicit package name at - line 9.
8ebc5c01 183Execution of - aborted due to compilation errors.
184########
185
186# Check scope of pragma with eval
187no strict ;
188eval '
189 $joe = 1 ;
190'; print STDERR $@ ;
191$joe = 1 ;
192EXPECT
193
194########
195
196# Check scope of pragma with eval
197no strict ;
198eval q[
199 use strict 'vars' ;
200 $joe = 1 ;
201]; print STDERR $@;
202EXPECT
2c4aebbd 203Global symbol "$joe" requires explicit package name at (eval 1) line 3.
8ebc5c01 204########
205
206# Check scope of pragma with eval
207use strict 'vars' ;
208eval '
209 $joe = 1 ;
210'; print STDERR $@ ;
211EXPECT
2c4aebbd 212Global symbol "$joe" requires explicit package name at (eval 1) line 2.
8ebc5c01 213########
214
215# Check scope of pragma with eval
216use strict 'vars' ;
217eval '
218 no strict ;
219 $joe = 1 ;
220'; print STDERR $@;
221$joe = 1 ;
222EXPECT
2c4aebbd 223Global symbol "$joe" requires explicit package name at - line 8.
8ebc5c01 224Execution of - aborted due to compilation errors.
5a844595
GS
225########
226
227# Check if multiple evals produce same errors
228use strict 'vars';
229my $ret = eval q{ print $x; };
230print $@;
231print "ok 1\n" unless defined $ret;
232$ret = eval q{ print $x; };
233print $@;
234print "ok 2\n" unless defined $ret;
235EXPECT
236Global symbol "$x" requires explicit package name at (eval 1) line 1.
237ok 1
238Global symbol "$x" requires explicit package name at (eval 2) line 1.
239ok 2
77ca0c92
LW
240########
241
242# strict vars with outer our - no error
243use strict 'vars' ;
244our $freddy;
245local $abc::joe ;
246my $fred ;
247my $b = \$fred ;
248$Fred::ABC = 1 ;
249$freddy = 2 ;
250EXPECT
251
252########
253
254# strict vars with inner our - no error
255use strict 'vars' ;
256sub foo {
257 our $fred;
258 $fred;
259}
260EXPECT
261
262########
263
264# strict vars with outer our, inner use - no error
265use strict 'vars' ;
266our $fred;
267sub foo {
268 $fred;
269}
270EXPECT
271
272########
273
274# strict vars with nested our - no error
275use strict 'vars' ;
276our $fred;
277sub foo {
278 our $fred;
279 $fred;
280}
281$fred ;
282EXPECT
283
284########
285
286# strict vars with elapsed our - error
287use strict 'vars' ;
288sub foo {
289 our $fred;
290 $fred;
291}
292$fred ;
293EXPECT
294Variable "$fred" is not imported at - line 8.
295Global symbol "$fred" requires explicit package name at - line 8.
296Execution of - aborted due to compilation errors.
297########
298
299# nested our with local - no error
300$fred = 1;
301use strict 'vars';
302{
303 local our $fred = 2;
304 print $fred,"\n";
305}
306print our $fred,"\n";
307EXPECT
3082
3091
f472eb5c
GS
310########
311
312# "nailed" our declaration visibility across package boundaries
313use strict 'vars';
314our $foo;
315$foo = 20;
316package Foo;
317print $foo, "\n";
318EXPECT
31920
320########
321
322# multiple our declarations in same scope, different packages, no warning
323use strict 'vars';
324use warnings;
325our $foo;
326${foo} = 10;
327package Foo;
328our $foo = 20;
329print $foo, "\n";
330EXPECT
33120
332########
333
334# multiple our declarations in same scope, same package, warning
335use strict 'vars';
336use warnings;
337our $foo;
338${foo} = 10;
339our $foo;
340EXPECT
341"our" variable $foo masks earlier declaration in same scope at - line 7.
33633739
GS
342########
343
344# multiple our declarations in same scope, same package, warning
345use strict 'vars';
346use warnings;
347our $foo;
348{
349 our $foo;
350 package Foo;
351 our $foo;
352}
353EXPECT
354"our" variable $foo redeclared at - line 7.
355(Did you mean "local" instead of "our"?)
356Name "Foo::foo" used only once: possible typo at - line 9.