This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Use of strings with code points over 0xFF as arguments to "vec"
[perl5.git] / t / lib / strict / subs
... / ...
CommitLineData
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' ;
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' ;
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
45use strict 'subs' ;
46my @a = (A..Z);
47EXPECT
48Bareword "A" not allowed while "strict subs" in use at - line 4.
49Bareword "Z" 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 "B" not allowed while "strict subs" in use at - line 4.
58Bareword "Y" not allowed while "strict subs" in use at - line 4.
59Execution of - aborted due to compilation errors.
60########
61
62# strict subs - error
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.
111Global symbol "$joe" requires explicit package name (did you forget to declare "my $joe"?) at - line 8.
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
123Global symbol "$joe" requires explicit package name (did you forget to declare "my $joe"?) at - line 6.
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.
202Compilation failed in require at - line 2.
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.
214Compilation failed in require at - line 2.
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.
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
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.
337
338########
339
340# ID 20020703.002 (#10021)
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.
348########
349
350# [perl #10021]
351use strict;
352use warnings;
353print "" if BAREWORD;
354EXPECT
355Bareword "BAREWORD" not allowed while "strict subs" in use at - line 5.
356Execution of - aborted due to compilation errors.
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.
364########
365eval q{ use strict; no strict refs; };
366print $@;
367EXPECT
368Bareword "refs" not allowed while "strict subs" in use at (eval 1) line 1.
369########
370# [perl #25147]
371use strict;
372print "" if BAREWORD;
373EXPECT
374Bareword "BAREWORD" not allowed while "strict subs" in use at - line 3.
375Execution of - aborted due to compilation errors.
376########
377# [perl #26910] hints not propagated into (?{...})
378use strict 'subs';
379qr/(?{my $x=foo})/;
380EXPECT
381Bareword "foo" not allowed while "strict subs" in use at - line 3.
382Execution of - aborted due to compilation errors.
383########
384# Regexp compilation errors weren't UTF-8 clean
385use strict 'subs';
386use utf8;
387use open qw( :utf8 :std );
388qr/(?{my $x=fòò})/;
389EXPECT
390Bareword "fòò" not allowed while "strict subs" in use at - line 5.
391Execution of - aborted due to compilation errors.
392########
393# [perl #27628] strict 'subs' didn't warn on bareword array index
394use strict 'subs';
395my $x=$a[FOO];
396EXPECT
397Bareword "FOO" not allowed while "strict subs" in use at - line 3.
398Execution of - aborted due to compilation errors.
399########
400use strict 'subs';
401my @a;my $x=$a[FOO];
402EXPECT
403Bareword "FOO" not allowed while "strict subs" in use at - line 2.
404Execution of - aborted due to compilation errors.
405########
406# [perl #53806] No complain about bareword
407use strict 'subs';
408print FOO . "\n";
409EXPECT
410Bareword "FOO" not allowed while "strict subs" in use at - line 3.
411Execution of - aborted due to compilation errors.
412########
413# [perl #53806] No complain about bareword
414use strict 'subs';
415$ENV{PATH} = "";
416system(FOO . "\n");
417EXPECT
418Bareword "FOO" not allowed while "strict subs" in use at - line 4.
419Execution of - aborted due to compilation errors.
420########
421use strict 'subs';
422my @players;
423eval { @players = sort(_rankCompare @players) };
424sub _rankCompare2 { }
425@players = sort(_rankCompare2 @players);
426EXPECT
427
428########
429use strict;
430readline(FOO);
431EXPECT
432
433########
434use strict 'subs';
435sub sayfoo { print "foo:@_\n" ; "ret\n" }
436print sayfoo "bar";
437print sayfoo . "bar\n";
438EXPECT
439foo:bar
440ret
441foo:
442ret
443bar
444########
445# infinite loop breaks some strict checking
446use strict 'subs';
447sub foo {
448 1 while 1;
449 kill FOO, 1;
450}
451EXPECT
452Bareword "FOO" not allowed while "strict subs" in use at - line 5.
453Execution of - aborted due to compilation errors.
454########
455# make sure checks are done within (?{})
456use strict 'subs';
457/(?{FOO})/
458EXPECT
459Bareword "FOO" not allowed while "strict subs" in use at - line 3.
460Execution of - aborted due to compilation errors.
461########
462# [perl #126981] Strict subs vs. multideref
463my $h;
464my $v1 = $h->{+CONST_TYPO};
465use strict 'subs';
466my $v2 = $h->{+CONST_TYPO};
467EXPECT
468Bareword "CONST_TYPO" not allowed while "strict subs" in use at - line 5.
469Execution of - aborted due to compilation errors.
470########
471# NAME constant-folded barewords still trigger stricture
472my $x = !BARE1;
473use strict 'subs';
474my $y = !BARE2;
475EXPECT
476Bareword "BARE2" not allowed while "strict subs" in use at - line 3.
477Execution of - aborted due to compilation errors.