6 if (($Config::Config{'extensions'} !~ /\bB\b/) ){
7 print "1..0 # Skip -- Perl configured without B module\n";
10 if (!$Config::Config{useperlio}) {
11 print "1..0 # Skip -- need perlio to walk the optree\n";
19 pass("GENERAL OPTREE EXAMPLES");
21 pass("IF,THEN,ELSE, ?:");
23 checkOptree ( name => '-basic sub {if shift print then,else}',
25 code => sub { if (shift) { print "then" }
28 strip_open_hints => 1,
29 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
30 # 7 <1> leavesub[1 ref] K/REFC,1 ->(end)
31 # - <@> lineseq KP ->7
32 # 1 <;> nextstate(main 665 optree_samples.t:24) v:>,<,% ->2
34 # 3 <|> cond_expr(other->4) K/1 ->8
37 # - <;> ex-nextstate(main 1594 optree_samples.t:25) v:>,<,% ->4
39 # 4 <0> pushmark s ->5
40 # 5 <$> const[PV "then"] s ->6
43 # 9 <;> nextstate(main 663 optree_samples.t:25) v:>,<,% ->a
45 # a <0> pushmark s ->b
46 # b <$> const[PV "else"] s ->c
48 # 7 <1> leavesub[1 ref] K/REFC,1 ->(end)
49 # - <@> lineseq KP ->7
50 # 1 <;> nextstate(main 665 optree_samples.t:24) v:>,<,% ->2
52 # 3 <|> cond_expr(other->4) K/1 ->8
55 # - <;> ex-nextstate(main 1594 optree_samples.t:25) v:>,<,% ->4
57 # 4 <0> pushmark s ->5
58 # 5 <$> const(PV "then") s ->6
61 # 9 <;> nextstate(main 663 optree_samples.t:25) v:>,<,% ->a
63 # a <0> pushmark s ->b
64 # b <$> const(PV "else") s ->c
67 checkOptree ( name => '-basic (see above, with my $a = shift)',
69 code => sub { my $a = shift;
70 if ($a) { print "foo" }
73 strip_open_hints => 1,
74 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
75 # b <1> leavesub[1 ref] K/REFC,1 ->(end)
76 # - <@> lineseq KP ->b
77 # 1 <;> nextstate(main 666 optree_samples.t:70) v:>,<,% ->2
78 # 4 <2> sassign vKS/2 ->5
80 # 3 <0> padsv[$a:666,670] sRM*/LVINTRO ->4
81 # 5 <;> nextstate(main 670 optree_samples.t:71) v:>,<,% ->6
83 # 7 <|> cond_expr(other->8) K/1 ->c
84 # 6 <0> padsv[$a:666,670] s ->7
86 # - <;> ex-nextstate(main 1603 optree_samples.t:70) v:>,<,% ->8
88 # 8 <0> pushmark s ->9
89 # 9 <$> const[PV "foo"] s ->a
92 # d <;> nextstate(main 668 optree_samples.t:72) v:>,<,% ->e
94 # e <0> pushmark s ->f
95 # f <$> const[PV "bar"] s ->g
97 # b <1> leavesub[1 ref] K/REFC,1 ->(end)
98 # - <@> lineseq KP ->b
99 # 1 <;> nextstate(main 666 optree_samples.t:72) v:>,<,% ->2
100 # 4 <2> sassign vKS/2 ->5
102 # 3 <0> padsv[$a:666,670] sRM*/LVINTRO ->4
103 # 5 <;> nextstate(main 670 optree_samples.t:73) v:>,<,% ->6
105 # 7 <|> cond_expr(other->8) K/1 ->c
106 # 6 <0> padsv[$a:666,670] s ->7
108 # - <;> ex-nextstate(main 1603 optree_samples.t:70) v:>,<,% ->8
110 # 8 <0> pushmark s ->9
111 # 9 <$> const(PV "foo") s ->a
114 # d <;> nextstate(main 668 optree_samples.t:74) v:>,<,% ->e
116 # e <0> pushmark s ->f
117 # f <$> const(PV "bar") s ->g
120 checkOptree ( name => '-exec sub {if shift print then,else}',
122 code => sub { if (shift) { print "then" }
123 else { print "else" }
125 strip_open_hints => 1,
126 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
127 # 1 <;> nextstate(main 674 optree_samples.t:125) v:>,<,%
129 # 3 <|> cond_expr(other->4) K/1
131 # 5 <$> const[PV "then"] s
135 # 9 <;> nextstate(main 672 optree_samples.t:126) v:>,<,%
137 # b <$> const[PV "else"] s
140 # 7 <1> leavesub[1 ref] K/REFC,1
142 # 1 <;> nextstate(main 674 optree_samples.t:129) v:>,<,%
144 # 3 <|> cond_expr(other->4) K/1
146 # 5 <$> const(PV "then") s
150 # 9 <;> nextstate(main 672 optree_samples.t:130) v:>,<,%
152 # b <$> const(PV "else") s
155 # 7 <1> leavesub[1 ref] K/REFC,1
158 checkOptree ( name => '-exec (see above, with my $a = shift)',
160 code => sub { my $a = shift;
161 if ($a) { print "foo" }
164 strip_open_hints => 1,
165 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
166 # 1 <;> nextstate(main 675 optree_samples.t:165) v:>,<,%
168 # 3 <0> padsv[$a:675,679] sRM*/LVINTRO
169 # 4 <2> sassign vKS/2
170 # 5 <;> nextstate(main 679 optree_samples.t:166) v:>,<,%
171 # 6 <0> padsv[$a:675,679] s
172 # 7 <|> cond_expr(other->8) K/1
174 # 9 <$> const[PV "foo"] s
178 # d <;> nextstate(main 677 optree_samples.t:167) v:>,<,%
180 # f <$> const[PV "bar"] s
183 # b <1> leavesub[1 ref] K/REFC,1
185 # 1 <;> nextstate(main 675 optree_samples.t:171) v:>,<,%
187 # 3 <0> padsv[$a:675,679] sRM*/LVINTRO
188 # 4 <2> sassign vKS/2
189 # 5 <;> nextstate(main 679 optree_samples.t:172) v:>,<,%
190 # 6 <0> padsv[$a:675,679] s
191 # 7 <|> cond_expr(other->8) K/1
193 # 9 <$> const(PV "foo") s
197 # d <;> nextstate(main 677 optree_samples.t:173) v:>,<,%
199 # f <$> const(PV "bar") s
202 # b <1> leavesub[1 ref] K/REFC,1
205 checkOptree ( name => '-exec sub { print (shift) ? "foo" : "bar" }',
206 code => sub { print (shift) ? "foo" : "bar" },
208 strip_open_hints => 1,
209 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
210 # 1 <;> nextstate(main 680 optree_samples.t:213) v:>,<,%
214 # 5 <|> cond_expr(other->6) K/1
215 # 6 <$> const[PV "foo"] s
217 # 8 <$> const[PV "bar"] s
218 # 7 <1> leavesub[1 ref] K/REFC,1
220 # 1 <;> nextstate(main 680 optree_samples.t:221) v:>,<,%
224 # 5 <|> cond_expr(other->6) K/1
225 # 6 <$> const(PV "foo") s
227 # 8 <$> const(PV "bar") s
228 # 7 <1> leavesub[1 ref] K/REFC,1
233 checkOptree ( name => '-exec sub { foreach (1..10) {print "foo $_"} }',
234 code => sub { foreach (1..10) {print "foo $_"} },
236 strip_open_hints => 1,
237 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
238 # 1 <;> nextstate(main 443 optree.t:158) v:>,<,%
240 # 3 <$> const[IV 1] s
241 # 4 <$> const[IV 10] s
243 # 6 <{> enteriter(next->c last->f redo->7) KS/DEF
245 # e <|> and(other->7) K/1
246 # 7 <;> nextstate(main 1659 optree_samples.t:234) v:>,<,%
249 # a <+> multiconcat("foo ",4,-1)[t5] sK/STRINGIFY
253 # f <2> leaveloop K/2
254 # g <1> leavesub[1 ref] K/REFC,1
256 # 1 <;> nextstate(main 444 optree_samples.t:182) v:>,<,%
258 # 3 <$> const(IV 1) s
259 # 4 <$> const(IV 10) s
261 # 6 <{> enteriter(next->c last->f redo->7) KS/DEF
263 # e <|> and(other->7) K/1
264 # 7 <;> nextstate(main 443 optree_samples.t:182) v:>,<,%
267 # a <+> multiconcat("foo ",4,-1)[t4] sK/STRINGIFY
271 # f <2> leaveloop K/2
272 # g <1> leavesub[1 ref] K/REFC,1
275 checkOptree ( name => '-basic sub { print "foo $_" foreach (1..10) }',
276 code => sub { print "foo $_" foreach (1..10) },
278 strip_open_hints => 1,
279 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
280 # f <1> leavesub[1 ref] K/REFC,1 ->(end)
281 # - <@> lineseq KP ->f
282 # 1 <;> nextstate(main 445 optree.t:167) v:>,<,% ->2
283 # e <2> leaveloop K/2 ->f
284 # 6 <{> enteriter(next->b last->e redo->7) KS/DEF ->c
285 # - <0> ex-pushmark s ->2
286 # - <1> ex-list lK ->5
287 # 2 <0> pushmark s ->3
288 # 3 <$> const[IV 1] s ->4
289 # 4 <$> const[IV 10] s ->5
292 # d <|> and(other->7) K/1 ->e
294 # - <@> lineseq sK ->-
296 # 7 <0> pushmark s ->8
297 # 9 <+> multiconcat("foo ",4,-1)[t3] sK/STRINGIFY ->a
298 # - <0> ex-pushmark s ->-
299 # - <0> ex-const s ->8
300 # - <1> ex-rv2sv sK/1 ->9
301 # 8 <#> gvsv[*_] s ->9
302 # b <0> unstack s ->c
304 # f <1> leavesub[1 ref] K/REFC,1 ->(end)
305 # - <@> lineseq KP ->f
306 # 1 <;> nextstate(main 446 optree_samples.t:192) v:>,<,% ->2
307 # e <2> leaveloop K/2 ->f
308 # 6 <{> enteriter(next->b last->e redo->7) KS/DEF ->c
309 # - <0> ex-pushmark s ->2
310 # - <1> ex-list lK ->5
311 # 2 <0> pushmark s ->3
312 # 3 <$> const(IV 1) s ->4
313 # 4 <$> const(IV 10) s ->5
316 # d <|> and(other->7) K/1 ->e
318 # - <@> lineseq sK ->-
320 # 7 <0> pushmark s ->8
321 # 9 <+> multiconcat("foo ",4,-1)[t2] sK/STRINGIFY ->a
322 # - <0> ex-pushmark s ->-
323 # - <0> ex-const s ->8
324 # - <1> ex-rv2sv sK/1 ->9
325 # 8 <$> gvsv(*_) s ->9
326 # b <0> unstack s ->c
329 checkOptree ( name => '-exec -e foreach (1..10) {print qq{foo $_}}',
330 prog => 'foreach (1..10) {print qq{foo $_}}',
332 strip_open_hints => 1,
333 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
335 # 2 <;> nextstate(main 2 -e:1) v:>,<,%,{
337 # 4 <$> const[IV 1] s
338 # 5 <$> const[IV 10] s
340 # 7 <{> enteriter(next->d last->g redo->8) vKS/DEF
342 # f <|> and(other->8) vK/1
343 # 8 <;> nextstate(main 1 -e:1) v:>,<,%
346 # b <+> multiconcat("foo ",4,-1)[t5] sK/STRINGIFY
350 # g <2> leaveloop vK/2
351 # h <@> leave[1 ref] vKP/REFC
354 # 2 <;> nextstate(main 2 -e:1) v:>,<,%,{
356 # 4 <$> const(IV 1) s
357 # 5 <$> const(IV 10) s
359 # 7 <{> enteriter(next->d last->g redo->8) vKS/DEF
361 # f <|> and(other->8) vK/1
362 # 8 <;> nextstate(main 1 -e:1) v:>,<,%
365 # b <+> multiconcat("foo ",4,-1)[t4] sK/STRINGIFY
369 # g <2> leaveloop vK/2
370 # h <@> leave[1 ref] vKP/REFC
373 checkOptree ( name => '-exec sub { print "foo $_" foreach (1..10) }',
374 code => sub { print "foo $_" foreach (1..10) },
376 strip_open_hints => 1,
377 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
378 # 1 <;> nextstate(main 445 optree.t:167) v:>,<,%
380 # 3 <$> const[IV 1] s
381 # 4 <$> const[IV 10] s
383 # 6 <{> enteriter(next->b last->e redo->7) KS/DEF
385 # d <|> and(other->7) K/1
388 # 9 <+> multiconcat("foo ",4,-1)[t3] sK/STRINGIFY
392 # e <2> leaveloop K/2
393 # f <1> leavesub[1 ref] K/REFC,1
395 # 1 <;> nextstate(main 447 optree_samples.t:252) v:>,<,%
397 # 3 <$> const(IV 1) s
398 # 4 <$> const(IV 10) s
400 # 6 <{> enteriter(next->b last->e redo->7) KS/DEF
402 # d <|> and(other->7) K/1
405 # 9 <+> multiconcat("foo ",4,-1)[t2] sK/STRINGIFY
409 # e <2> leaveloop K/2
410 # f <1> leavesub[1 ref] K/REFC,1
413 pass("GREP: SAMPLES FROM PERLDOC -F GREP");
415 checkOptree ( name => '@foo = grep(!/^\#/, @bar)',
416 code => '@foo = grep(!/^\#/, @bar)',
418 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
419 # 1 <;> nextstate(main 496 (eval 20):1) v:{
423 # 5 <1> rv2av[t4] lKM/1
425 # 7 <|> grepwhile(other->8)[t5] lK
426 # 8 </> match(/"^#"/) s
431 # c <1> rv2av[t2] lKRM*/1
432 # d <2> aassign[t6] KS/COM_AGG
433 # e <1> leavesub[1 ref] K/REFC,1
435 # 1 <;> nextstate(main 496 (eval 20):1) v:{
439 # 5 <1> rv2av[t2] lKM/1
441 # 7 <|> grepwhile(other->8)[t3] lK
442 # 8 </> match(/"^\\#"/) s
447 # c <1> rv2av[t1] lKRM*/1
448 # d <2> aassign[t4] KS/COM_AGG
449 # e <1> leavesub[1 ref] K/REFC,1
453 pass("MAP: SAMPLES FROM PERLDOC -F MAP");
455 checkOptree ( name => '%h = map { getkey($_) => $_ } @a',
456 code => '%h = map { getkey($_) => $_ } @a',
458 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
459 # 1 <;> nextstate(main 501 (eval 22):1) v:{
463 # 5 <1> rv2av[t8] lKM/1
464 # 6 <@> mapstart lK* < 5.017002
465 # 6 <@> mapstart lK >=5.017002
466 # 7 <|> mapwhile(other->8)[t9] lK
468 # 9 <;> nextstate(main 500 (eval 22):1) v:{
471 # c <#> gv[*getkey] s/EARLYCV
472 # d <1> entersub[t5] lKS/TARG
478 # i <1> rv2hv[t2] lKRM*
479 # j <2> aassign[t10] KS/COM_AGG
480 # k <1> leavesub[1 ref] K/REFC,1
482 # 1 <;> nextstate(main 501 (eval 22):1) v:{
486 # 5 <1> rv2av[t3] lKM/1
487 # 6 <@> mapstart lK* < 5.017002
488 # 6 <@> mapstart lK >=5.017002
489 # 7 <|> mapwhile(other->8)[t4] lK
491 # 9 <;> nextstate(main 500 (eval 22):1) v:{
494 # c <$> gv(*getkey) s/EARLYCV
495 # d <1> entersub[t2] lKS/TARG
501 # i <1> rv2hv[t1] lKRM*
502 # j <2> aassign[t5] KS/COM_AGG
503 # k <1> leavesub[1 ref] K/REFC,1
506 checkOptree ( name => '%h=(); for $_(@a){$h{getkey($_)} = $_}',
507 code => '%h=(); for $_(@a){$h{getkey($_)} = $_}',
509 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
510 # 1 <;> nextstate(main 505 (eval 24):1) v
514 # 5 <1> rv2hv[t2] lKRM*
515 # 6 <2> aassign[t3] vKS
516 # 7 <;> nextstate(main 506 (eval 24):1) v:{
519 # a <1> rv2av[t6] sKRM/1
522 # d <{> enteriter(next->o last->r redo->e) KS/DEF
524 # q <|> and(other->e) K/1
525 # e <;> nextstate(main 505 (eval 24):1) v:{
531 # k <#> gv[*getkey] s/EARLYCV
532 # l <1> entersub[t10] sKS/TARG
533 # m <2> helem sKRM*/2
534 # n <2> sassign vKS/2
537 # r <2> leaveloop KP/2
538 # s <1> leavesub[1 ref] K/REFC,1
540 # 1 <;> nextstate(main 505 (eval 24):1) v
544 # 5 <1> rv2hv[t1] lKRM*
545 # 6 <2> aassign[t2] vKS
546 # 7 <;> nextstate(main 506 (eval 24):1) v:{
549 # a <1> rv2av[t3] sKRM/1
552 # d <{> enteriter(next->o last->r redo->e) KS/DEF
554 # q <|> and(other->e) K/1
555 # e <;> nextstate(main 505 (eval 24):1) v:{
561 # k <$> gv(*getkey) s/EARLYCV
562 # l <1> entersub[t4] sKS/TARG
563 # m <2> helem sKRM*/2
564 # n <2> sassign vKS/2
567 # r <2> leaveloop KP/2
568 # s <1> leavesub[1 ref] K/REFC,1
571 checkOptree ( name => 'map $_+42, 10..20',
572 code => 'map $_+42, 10..20',
574 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
575 # 1 <;> nextstate(main 497 (eval 20):1) v
580 # 6 <|> mapwhile(other->7)[t5] K
582 # 8 <$> const[IV 42] s
585 # a <1> leavesub[1 ref] K/REFC,1
587 # 1 <;> nextstate(main 511 (eval 26):1) v
592 # 6 <|> mapwhile(other->7)[t4] K
594 # 8 <$> const(IV 42) s
597 # a <1> leavesub[1 ref] K/REFC,1
602 checkOptree ( name => '-e use constant j => qq{junk}; print j',
603 prog => 'use constant j => qq{junk}; print j',
605 strip_open_hints => 1,
606 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
608 # 2 <;> nextstate(main 71 -e:1) v:>,<,%,{
610 # 4 <$> const[PV "junk"] s* < 5.017002
611 # 4 <$> const[PV "junk"] s*/FOLD >=5.017002
613 # 6 <@> leave[1 ref] vKP/REFC
616 # 2 <;> nextstate(main 71 -e:1) v:>,<,%,{
618 # 4 <$> const(PV "junk") s* < 5.017002
619 # 4 <$> const(PV "junk") s*/FOLD >=5.017002
621 # 6 <@> leave[1 ref] vKP/REFC
624 pass("rpeep - return \$x at end of sub");
626 checkOptree ( name => '-exec sub { return 1 }',
627 code => sub { return 1 },
629 strip_open_hints => 1,
630 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
631 # 1 <;> nextstate(main 1 -e:1) v:>,<,%
632 # 2 <$> const[IV 1] s
633 # 3 <1> leavesub[1 ref] K/REFC,1
635 # 1 <;> nextstate(main 1 -e:1) v:>,<,%
636 # 2 <$> const(IV 1) s
637 # 3 <1> leavesub[1 ref] K/REFC,1
640 pass("rpeep - if ($a || $b)");
642 checkOptree ( name => 'if ($a || $b) { } return 1',
643 code => 'if ($a || $b) { } return 1',
645 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
646 # 1 <;> nextstate(main 997 (eval 15):1) v
648 # 3 <|> or(other->4) sK/1
650 # 5 <|> and(other->6) vK/1
652 # 7 <;> nextstate(main 997 (eval 15):1) v
653 # 8 <$> const[IV 1] s
654 # 9 <1> leavesub[1 ref] K/REFC,1
656 # 1 <;> nextstate(main 997 (eval 15):1) v
658 # 3 <|> or(other->4) sK/1
660 # 5 <|> and(other->6) vK/1
662 # 7 <;> nextstate(main 3 (eval 3):1) v
663 # 8 <$> const(IV 1) s
664 # 9 <1> leavesub[1 ref] K/REFC,1
667 pass("rpeep - unless ($a && $b)");
669 checkOptree ( name => 'unless ($a && $b) { } return 1',
670 code => 'unless ($a && $b) { } return 1',
672 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
673 # 1 <;> nextstate(main 997 (eval 15):1) v
675 # 3 <|> and(other->4) sK/1
677 # 5 <|> or(other->6) vK/1
679 # 7 <;> nextstate(main 997 (eval 15):1) v
680 # 8 <$> const[IV 1] s
681 # 9 <1> leavesub[1 ref] K/REFC,1
683 # 1 <;> nextstate(main 997 (eval 15):1) v
685 # 3 <|> and(other->4) sK/1
687 # 5 <|> or(other->6) vK/1
689 # 7 <;> nextstate(main 3 (eval 3):1) v
690 # 8 <$> const(IV 1) s
691 # 9 <1> leavesub[1 ref] K/REFC,1
694 pass("rpeep - my $a; my @b; my %c; print 'f'");
696 checkOptree ( name => 'my $a; my @b; my %c; return 1',
697 code => 'my $a; my @b; my %c; return 1',
699 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
700 # 1 <;> nextstate(main 991 (eval 17):1) v
701 # 2 <0> padrange[$a:991,994; @b:992,994; %c:993,994] vM/LVINTRO,range=3
702 # 3 <;> nextstate(main 994 (eval 17):1) v:{
703 # 4 <$> const[IV 1] s
704 # 5 <1> leavesub[1 ref] K/REFC,1
706 # 1 <;> nextstate(main 991 (eval 17):1) v
707 # 2 <0> padrange[$a:991,994; @b:992,994; %c:993,994] vM/LVINTRO,range=3
708 # 3 <;> nextstate(main 994 (eval 17):1) v:{
709 # 4 <$> const(IV 1) s
710 # 5 <1> leavesub[1 ref] K/REFC,1
715 #######################################################################
717 checkOptree ( name => '-exec sub a { print (shift) ? "foo" : "bar" }',
718 code => sub { print (shift) ? "foo" : "bar" },
720 expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
721 insert threaded reference here
723 insert non-threaded reference here