This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
force i_ieeefp=undef on dos_djgpp (it reportedly causes failures
[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
144# Check scope of pragma with eval
145no strict ;
146eval {
147 $joe = 1 ;
148};
149print STDERR $@;
150$joe = 1 ;
151EXPECT
152
153########
154
155# Check scope of pragma with eval
156no strict ;
157eval {
158 use strict 'vars' ;
159 $joe = 1 ;
160};
161print STDERR $@;
162$joe = 1 ;
163EXPECT
2c4aebbd 164Global symbol "$joe" requires explicit package name at - line 6.
8ebc5c01
PP
165Execution of - aborted due to compilation errors.
166########
167
168# Check scope of pragma with eval
169use strict 'vars' ;
170eval {
171 $joe = 1 ;
172};
173print STDERR $@;
174$joe = 1 ;
175EXPECT
2c4aebbd 176Global symbol "$joe" requires explicit package name at - line 5.
5a844595 177Global symbol "$joe" requires explicit package name at - line 8.
8ebc5c01
PP
178Execution of - aborted due to compilation errors.
179########
180
181# Check scope of pragma with eval
182use strict 'vars' ;
183eval {
184 no strict ;
185 $joe = 1 ;
186};
187print STDERR $@;
188$joe = 1 ;
189EXPECT
190Variable "$joe" is not imported at - line 9.
2c4aebbd 191Global symbol "$joe" requires explicit package name at - line 9.
8ebc5c01
PP
192Execution of - aborted due to compilation errors.
193########
194
195# Check scope of pragma with eval
196no strict ;
197eval '
198 $joe = 1 ;
199'; print STDERR $@ ;
200$joe = 1 ;
201EXPECT
202
203########
204
205# Check scope of pragma with eval
206no strict ;
207eval q[
208 use strict 'vars' ;
209 $joe = 1 ;
210]; print STDERR $@;
211EXPECT
2c4aebbd 212Global symbol "$joe" requires explicit package name at (eval 1) line 3.
8ebc5c01
PP
213########
214
215# Check scope of pragma with eval
216use strict 'vars' ;
217eval '
218 $joe = 1 ;
219'; print STDERR $@ ;
220EXPECT
2c4aebbd 221Global symbol "$joe" requires explicit package name at (eval 1) line 2.
8ebc5c01
PP
222########
223
224# Check scope of pragma with eval
225use strict 'vars' ;
226eval '
227 no strict ;
228 $joe = 1 ;
229'; print STDERR $@;
230$joe = 1 ;
231EXPECT
2c4aebbd 232Global symbol "$joe" requires explicit package name at - line 8.
8ebc5c01 233Execution of - aborted due to compilation errors.
5a844595
GS
234########
235
236# Check if multiple evals produce same errors
237use strict 'vars';
238my $ret = eval q{ print $x; };
239print $@;
240print "ok 1\n" unless defined $ret;
241$ret = eval q{ print $x; };
242print $@;
243print "ok 2\n" unless defined $ret;
244EXPECT
245Global symbol "$x" requires explicit package name at (eval 1) line 1.
246ok 1
247Global symbol "$x" requires explicit package name at (eval 2) line 1.
248ok 2
77ca0c92
LW
249########
250
251# strict vars with outer our - no error
252use strict 'vars' ;
253our $freddy;
254local $abc::joe ;
255my $fred ;
256my $b = \$fred ;
257$Fred::ABC = 1 ;
258$freddy = 2 ;
259EXPECT
260
261########
262
263# strict vars with inner our - no error
264use strict 'vars' ;
265sub foo {
266 our $fred;
267 $fred;
268}
269EXPECT
270
271########
272
273# strict vars with outer our, inner use - no error
274use strict 'vars' ;
275our $fred;
276sub foo {
277 $fred;
278}
279EXPECT
280
281########
282
283# strict vars with nested our - no error
284use strict 'vars' ;
285our $fred;
286sub foo {
287 our $fred;
288 $fred;
289}
290$fred ;
291EXPECT
292
293########
294
295# strict vars with elapsed our - error
296use strict 'vars' ;
297sub foo {
298 our $fred;
299 $fred;
300}
301$fred ;
302EXPECT
303Variable "$fred" is not imported at - line 8.
304Global symbol "$fred" requires explicit package name at - line 8.
305Execution of - aborted due to compilation errors.
306########
307
308# nested our with local - no error
309$fred = 1;
310use strict 'vars';
311{
312 local our $fred = 2;
313 print $fred,"\n";
314}
315print our $fred,"\n";
316EXPECT
3172
3181
f472eb5c
GS
319########
320
321# "nailed" our declaration visibility across package boundaries
322use strict 'vars';
323our $foo;
324$foo = 20;
325package Foo;
326print $foo, "\n";
327EXPECT
32820
329########
330
331# multiple our declarations in same scope, different packages, no warning
332use strict 'vars';
333use warnings;
334our $foo;
335${foo} = 10;
336package Foo;
337our $foo = 20;
338print $foo, "\n";
339EXPECT
34020
341########
342
343# multiple our declarations in same scope, same package, warning
344use strict 'vars';
345use warnings;
346our $foo;
347${foo} = 10;
348our $foo;
349EXPECT
350"our" variable $foo masks earlier declaration in same scope at - line 7.
33633739
GS
351########
352
353# multiple our declarations in same scope, same package, warning
354use strict 'vars';
355use warnings;
356our $foo;
357{
358 our $foo;
359 package Foo;
360 our $foo;
361}
362EXPECT
363"our" variable $foo redeclared at - line 7.
364(Did you mean "local" instead of "our"?)
365Name "Foo::foo" used only once: possible typo at - line 9.