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