This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix *_ = "" for 0 .. 1;
[perl5.git] / t / op / for.t
CommitLineData
e150f77d
JK
1#!./perl
2
3BEGIN {
b5efbd1f 4 chdir 't' if -d 't';
1ae3d757 5 require "./test.pl";
e150f77d
JK
6}
7
6d3ca00e 8plan(124);
e150f77d
JK
9
10# A lot of tests to check that reversed for works.
e150f77d
JK
11
12@array = ('A', 'B', 'C');
13for (@array) {
14 $r .= $_;
15}
16is ($r, 'ABC', 'Forwards for array');
17$r = '';
18for (1,2,3) {
19 $r .= $_;
20}
21is ($r, '123', 'Forwards for list');
22$r = '';
23for (map {$_} @array) {
24 $r .= $_;
25}
26is ($r, 'ABC', 'Forwards for array via map');
27$r = '';
28for (map {$_} 1,2,3) {
29 $r .= $_;
30}
31is ($r, '123', 'Forwards for list via map');
32$r = '';
33for (1 .. 3) {
34 $r .= $_;
35}
36is ($r, '123', 'Forwards for list via ..');
37$r = '';
38for ('A' .. 'C') {
39 $r .= $_;
40}
41is ($r, 'ABC', 'Forwards for list via ..');
42
43$r = '';
44for (reverse @array) {
45 $r .= $_;
46}
47is ($r, 'CBA', 'Reverse for array');
48$r = '';
49for (reverse 1,2,3) {
50 $r .= $_;
51}
52is ($r, '321', 'Reverse for list');
53$r = '';
54for (reverse map {$_} @array) {
55 $r .= $_;
56}
57is ($r, 'CBA', 'Reverse for array via map');
58$r = '';
59for (reverse map {$_} 1,2,3) {
60 $r .= $_;
61}
62is ($r, '321', 'Reverse for list via map');
63$r = '';
64for (reverse 1 .. 3) {
65 $r .= $_;
66}
67is ($r, '321', 'Reverse for list via ..');
68$r = '';
69for (reverse 'A' .. 'C') {
70 $r .= $_;
71}
72is ($r, 'CBA', 'Reverse for list via ..');
73
74$r = '';
75for my $i (@array) {
76 $r .= $i;
77}
78is ($r, 'ABC', 'Forwards for array with var');
79$r = '';
80for my $i (1,2,3) {
81 $r .= $i;
82}
83is ($r, '123', 'Forwards for list with var');
84$r = '';
85for my $i (map {$_} @array) {
86 $r .= $i;
87}
88is ($r, 'ABC', 'Forwards for array via map with var');
89$r = '';
90for my $i (map {$_} 1,2,3) {
91 $r .= $i;
92}
93is ($r, '123', 'Forwards for list via map with var');
94$r = '';
95for my $i (1 .. 3) {
96 $r .= $i;
97}
98is ($r, '123', 'Forwards for list via .. with var');
99$r = '';
100for my $i ('A' .. 'C') {
101 $r .= $i;
102}
103is ($r, 'ABC', 'Forwards for list via .. with var');
104
105$r = '';
106for my $i (reverse @array) {
107 $r .= $i;
108}
109is ($r, 'CBA', 'Reverse for array with var');
110$r = '';
111for my $i (reverse 1,2,3) {
112 $r .= $i;
113}
114is ($r, '321', 'Reverse for list with var');
115$r = '';
116for my $i (reverse map {$_} @array) {
117 $r .= $i;
118}
119is ($r, 'CBA', 'Reverse for array via map with var');
120$r = '';
121for my $i (reverse map {$_} 1,2,3) {
122 $r .= $i;
123}
124is ($r, '321', 'Reverse for list via map with var');
125$r = '';
126for my $i (reverse 1 .. 3) {
127 $r .= $i;
128}
129is ($r, '321', 'Reverse for list via .. with var');
130$r = '';
131for my $i (reverse 'A' .. 'C') {
132 $r .= $i;
133}
134is ($r, 'CBA', 'Reverse for list via .. with var');
135
136# For some reason the generate optree is different when $_ is implicit.
137$r = '';
138for $_ (@array) {
139 $r .= $_;
140}
141is ($r, 'ABC', 'Forwards for array with explicit $_');
142$r = '';
143for $_ (1,2,3) {
144 $r .= $_;
145}
146is ($r, '123', 'Forwards for list with explicit $_');
147$r = '';
148for $_ (map {$_} @array) {
149 $r .= $_;
150}
151is ($r, 'ABC', 'Forwards for array via map with explicit $_');
152$r = '';
153for $_ (map {$_} 1,2,3) {
154 $r .= $_;
155}
156is ($r, '123', 'Forwards for list via map with explicit $_');
157$r = '';
158for $_ (1 .. 3) {
159 $r .= $_;
160}
161is ($r, '123', 'Forwards for list via .. with var with explicit $_');
162$r = '';
163for $_ ('A' .. 'C') {
164 $r .= $_;
165}
166is ($r, 'ABC', 'Forwards for list via .. with var with explicit $_');
167
168$r = '';
169for $_ (reverse @array) {
170 $r .= $_;
171}
172is ($r, 'CBA', 'Reverse for array with explicit $_');
173$r = '';
174for $_ (reverse 1,2,3) {
175 $r .= $_;
176}
177is ($r, '321', 'Reverse for list with explicit $_');
178$r = '';
179for $_ (reverse map {$_} @array) {
180 $r .= $_;
181}
182is ($r, 'CBA', 'Reverse for array via map with explicit $_');
183$r = '';
184for $_ (reverse map {$_} 1,2,3) {
185 $r .= $_;
186}
187is ($r, '321', 'Reverse for list via map with explicit $_');
188$r = '';
189for $_ (reverse 1 .. 3) {
190 $r .= $_;
191}
192is ($r, '321', 'Reverse for list via .. with var with explicit $_');
193$r = '';
194for $_ (reverse 'A' .. 'C') {
195 $r .= $_;
196}
197is ($r, 'CBA', 'Reverse for list via .. with var with explicit $_');
198
199# I don't think that my is that different from our in the optree. But test a
200# few:
201$r = '';
202for our $i (reverse @array) {
203 $r .= $i;
204}
205is ($r, 'CBA', 'Reverse for array with our var');
206$r = '';
207for our $i (reverse 1,2,3) {
208 $r .= $i;
209}
210is ($r, '321', 'Reverse for list with our var');
211$r = '';
212for our $i (reverse map {$_} @array) {
213 $r .= $i;
214}
215is ($r, 'CBA', 'Reverse for array via map with our var');
216$r = '';
217for our $i (reverse map {$_} 1,2,3) {
218 $r .= $i;
219}
220is ($r, '321', 'Reverse for list via map with our var');
221$r = '';
222for our $i (reverse 1 .. 3) {
223 $r .= $i;
224}
225is ($r, '321', 'Reverse for list via .. with our var');
226$r = '';
227for our $i (reverse 'A' .. 'C') {
228 $r .= $i;
229}
230is ($r, 'CBA', 'Reverse for list via .. with our var');
231
232
233$r = '';
234for (1, reverse @array) {
235 $r .= $_;
236}
237is ($r, '1CBA', 'Reverse for array with leading value');
238$r = '';
239for ('A', reverse 1,2,3) {
240 $r .= $_;
241}
242is ($r, 'A321', 'Reverse for list with leading value');
243$r = '';
244for (1, reverse map {$_} @array) {
245 $r .= $_;
246}
247is ($r, '1CBA', 'Reverse for array via map with leading value');
248$r = '';
249for ('A', reverse map {$_} 1,2,3) {
250 $r .= $_;
251}
252is ($r, 'A321', 'Reverse for list via map with leading value');
253$r = '';
254for ('A', reverse 1 .. 3) {
255 $r .= $_;
256}
257is ($r, 'A321', 'Reverse for list via .. with leading value');
258$r = '';
259for (1, reverse 'A' .. 'C') {
260 $r .= $_;
261}
262is ($r, '1CBA', 'Reverse for list via .. with leading value');
263
264$r = '';
265for (reverse (@array), 1) {
266 $r .= $_;
267}
268is ($r, 'CBA1', 'Reverse for array with trailing value');
269$r = '';
270for (reverse (1,2,3), 'A') {
271 $r .= $_;
272}
273is ($r, '321A', 'Reverse for list with trailing value');
274$r = '';
275for (reverse (map {$_} @array), 1) {
276 $r .= $_;
277}
278is ($r, 'CBA1', 'Reverse for array via map with trailing value');
279$r = '';
280for (reverse (map {$_} 1,2,3), 'A') {
281 $r .= $_;
282}
283is ($r, '321A', 'Reverse for list via map with trailing value');
284$r = '';
285for (reverse (1 .. 3), 'A') {
286 $r .= $_;
287}
288is ($r, '321A', 'Reverse for list via .. with trailing value');
289$r = '';
290for (reverse ('A' .. 'C'), 1) {
291 $r .= $_;
292}
293is ($r, 'CBA1', 'Reverse for list via .. with trailing value');
294
295
296$r = '';
297for $_ (1, reverse @array) {
298 $r .= $_;
299}
300is ($r, '1CBA', 'Reverse for array with leading value with explicit $_');
301$r = '';
302for $_ ('A', reverse 1,2,3) {
303 $r .= $_;
304}
305is ($r, 'A321', 'Reverse for list with leading value with explicit $_');
306$r = '';
307for $_ (1, reverse map {$_} @array) {
308 $r .= $_;
309}
310is ($r, '1CBA',
311 'Reverse for array via map with leading value with explicit $_');
312$r = '';
313for $_ ('A', reverse map {$_} 1,2,3) {
314 $r .= $_;
315}
316is ($r, 'A321', 'Reverse for list via map with leading value with explicit $_');
317$r = '';
318for $_ ('A', reverse 1 .. 3) {
319 $r .= $_;
320}
321is ($r, 'A321', 'Reverse for list via .. with leading value with explicit $_');
322$r = '';
323for $_ (1, reverse 'A' .. 'C') {
324 $r .= $_;
325}
326is ($r, '1CBA', 'Reverse for list via .. with leading value with explicit $_');
327
328$r = '';
329for $_ (reverse (@array), 1) {
330 $r .= $_;
331}
332is ($r, 'CBA1', 'Reverse for array with trailing value with explicit $_');
333$r = '';
334for $_ (reverse (1,2,3), 'A') {
335 $r .= $_;
336}
337is ($r, '321A', 'Reverse for list with trailing value with explicit $_');
338$r = '';
339for $_ (reverse (map {$_} @array), 1) {
340 $r .= $_;
341}
342is ($r, 'CBA1',
343 'Reverse for array via map with trailing value with explicit $_');
344$r = '';
345for $_ (reverse (map {$_} 1,2,3), 'A') {
346 $r .= $_;
347}
348is ($r, '321A',
349 'Reverse for list via map with trailing value with explicit $_');
350$r = '';
351for $_ (reverse (1 .. 3), 'A') {
352 $r .= $_;
353}
354is ($r, '321A', 'Reverse for list via .. with trailing value with explicit $_');
355$r = '';
356for $_ (reverse ('A' .. 'C'), 1) {
357 $r .= $_;
358}
359is ($r, 'CBA1', 'Reverse for list via .. with trailing value with explicit $_');
360
361$r = '';
362for my $i (1, reverse @array) {
363 $r .= $i;
364}
365is ($r, '1CBA', 'Reverse for array with leading value and var');
366$r = '';
367for my $i ('A', reverse 1,2,3) {
368 $r .= $i;
369}
370is ($r, 'A321', 'Reverse for list with leading value and var');
371$r = '';
372for my $i (1, reverse map {$_} @array) {
373 $r .= $i;
374}
375is ($r, '1CBA', 'Reverse for array via map with leading value and var');
376$r = '';
377for my $i ('A', reverse map {$_} 1,2,3) {
378 $r .= $i;
379}
380is ($r, 'A321', 'Reverse for list via map with leading value and var');
381$r = '';
382for my $i ('A', reverse 1 .. 3) {
383 $r .= $i;
384}
385is ($r, 'A321', 'Reverse for list via .. with leading value and var');
386$r = '';
387for my $i (1, reverse 'A' .. 'C') {
388 $r .= $i;
389}
390is ($r, '1CBA', 'Reverse for list via .. with leading value and var');
391
392$r = '';
393for my $i (reverse (@array), 1) {
394 $r .= $i;
395}
396is ($r, 'CBA1', 'Reverse for array with trailing value and var');
397$r = '';
398for my $i (reverse (1,2,3), 'A') {
399 $r .= $i;
400}
401is ($r, '321A', 'Reverse for list with trailing value and var');
402$r = '';
403for my $i (reverse (map {$_} @array), 1) {
404 $r .= $i;
405}
406is ($r, 'CBA1', 'Reverse for array via map with trailing value and var');
407$r = '';
408for my $i (reverse (map {$_} 1,2,3), 'A') {
409 $r .= $i;
410}
411is ($r, '321A', 'Reverse for list via map with trailing value and var');
412$r = '';
413for my $i (reverse (1 .. 3), 'A') {
414 $r .= $i;
415}
416is ($r, '321A', 'Reverse for list via .. with trailing value and var');
417$r = '';
418for my $i (reverse ('A' .. 'C'), 1) {
419 $r .= $i;
420}
421is ($r, 'CBA1', 'Reverse for list via .. with trailing value and var');
422
423
424$r = '';
425for (reverse 1, @array) {
426 $r .= $_;
427}
428is ($r, 'CBA1', 'Reverse for value and array');
429$r = '';
430for (reverse map {$_} 1, @array) {
431 $r .= $_;
432}
433is ($r, 'CBA1', 'Reverse for value and array via map');
434$r = '';
435for (reverse 1 .. 3, @array) {
436 $r .= $_;
437}
438is ($r, 'CBA321', 'Reverse for .. and array');
439$r = '';
440for (reverse 'X' .. 'Z', @array) {
441 $r .= $_;
442}
443is ($r, 'CBAZYX', 'Reverse for .. and array');
444$r = '';
445for (reverse map {$_} 1 .. 3, @array) {
446 $r .= $_;
447}
448is ($r, 'CBA321', 'Reverse for .. and array via map');
449$r = '';
450for (reverse map {$_} 'X' .. 'Z', @array) {
451 $r .= $_;
452}
453is ($r, 'CBAZYX', 'Reverse for .. and array via map');
454
455$r = '';
456for (reverse (@array, 1)) {
457 $r .= $_;
458}
459is ($r, '1CBA', 'Reverse for array and value');
460$r = '';
461for (reverse (map {$_} @array, 1)) {
462 $r .= $_;
463}
464is ($r, '1CBA', 'Reverse for array and value via map');
465
466$r = '';
467for $_ (reverse 1, @array) {
468 $r .= $_;
469}
470is ($r, 'CBA1', 'Reverse for value and array with explicit $_');
471$r = '';
472for $_ (reverse map {$_} 1, @array) {
473 $r .= $_;
474}
475is ($r, 'CBA1', 'Reverse for value and array via map with explicit $_');
476$r = '';
477for $_ (reverse 1 .. 3, @array) {
478 $r .= $_;
479}
480is ($r, 'CBA321', 'Reverse for .. and array with explicit $_');
481$r = '';
482for $_ (reverse 'X' .. 'Z', @array) {
483 $r .= $_;
484}
485is ($r, 'CBAZYX', 'Reverse for .. and array with explicit $_');
486$r = '';
487for $_ (reverse map {$_} 1 .. 3, @array) {
488 $r .= $_;
489}
490is ($r, 'CBA321', 'Reverse for .. and array via map with explicit $_');
491$r = '';
492for $_ (reverse map {$_} 'X' .. 'Z', @array) {
493 $r .= $_;
494}
495is ($r, 'CBAZYX', 'Reverse for .. and array via map with explicit $_');
496
497$r = '';
498for $_ (reverse (@array, 1)) {
499 $r .= $_;
500}
501is ($r, '1CBA', 'Reverse for array and value with explicit $_');
502$r = '';
503for $_ (reverse (map {$_} @array, 1)) {
504 $r .= $_;
505}
506is ($r, '1CBA', 'Reverse for array and value via map with explicit $_');
507
508
509$r = '';
510for my $i (reverse 1, @array) {
511 $r .= $i;
512}
513is ($r, 'CBA1', 'Reverse for value and array with var');
514$r = '';
515for my $i (reverse map {$_} 1, @array) {
516 $r .= $i;
517}
518is ($r, 'CBA1', 'Reverse for value and array via map with var');
519$r = '';
520for my $i (reverse 1 .. 3, @array) {
521 $r .= $i;
522}
523is ($r, 'CBA321', 'Reverse for .. and array with var');
524$r = '';
525for my $i (reverse 'X' .. 'Z', @array) {
526 $r .= $i;
527}
528is ($r, 'CBAZYX', 'Reverse for .. and array with var');
529$r = '';
530for my $i (reverse map {$_} 1 .. 3, @array) {
531 $r .= $i;
532}
533is ($r, 'CBA321', 'Reverse for .. and array via map with var');
534$r = '';
535for my $i (reverse map {$_} 'X' .. 'Z', @array) {
536 $r .= $i;
537}
538is ($r, 'CBAZYX', 'Reverse for .. and array via map with var');
539
540$r = '';
541for my $i (reverse (@array, 1)) {
542 $r .= $i;
543}
544is ($r, '1CBA', 'Reverse for array and value with var');
545$r = '';
546for my $i (reverse (map {$_} @array, 1)) {
547 $r .= $i;
548}
549is ($r, '1CBA', 'Reverse for array and value via map with var');
550
551TODO: {
552 if (do {17; foreach (1, 2) { 1; } } != 17) {
553 #print "not ";
554 todo_skip("RT #1085: what should be output of perl -we 'print do { foreach (1, 2) { 1; } }'");
555 }
556}
557
558TODO: {
559 local $TODO = "RT #2166: foreach spuriously autovivifies";
560 my %h;
561 foreach (@h{a, b}) {}
562 if(keys(%h)) {
563 todo_skip("RT #2166: foreach spuriously autovivifies");
564 }
565}
a600f7e6
FC
566
567sub {
568 foreach (@_) {
569 is eval { \$_ }, \undef, 'foreach (@array_containing_undef)'
570 }
571}->(undef);
031c6d82
FC
572
573SKIP: {
574 skip "No XS::APItest under miniperl", 1, if is_miniperl;
7a258a81 575 skip "no XS::APItest", 1 if !eval { require XS::APItest };
031c6d82
FC
576 my @a;
577 sub {
031c6d82
FC
578 XS::APItest::alias_av(\@a, 0, undef);
579 eval { \$_[0] }
580 }->($a[0]);
581 is $@, "", 'vivify_defelem does not croak on &PL_sv_undef elements';
582}
c997e362
FC
583
584for $x ($y) {
585 $x = 3;
586 ($x, my $z) = (1, $y);
587 is $z, 3, 'list assignment after aliasing via foreach';
588}
6bd144e0
FC
589
590for my $x (my $y) {
591 $x = 3;
592 ($x, my $z) = (1, $y);
593 is $z, 3, 'list assignment after aliasing lexical var via foreach';
594}
c5f78d08
FC
595
596@_ = ();
597@_ = (1,2,3,scalar do{for(@_){}} + 1, 4, 5, 6);
598is "@_", "1 2 3 1 4 5 6",
599 '[perl #124004] scalar for(@empty_array) stack bug';
b52de964
DM
600
601# DAPM: while messing with the scope code, I broke some cpan/ code,
602# but surprisingly didn't break any dedicated tests. So test it:
603
604sub fscope {
605 for my $y (1,2) {
606 my $a = $y;
607 return $a;
608 }
609}
610
611is(fscope(), 1, 'return via loop in sub');
9af0df0b
DM
612
613# make sure a NULL GvSV is restored at the end of the loop
614
615{
616 local $foo = "boo";
617 {
618 local *foo;
619 for $foo (1,2) {}
620 ok(!defined $foo, "NULL GvSV");
621 }
622}
a62db3cf
DM
623
624# make sure storing an int in a NULL GvSV is ok
625
626{
627 local $foo = "boo";
628 {
629 local *foo;
630 for $foo (1..2) {}
631 ok(!defined $foo, "NULL GvSV int iterator");
632 }
633}
6d3ca00e
DM
634
635# RT #123994 - handle a null GVSV within a loop
636
637{
638 local *foo;
639 local $foo = "outside";
640
641 my $i = 0;
642 for $foo (0..1) {
643 is($foo, $i, "RT #123994 int range $i");
644 *foo = "";
645 $i++;
646 }
647 is($foo, "outside", "RT #123994 int range outside");
648
649 $i = 0;
650 for $foo ('0'..'1') {
651 is($foo, $i, "RT #123994 str range $i");
652 *foo = "";
653 $i++;
654 }
655 is($foo, "outside", "RT #123994 str range outside");
656
657 $i = 0;
658 for $foo (0, 1) {
659 is($foo, $i, "RT #123994 list $i");
660 *foo = "";
661 $i++;
662 }
663 is($foo, "outside", "RT #123994 list outside");
664
665 my @a = (0,1);
666 $i = 0;
667 for $foo (@a) {
668 is($foo, $i, "RT #123994 array $i");
669 *foo = "";
670 $i++;
671 }
672 is($foo, "outside", "RT #123994 array outside");
673}