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