This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make defined %hash on a non-lexical (also) generate a deprecated warning.
[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 $fred ;
23 my $a = ${"fred"} ;
24 EXPECT
25 Can't use string ("fred") as a SCALAR ref while "strict refs" in use at - line 5.
26 ########
27
28 # strict refs - error
29 use strict 'refs' ;
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 $b = "fred" ;
40 my $a = $$b ;
41 EXPECT
42 Can't use string ("fred") as a SCALAR ref while "strict refs" in use at - line 6.
43 ########
44
45 # strict refs - error
46 use strict 'refs' ;
47 my $b ;
48 my $a = $$b ;
49 EXPECT
50 Can't use an undefined value as a SCALAR reference at - line 5.
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 an ARRAY 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 a HASH 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 symbol reference at - line 5.
75 ########
76
77 # strict refs - error
78 use strict 'refs' ;
79 my $a = fred->[0] ;
80 EXPECT
81 Can't use bareword ("fred") as an ARRAY ref while "strict refs" in use at - line 4.
82 ########
83
84 # strict refs - error
85 use strict 'refs' ;
86 my $a = fred->{barney} ;
87 EXPECT
88 Can't use bareword ("fred") as a HASH ref while "strict refs" in use at - line 4.
89 ########
90
91 # strict refs - no error
92 use strict ;
93 no strict 'refs' ;
94 my $fred ;
95 my $b = "fred" ;
96 my $a = $$b ;
97 use strict 'refs' ;
98 EXPECT
99
100 ########
101
102 # strict refs - no error
103 use strict qw(subs vars) ;
104 my $fred ;
105 my $b = "fred" ;
106 my $a = $$b ;
107 use strict 'refs' ;
108 EXPECT
109
110 ########
111
112 # strict refs - no error
113 my $fred ;
114 my $b = "fred" ;
115 my $a = $$b ;
116 use strict 'refs' ;
117 EXPECT
118
119 ########
120
121 # strict refs - no error
122 use strict 'refs' ;
123 my $fred ;
124 my $b = \$fred ;
125 my $a = $$b ;
126 EXPECT
127
128 ########
129
130 # Check runtime scope of strict refs pragma
131 use strict 'refs';
132 my $fred ;
133 my $b = "fred" ;
134 {
135     no strict ;
136     my $a = $$b ;
137 }
138 my $a = $$b ;
139 EXPECT
140 Can't use string ("fred") as a SCALAR ref while "strict refs" in use at - line 10.
141 ########
142
143 # Check runtime scope of strict refs pragma
144 no strict ;
145 my $fred ;
146 my $b = "fred" ;
147 {
148     use strict 'refs' ;
149     my $a = $$b ;
150 }
151 my $a = $$b ;
152 EXPECT
153 Can't use string ("fred") as a SCALAR ref while "strict refs" in use at - line 8.
154 ########
155
156 # Check runtime scope of strict refs pragma
157 no strict ;
158 my $fred ;
159 my $b = "fred" ;
160 {
161     use strict 'refs' ;
162     $a = sub { my $c = $$b ; }
163 }
164 &$a ;
165 EXPECT
166 Can't use string ("fred") as a SCALAR ref while "strict refs" in use at - line 8.
167 ########
168
169
170 --FILE-- abc
171 my $a = ${"Fred"} ;
172 1;
173 --FILE-- 
174 use strict 'refs' ;
175 require "./abc";
176 EXPECT
177
178 ########
179
180 --FILE-- abc
181 use strict 'refs' ;
182 1;
183 --FILE-- 
184 require "./abc";
185 my $a = ${"Fred"} ;
186 EXPECT
187
188 ########
189
190 --FILE-- abc
191 use strict 'refs' ;
192 my $a = ${"Fred"} ;
193 1;
194 --FILE-- 
195 ${"Fred"} ;
196 require "./abc";
197 EXPECT
198 Can't use string ("Fred") as a SCALAR ref while "strict refs" in use at ./abc line 2.
199 Compilation failed in require at - line 2.
200 ########
201
202 --FILE-- abc.pm
203 use strict 'refs' ;
204 my $a = ${"Fred"} ;
205 1;
206 --FILE-- 
207 my $a = ${"Fred"} ;
208 use abc;
209 EXPECT
210 Can't use string ("Fred") as a SCALAR ref while "strict refs" in use at abc.pm line 2.
211 Compilation failed in require at - line 2.
212 BEGIN failed--compilation aborted at - line 2.
213 ########
214
215 # Check scope of pragma with eval
216 no strict ;
217 eval {
218     my $a = ${"Fred"} ;
219 };
220 print STDERR $@ ;
221 my $a = ${"Fred"} ;
222 EXPECT
223
224 ########
225
226 # Check scope of pragma with eval
227 no strict ;
228 eval {
229     use strict 'refs' ;
230     my $a = ${"Fred"} ;
231 };
232 print STDERR $@ ;
233 my $a = ${"Fred"} ;
234 EXPECT
235 Can't use string ("Fred") as a SCALAR ref while "strict refs" in use at - line 6.
236 ########
237
238 # Check scope of pragma with eval
239 use strict 'refs' ;
240 eval {
241     my $a = ${"Fred"} ;
242 };
243 print STDERR $@ ;
244 EXPECT
245 Can't use string ("Fred") as a SCALAR ref while "strict refs" in use at - line 5.
246 ########
247
248 # Check scope of pragma with eval
249 use strict 'refs' ;
250 eval {
251     no strict ;
252     my $a = ${"Fred"} ;
253 };
254 print STDERR $@ ;
255 my $a = ${"Fred"} ;
256 EXPECT
257 Can't use string ("Fred") as a SCALAR ref while "strict refs" in use at - line 9.
258 ########
259
260 # Check scope of pragma with eval
261 no strict ;
262 eval '
263     my $a = ${"Fred"} ;
264 '; print STDERR $@ ;
265 my $a = ${"Fred"} ;
266 EXPECT
267
268 ########
269
270 # Check scope of pragma with eval
271 no strict ;
272 eval q[ 
273     use strict 'refs' ;
274     my $a = ${"Fred"} ;
275 ]; print STDERR $@;
276 EXPECT
277 Can't use string ("Fred") as a SCALAR ref while "strict refs" in use at (eval 1) line 3.
278 ########
279
280 # Check scope of pragma with eval
281 use strict 'refs' ;
282 eval '
283     my $a = ${"Fred"} ;
284 '; print STDERR $@ ;
285 EXPECT
286 Can't use string ("Fred") as a SCALAR ref while "strict refs" in use at (eval 1) line 2.
287 ########
288
289 # Check scope of pragma with eval
290 use strict 'refs' ;
291 eval '
292     no strict ;
293     my $a = ${"Fred"} ;
294 '; print STDERR $@;
295 my $a = ${"Fred"} ;
296 EXPECT
297 Can't use string ("Fred") as a SCALAR ref while "strict refs" in use at - line 8.
298 ########
299 # [perl #26910] hints not propagated into (?{...})
300 use strict 'refs';
301 /(?{${"foo"}++})/;
302 EXPECT
303 Can't use string ("foo") as a SCALAR ref while "strict refs" in use at (re_eval 1) line 1.
304 ########
305 # [perl #37886] strict 'refs' doesn't apply inside defined
306 use strict 'refs';
307 my $x = "foo";
308 defined $$x;
309 EXPECT
310 Can't use string ("foo") as a SCALAR ref while "strict refs" in use at - line 4.
311 ########
312 # [perl #37886] strict 'refs' doesn't apply inside defined
313 use strict 'refs';
314 my $x = "foo";
315 defined @$x;
316 EXPECT
317 Can't use string ("foo") as an ARRAY ref while "strict refs" in use at - line 4.
318 ########
319 # [perl #37886] strict 'refs' doesn't apply inside defined
320 use strict 'refs';
321 my $x = "foo";
322 defined %$x;
323 EXPECT
324 defined(%hash) is deprecated at - line 4.
325         (Maybe you should just omit the defined()?)
326 Can't use string ("foo") as a HASH ref while "strict refs" in use at - line 4.