This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #112316] Make strict vars respect null-to-null assignment
[perl5.git] / t / lib / strict / refs
1 Check strict refs functionality
2
3 __END__
4
5 # no strict, should build & run ok.
6 my $fred ;
7 $b = "fred" ;
8 $a = $$b ;
9 $c = ${"def"} ;
10 $c = @{"def"} ;
11 $c = %{"def"} ;
12 $c = *{"def"} ;
13 $c = \&{"def"} ;
14 $c = def->[0];
15 $c = def->{xyz};
16 EXPECT
17
18 ########
19
20 # strict refs - error
21 use strict ;
22 my $str="A::Really::Big::Package::Name::To::Use"; 
23 $str->{foo}= 1;
24 EXPECT
25 Can't use string ("A::Really::Big::Package::Name::T"...) as a HASH ref while "strict refs" in use at - line 5.
26 ########
27
28 # strict refs - error
29 use strict ;
30 my $fred ;
31 my $a = ${"fred"} ;
32 EXPECT
33 Can't use string ("fred") as a SCALAR ref while "strict refs" in use at - line 5.
34 ########
35
36 # strict refs - error
37 use strict 'refs' ;
38 my $fred ;
39 my $a = ${"fred"} ;
40 EXPECT
41 Can't use string ("fred") as a SCALAR ref while "strict refs" in use at - line 5.
42 ########
43
44 # strict refs - error
45 use strict 'refs' ;
46 my $fred ;
47 my $b = "fred" ;
48 my $a = $$b ;
49 EXPECT
50 Can't use string ("fred") as a SCALAR ref while "strict refs" in use at - line 6.
51 ########
52
53 # strict refs - error
54 use strict 'refs' ;
55 my $b ;
56 my $a = $$b ;
57 EXPECT
58 Can't use an undefined value as a SCALAR reference at - line 5.
59 ########
60
61 # strict refs - error
62 use strict 'refs' ;
63 my $b ;
64 my $a = @$b ;
65 EXPECT
66 Can't use an undefined value as an ARRAY reference at - line 5.
67 ########
68
69 # strict refs - error
70 use strict 'refs' ;
71 my $b ;
72 my $a = %$b ;
73 EXPECT
74 Can't use an undefined value as a HASH reference at - line 5.
75 ########
76
77 # strict refs - error
78 use strict 'refs' ;
79 my $b ;
80 my $a = *$b ;
81 EXPECT
82 Can't use an undefined value as a symbol reference at - line 5.
83 ########
84
85 # strict refs - error
86 use strict 'refs' ;
87 my $a = fred->[0] ;
88 EXPECT
89 Can't use bareword ("fred") as an ARRAY ref while "strict refs" in use at - line 4.
90 ########
91
92 # strict refs - error
93 use strict 'refs' ;
94 my $a = fred->{barney} ;
95 EXPECT
96 Can't use bareword ("fred") as a HASH ref while "strict refs" in use at - line 4.
97 ########
98
99 # strict refs - no error
100 use strict ;
101 no strict 'refs' ;
102 my $fred ;
103 my $b = "fred" ;
104 my $a = $$b ;
105 use strict 'refs' ;
106 EXPECT
107
108 ########
109
110 # strict refs - no error
111 use strict qw(subs vars) ;
112 my $fred ;
113 my $b = "fred" ;
114 my $a = $$b ;
115 use strict 'refs' ;
116 EXPECT
117
118 ########
119
120 # strict refs - no error
121 my $fred ;
122 my $b = "fred" ;
123 my $a = $$b ;
124 use strict 'refs' ;
125 EXPECT
126
127 ########
128
129 # strict refs - no error
130 use strict 'refs' ;
131 my $fred ;
132 my $b = \$fred ;
133 my $a = $$b ;
134 EXPECT
135
136 ########
137
138 # Check runtime scope of strict refs pragma
139 use strict 'refs';
140 my $fred ;
141 my $b = "fred" ;
142 {
143     no strict ;
144     my $a = $$b ;
145 }
146 my $a = $$b ;
147 EXPECT
148 Can't use string ("fred") as a SCALAR ref while "strict refs" in use at - line 10.
149 ########
150
151 # Check runtime scope of strict refs pragma
152 no strict ;
153 my $fred ;
154 my $b = "fred" ;
155 {
156     use strict 'refs' ;
157     my $a = $$b ;
158 }
159 my $a = $$b ;
160 EXPECT
161 Can't use string ("fred") as a SCALAR ref while "strict refs" in use at - line 8.
162 ########
163
164 # Check runtime scope of strict refs pragma
165 no strict ;
166 my $fred ;
167 my $b = "fred" ;
168 {
169     use strict 'refs' ;
170     $a = sub { my $c = $$b ; }
171 }
172 &$a ;
173 EXPECT
174 Can't use string ("fred") as a SCALAR ref while "strict refs" in use at - line 8.
175 ########
176
177
178 --FILE-- abc
179 my $a = ${"Fred"} ;
180 1;
181 --FILE-- 
182 use strict 'refs' ;
183 require "./abc";
184 EXPECT
185
186 ########
187
188 --FILE-- abc
189 use strict 'refs' ;
190 1;
191 --FILE-- 
192 require "./abc";
193 my $a = ${"Fred"} ;
194 EXPECT
195
196 ########
197
198 --FILE-- abc
199 use strict 'refs' ;
200 my $a = ${"Fred"} ;
201 1;
202 --FILE-- 
203 ${"Fred"} ;
204 require "./abc";
205 EXPECT
206 Can't use string ("Fred") as a SCALAR ref while "strict refs" in use at ./abc line 2.
207 Compilation failed in require at - line 2.
208 ########
209
210 --FILE-- abc.pm
211 use strict 'refs' ;
212 my $a = ${"Fred"} ;
213 1;
214 --FILE-- 
215 my $a = ${"Fred"} ;
216 use abc;
217 EXPECT
218 Can't use string ("Fred") as a SCALAR ref while "strict refs" in use at abc.pm line 2.
219 Compilation failed in require at - line 2.
220 BEGIN failed--compilation aborted at - line 2.
221 ########
222
223 # Check scope of pragma with eval
224 no strict ;
225 eval {
226     my $a = ${"Fred"} ;
227 };
228 print STDERR $@ ;
229 my $a = ${"Fred"} ;
230 EXPECT
231
232 ########
233
234 # Check scope of pragma with eval
235 no strict ;
236 eval {
237     use strict 'refs' ;
238     my $a = ${"Fred"} ;
239 };
240 print STDERR $@ ;
241 my $a = ${"Fred"} ;
242 EXPECT
243 Can't use string ("Fred") as a SCALAR ref while "strict refs" in use at - line 6.
244 ########
245
246 # Check scope of pragma with eval
247 use strict 'refs' ;
248 eval {
249     my $a = ${"Fred"} ;
250 };
251 print STDERR $@ ;
252 EXPECT
253 Can't use string ("Fred") as a SCALAR ref while "strict refs" in use at - line 5.
254 ########
255
256 # Check scope of pragma with eval
257 use strict 'refs' ;
258 eval {
259     no strict ;
260     my $a = ${"Fred"} ;
261 };
262 print STDERR $@ ;
263 my $a = ${"Fred"} ;
264 EXPECT
265 Can't use string ("Fred") as a SCALAR ref while "strict refs" in use at - line 9.
266 ########
267
268 # Check scope of pragma with eval
269 no strict ;
270 eval '
271     my $a = ${"Fred"} ;
272 '; print STDERR $@ ;
273 my $a = ${"Fred"} ;
274 EXPECT
275
276 ########
277
278 # Check scope of pragma with eval
279 no strict ;
280 eval q[ 
281     use strict 'refs' ;
282     my $a = ${"Fred"} ;
283 ]; print STDERR $@;
284 EXPECT
285 Can't use string ("Fred") as a SCALAR ref while "strict refs" in use at (eval 1) line 3.
286 ########
287
288 # Check scope of pragma with eval
289 use strict 'refs' ;
290 eval '
291     my $a = ${"Fred"} ;
292 '; print STDERR $@ ;
293 EXPECT
294 Can't use string ("Fred") as a SCALAR ref while "strict refs" in use at (eval 1) line 2.
295 ########
296
297 # Check scope of pragma with eval
298 use strict 'refs' ;
299 eval '
300     no strict ;
301     my $a = ${"Fred"} ;
302 '; print STDERR $@;
303 my $a = ${"Fred"} ;
304 EXPECT
305 Can't use string ("Fred") as a SCALAR ref while "strict refs" in use at - line 8.
306 ########
307 # [perl #26910] hints not propagated into (?{...})
308 use strict 'refs';
309 /(?{${"foo"}++})/;
310 EXPECT
311 Can't use string ("foo") as a SCALAR ref while "strict refs" in use at (re_eval 1) line 1.
312 ########
313 # [perl #37886] strict 'refs' doesn't apply inside defined
314 use strict 'refs';
315 my $x = "foo";
316 defined $$x;
317 EXPECT
318 Can't use string ("foo") as a SCALAR ref while "strict refs" in use at - line 4.
319 ########
320 # [perl #37886] strict 'refs' doesn't apply inside defined
321 use strict 'refs';
322 my $x = "foo";
323 defined @$x;
324 EXPECT
325 defined(@array) is deprecated at - line 4.
326         (Maybe you should just omit the defined()?)
327 Can't use string ("foo") as an ARRAY ref while "strict refs" in use at - line 4.
328 ########
329 # [perl #37886] strict 'refs' doesn't apply inside defined
330 use strict 'refs';
331 my $x = "foo";
332 defined %$x;
333 EXPECT
334 defined(%hash) is deprecated at - line 4.
335         (Maybe you should just omit the defined()?)
336 Can't use string ("foo") as a HASH ref while "strict refs" in use at - line 4.
337 ########
338 # [perl #74168] Assertion failed: (SvTYPE(_svcur) >= SVt_PV), function Perl_softref2xv, file pp.c, line 240.
339 use strict 'refs';
340 my $o = 1 ; $o->{1} ;
341 EXPECT
342 Can't use string ("1") as a HASH ref while "strict refs" in use at - line 3.
343 ########
344 # pp_hot.c [pp_entersub]
345 use strict 'refs';
346 use utf8;
347 use open qw( :utf8 :std );
348 &{"F"};
349 EXPECT
350 Can't use string ("F") as a subroutine ref while "strict refs" in use at - line 5.