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