This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [perl #18927] barewords with no errors
[perl5.git] / t / lib / strict / subs
CommitLineData
8ebc5c01
PP
1Check strict subs functionality
2
3__END__
4
5# no strict, should build & run ok.
6Fred ;
7my $fred ;
8$b = "fred" ;
9$a = $$b ;
10EXPECT
11
12########
13
14use strict qw(refs vars);
15Fred ;
16EXPECT
17
18########
19
20use strict ;
21no strict 'subs' ;
22Fred ;
23EXPECT
24
25########
26
27# strict subs - error
28use strict 'subs' ;
c13f253a
JH
29my @a = (1..2);
30my $b = xyz;
31EXPECT
32Bareword "xyz" not allowed while "strict subs" in use at - line 5.
33Execution of - aborted due to compilation errors.
34########
35
36# strict subs - error
37use strict 'subs' ;
8ebc5c01
PP
38Fred ;
39EXPECT
40Bareword "Fred" not allowed while "strict subs" in use at - line 4.
41Execution of - aborted due to compilation errors.
42########
43
44# strict subs - error
7d4045d4
GS
45use strict 'subs' ;
46my @a = (A..Z);
47EXPECT
48Bareword "Z" not allowed while "strict subs" in use at - line 4.
49Bareword "A" not allowed while "strict subs" in use at - line 4.
50Execution of - aborted due to compilation errors.
51########
52
53# strict subs - error
54use strict 'subs' ;
55my $a = (B..Y);
56EXPECT
57Bareword "Y" not allowed while "strict subs" in use at - line 4.
58Bareword "B" not allowed while "strict subs" in use at - line 4.
59Execution of - aborted due to compilation errors.
60########
61
62# strict subs - error
8ebc5c01
PP
63use strict ;
64Fred ;
65EXPECT
66Bareword "Fred" not allowed while "strict subs" in use at - line 4.
67Execution of - aborted due to compilation errors.
68########
69
70# strict subs - no error
71use strict 'subs' ;
72sub Fred {}
73Fred ;
74EXPECT
75
76########
77
78# Check compile time scope of strict subs pragma
79use strict 'subs' ;
80{
81 no strict ;
82 my $a = Fred ;
83}
84my $a = Fred ;
85EXPECT
86Bareword "Fred" not allowed while "strict subs" in use at - line 8.
87Execution of - aborted due to compilation errors.
88########
89
90# Check compile time scope of strict subs pragma
91no strict;
92{
93 use strict 'subs' ;
94 my $a = Fred ;
95}
96my $a = Fred ;
97EXPECT
98Bareword "Fred" not allowed while "strict subs" in use at - line 6.
99Execution of - aborted due to compilation errors.
100########
101
102# Check compile time scope of strict vars pragma
103use strict 'vars' ;
104{
105 no strict ;
106 $joe = 1 ;
107}
108$joe = 1 ;
109EXPECT
110Variable "$joe" is not imported at - line 8.
2c4aebbd 111Global symbol "$joe" requires explicit package name at - line 8.
8ebc5c01
PP
112Execution of - aborted due to compilation errors.
113########
114
115# Check compile time scope of strict vars pragma
116no strict;
117{
118 use strict 'vars' ;
119 $joe = 1 ;
120}
121$joe = 1 ;
122EXPECT
2c4aebbd 123Global symbol "$joe" requires explicit package name at - line 6.
8ebc5c01
PP
124Execution of - aborted due to compilation errors.
125########
126
127# Check runtime scope of strict refs pragma
128use strict 'refs';
129my $fred ;
130my $b = "fred" ;
131{
132 no strict ;
133 my $a = $$b ;
134}
135my $a = $$b ;
136EXPECT
137Can't use string ("fred") as a SCALAR ref while "strict refs" in use at - line 10.
138########
139
140# Check runtime scope of strict refs pragma
141no strict ;
142my $fred ;
143my $b = "fred" ;
144{
145 use strict 'refs' ;
146 my $a = $$b ;
147}
148my $a = $$b ;
149EXPECT
150Can't use string ("fred") as a SCALAR ref while "strict refs" in use at - line 8.
151########
152
153# Check runtime scope of strict refs pragma
154no strict ;
155my $fred ;
156my $b = "fred" ;
157{
158 use strict 'refs' ;
159 $a = sub { my $c = $$b ; }
160}
161&$a ;
162EXPECT
163Can't use string ("fred") as a SCALAR ref while "strict refs" in use at - line 8.
164########
165
166use strict 'subs' ;
167my $a = Fred ;
168EXPECT
169Bareword "Fred" not allowed while "strict subs" in use at - line 3.
170Execution of - aborted due to compilation errors.
171########
172
173--FILE-- abc
174my $a = Fred ;
1751;
176--FILE--
177use strict 'subs' ;
178require "./abc";
179EXPECT
180
181########
182
183--FILE-- abc
184use strict 'subs' ;
1851;
186--FILE--
187require "./abc";
188my $a = Fred ;
189EXPECT
190
191########
192
193--FILE-- abc
194use strict 'subs' ;
195my $a = Fred ;
1961;
197--FILE--
198Fred ;
199require "./abc";
200EXPECT
201Bareword "Fred" not allowed while "strict subs" in use at ./abc line 2.
7a2e2cd6 202Compilation failed in require at - line 2.
8ebc5c01
PP
203########
204
205--FILE-- abc.pm
206use strict 'subs' ;
207my $a = Fred ;
2081;
209--FILE--
210Fred ;
211use abc;
212EXPECT
213Bareword "Fred" not allowed while "strict subs" in use at abc.pm line 2.
7a2e2cd6 214Compilation failed in require at - line 2.
8ebc5c01
PP
215BEGIN failed--compilation aborted at - line 2.
216########
217
218# Check scope of pragma with eval
219no strict ;
220eval {
221 my $a = Fred ;
222};
223print STDERR $@;
224my $a = Fred ;
225EXPECT
226
227########
228
229# Check scope of pragma with eval
230no strict ;
231eval {
232 use strict 'subs' ;
233 my $a = Fred ;
234};
235print STDERR $@;
236my $a = Fred ;
237EXPECT
238Bareword "Fred" not allowed while "strict subs" in use at - line 6.
239Execution of - aborted due to compilation errors.
240########
241
242# Check scope of pragma with eval
243use strict 'subs' ;
244eval {
245 my $a = Fred ;
246};
247print STDERR $@;
248my $a = Fred ;
249EXPECT
250Bareword "Fred" not allowed while "strict subs" in use at - line 5.
251Bareword "Fred" not allowed while "strict subs" in use at - line 8.
252Execution of - aborted due to compilation errors.
253########
254
255# Check scope of pragma with eval
256use strict 'subs' ;
257eval {
258 no strict ;
259 my $a = Fred ;
260};
261print STDERR $@;
262my $a = Fred ;
263EXPECT
264Bareword "Fred" not allowed while "strict subs" in use at - line 9.
265Execution of - aborted due to compilation errors.
266########
267
268# Check scope of pragma with eval
269no strict ;
270eval '
271 Fred ;
272'; print STDERR $@ ;
273Fred ;
274EXPECT
275
276########
277
278# Check scope of pragma with eval
279no strict ;
280eval q[
281 use strict 'subs' ;
282 Fred ;
283]; print STDERR $@;
284EXPECT
285Bareword "Fred" not allowed while "strict subs" in use at (eval 1) line 3.
286########
287
288# Check scope of pragma with eval
289use strict 'subs' ;
290eval '
291 Fred ;
292'; print STDERR $@ ;
293EXPECT
294Bareword "Fred" not allowed while "strict subs" in use at (eval 1) line 2.
295########
296
297# Check scope of pragma with eval
298use strict 'subs' ;
299eval '
300 no strict ;
301 my $a = Fred ;
302'; print STDERR $@;
303my $a = Fred ;
304EXPECT
305Bareword "Fred" not allowed while "strict subs" in use at - line 8.
306Execution of - aborted due to compilation errors.
58a40671
GS
307########
308
309# see if Foo->Bar(...) etc work under strictures
310use strict;
311package Foo; sub Bar { print "@_\n" }
312Foo->Bar('a',1);
313Bar Foo ('b',2);
314Foo->Bar(qw/c 3/);
315Bar Foo (qw/d 4/);
316Foo::->Bar('A',1);
317Bar Foo:: ('B',2);
318Foo::->Bar(qw/C 3/);
319Bar Foo:: (qw/D 4/);
320EXPECT
321Foo a 1
322Foo b 2
323Foo c 3
324Foo d 4
325Foo A 1
326Foo B 2
327Foo C 3
328Foo D 4
5cc9e5c9
RH
329########
330
331# Check that barewords on the RHS of a regex match are caught
332use strict;
333"" =~ foo;
334EXPECT
335Bareword "foo" not allowed while "strict subs" in use at - line 4.
336Execution of - aborted due to compilation errors.
2bc6235c
K
337
338########
339
340# ID 20020703.002
341use strict;
342use warnings;
343my $abc = XYZ ? 1 : 0;
344print "$abc\n";
345EXPECT
346Bareword "XYZ" not allowed while "strict subs" in use at - line 5.
347Execution of - aborted due to compilation errors.
989dfb19
K
348########
349
350# [perl #10021]
351use strict;
352use warnings;
353print "" if BAREWORD;
354EXPECT
4a06f842 355Bareword "BAREWORD" not allowed while "strict subs" in use at - line 5.
989dfb19 356Execution of - aborted due to compilation errors.
3b9d2129
AT
357########
358# Ticket: 18927
359use strict 'subs';
360print 1..1, bad;
361EXPECT
362Bareword "bad" not allowed while "strict subs" in use at - line 3.
363Execution of - aborted due to compilation errors.