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