This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
2d750505e636ccaacfb1f72f91d013f06e83c543
[perl5.git] / dist / B-Deparse / t / deparse.t
1 #!./perl
2
3 BEGIN {
4     unshift @INC, 't';
5     require Config;
6     if (($Config::Config{'extensions'} !~ /\bB\b/) ){
7         print "1..0 # Skip -- Perl configured without B module\n";
8         exit 0;
9     }
10 }
11
12 use warnings;
13 use strict;
14 use Test::More;
15
16 my $tests = 18; # not counting those in the __DATA__ section
17
18 use B::Deparse;
19 my $deparse = B::Deparse->new();
20 isa_ok($deparse, 'B::Deparse', 'instantiate a B::Deparse object');
21
22 $/ = "\n####\n";
23 while (<DATA>) {
24     chomp;
25     $tests ++;
26     # This code is pinched from the t/lib/common.pl for TODO.
27     # It's not clear how to avoid duplication
28     my %meta = (context => '');
29     foreach my $what (qw(skip todo context)) {
30         s/^#\s*\U$what\E\s*(.*)\n//m and $meta{$what} = $1;
31         # If the SKIP reason starts ? then it's taken as a code snippet to
32         # evaluate. This provides the flexibility to have conditional SKIPs
33         if ($meta{$what} && $meta{$what} =~ s/^\?//) {
34             my $temp = eval $meta{$what};
35             if ($@) {
36                 die "# In \U$what\E code reason:\n# $meta{$what}\n$@";
37             }
38             $meta{$what} = $temp;
39         }
40     }
41
42     s/^\s*#\s*(.*)$//mg;
43     my $desc = $1;
44     die "Missing name in test $_" unless defined $desc;
45
46     if ($meta{skip}) {
47         # Like this to avoid needing a label SKIP:
48         Test::More->builder->skip($meta{skip});
49         next;
50     }
51
52     my ($input, $expected);
53     if (/(.*)\n>>>>\n(.*)/s) {
54         ($input, $expected) = ($1, $2);
55     }
56     else {
57         ($input, $expected) = ($_, $_);
58     }
59
60     my $coderef = eval "$meta{context};\n" . <<'EOC' . "sub {$input}";
61 # Tell B::Deparse about our ambient pragmas
62 my ($hint_bits, $warning_bits, $hinthash);
63 BEGIN {
64     ($hint_bits, $warning_bits, $hinthash) = ($^H, ${^WARNING_BITS}, \%^H);
65 }
66 $deparse->ambient_pragmas (
67     hint_bits    => $hint_bits,
68     warning_bits => $warning_bits,
69     '%^H'        => $hinthash,
70 );
71 EOC
72
73     if ($@) {
74         is($@, "", "compilation of $desc");
75     }
76     else {
77         my $deparsed = $deparse->coderef2text( $coderef );
78         my $regex = $expected;
79         $regex =~ s/(\S+)/\Q$1/g;
80         $regex =~ s/\s+/\\s+/g;
81         $regex = '^\{\s*' . $regex . '\s*\}$';
82
83         local $::TODO = $meta{todo};
84         like($deparsed, qr/$regex/, $desc);
85     }
86 }
87
88 use constant 'c', 'stuff';
89 is((eval "sub ".$deparse->coderef2text(\&c))->(), 'stuff',
90    'the subroutine generated by use constant deparses');
91
92 my $a = 0;
93 is($deparse->coderef2text(sub{(-1) ** $a }), "{\n    (-1) ** \$a;\n}",
94    'anon sub capturing an external lexical');
95
96 use constant cr => ['hello'];
97 my $string = "sub " . $deparse->coderef2text(\&cr);
98 my $val = (eval $string)->() or diag $string;
99 is(ref($val), 'ARRAY', 'constant array references deparse');
100 is($val->[0], 'hello', 'and return the correct value');
101
102 my $path = join " ", map { qq["-I$_"] } @INC;
103
104 $a = `$^X $path "-MO=Deparse" -anlwi.bak -e 1 2>&1`;
105 $a =~ s/-e syntax OK\n//g;
106 $a =~ s/.*possible typo.*\n//;     # Remove warning line
107 $a =~ s/.*-i used with no filenames.*\n//;      # Remove warning line
108 $a =~ s{\\340\\242}{\\s} if (ord("\\") == 224); # EBCDIC, cp 1047 or 037
109 $a =~ s{\\274\\242}{\\s} if (ord("\\") == 188); # $^O eq 'posix-bc'
110 $b = <<'EOF';
111 BEGIN { $^I = ".bak"; }
112 BEGIN { $^W = 1; }
113 BEGIN { $/ = "\n"; $\ = "\n"; }
114 LINE: while (defined($_ = <ARGV>)) {
115     chomp $_;
116     our(@F) = split(' ', $_, 0);
117     '???';
118 }
119 EOF
120 is($a, $b,
121    'command line flags deparse as BEGIN blocks setting control variables');
122
123 $a = `$^X $path "-MO=Deparse" -e "use constant PI => 4" 2>&1`;
124 $a =~ s/-e syntax OK\n//g;
125 is($a, "use constant ('PI', 4);\n",
126    "Proxy Constant Subroutines must not show up as (incorrect) prototypes");
127
128 #Re: perlbug #35857, patch #24505
129 #handle warnings::register-ed packages properly.
130 package B::Deparse::Wrapper;
131 use strict;
132 use warnings;
133 use warnings::register;
134 sub getcode {
135    my $deparser = B::Deparse->new();
136    return $deparser->coderef2text(shift);
137 }
138
139 package Moo;
140 use overload '0+' => sub { 42 };
141
142 package main;
143 use strict;
144 use warnings;
145 use constant GLIPP => 'glipp';
146 use constant PI => 4;
147 use constant OVERLOADED_NUMIFICATION => bless({}, 'Moo');
148 use Fcntl qw/O_TRUNC O_APPEND O_EXCL/;
149 BEGIN { delete $::Fcntl::{O_APPEND}; }
150 use POSIX qw/O_CREAT/;
151 sub test {
152    my $val = shift;
153    my $res = B::Deparse::Wrapper::getcode($val);
154    like($res, qr/use warnings/,
155         '[perl #35857] [PATCH] B::Deparse doesnt handle warnings register properly');
156 }
157 my ($q,$p);
158 my $x=sub { ++$q,++$p };
159 test($x);
160 eval <<EOFCODE and test($x);
161    package bar;
162    use strict;
163    use warnings;
164    use warnings::register;
165    package main;
166    1
167 EOFCODE
168
169 # Exotic sub declarations
170 $a = `$^X $path "-MO=Deparse" -e "sub ::::{}sub ::::::{}" 2>&1`;
171 $a =~ s/-e syntax OK\n//g;
172 is($a, <<'EOCODG', "sub :::: and sub ::::::");
173 sub :::: {
174     
175 }
176 sub :::::: {
177     
178 }
179 EOCODG
180
181 # [perl #33752]
182 {
183   my $code = <<"EOCODE";
184 {
185     our \$\x{1e1f}\x{14d}\x{14d};
186 }
187 EOCODE
188   my $deparsed
189    = $deparse->coderef2text(eval "sub { our \$\x{1e1f}\x{14d}\x{14d} }" );
190   s/$ \n//x for $deparsed, $code;
191   is $deparsed, $code, 'our $funny_Unicode_chars';
192 }
193
194 # [perl #62500]
195 $a =
196   `$^X $path "-MO=Deparse" -e "BEGIN{*CORE::GLOBAL::require=sub{1}}" 2>&1`;
197 $a =~ s/-e syntax OK\n//g;
198 is($a, <<'EOCODF', "CORE::GLOBAL::require override causing panick");
199 sub BEGIN {
200     *CORE::GLOBAL::require = sub {
201         1;
202     }
203     ;
204 }
205 EOCODF
206
207 # [perl #91384]
208 $a =
209   `$^X $path "-MO=Deparse" -e "BEGIN{*Acme::Acme:: = *Acme::}" 2>&1`;
210 like($a, qr/-e syntax OK/,
211     "Deparse does not hang when traversing stash circularities");
212
213 # [perl #93990]
214 @* = ();
215 is($deparse->coderef2text(sub{ print "@{*}" }),
216 q<{
217     print "@{*}";
218 }>, 'curly around to interpolate "@{*}"');
219 is($deparse->coderef2text(sub{ print "@{-}" }),
220 q<{
221     print "@-";
222 }>, 'no need to curly around to interpolate "@-"');
223
224 # Strict hints in %^H are mercilessly suppressed
225 $a =
226   `$^X $path "-MO=Deparse" -e "use strict; print;" 2>&1`;
227 unlike($a, qr/BEGIN/,
228     "Deparse does not emit strict hh hints");
229
230 # ambient_pragmas should not mess with strict settings.
231 SKIP: {
232     skip "requires 5.11", 1 unless $] >= 5.011;
233     eval q`
234         BEGIN {
235             # Clear out all hints
236             %^H = ();
237             $^H = 0;
238             new B::Deparse -> ambient_pragmas(strict => 'all');
239         }
240         use 5.011;  # should enable strict
241         ok !eval '$do_noT_create_a_variable_with_this_name = 1',
242           'ambient_pragmas do not mess with compiling scope';
243    `;
244 }
245
246 # multiple statements on format lines
247 $a = `$^X $path "-MO=Deparse" -e "format =" -e "\@" -e "x();z()" -e. 2>&1`;
248 $a =~ s/-e syntax OK\n//g;
249 is($a, <<'EOCODH', 'multiple statements on format lines');
250 format STDOUT =
251 @
252 x(); z()
253 .
254 EOCODH
255
256
257 done_testing($tests);
258
259 __DATA__
260 # A constant
261 1;
262 ####
263 # Constants in a block
264 {
265     no warnings;
266     '???';
267     2;
268 }
269 ####
270 # Lexical and simple arithmetic
271 my $test;
272 ++$test and $test /= 2;
273 >>>>
274 my $test;
275 $test /= 2 if ++$test;
276 ####
277 # list x
278 -((1, 2) x 2);
279 ####
280 # lvalue sub
281 {
282     my $test = sub : lvalue {
283         my $x;
284     }
285     ;
286 }
287 ####
288 # method
289 {
290     my $test = sub : method {
291         my $x;
292     }
293     ;
294 }
295 ####
296 # block with continue
297 {
298     234;
299 }
300 continue {
301     123;
302 }
303 ####
304 # lexical and package scalars
305 my $x;
306 print $main::x;
307 ####
308 # lexical and package arrays
309 my @x;
310 print $main::x[1];
311 ####
312 # lexical and package hashes
313 my %x;
314 $x{warn()};
315 ####
316 # <>
317 my $foo;
318 $_ .= <ARGV> . <$foo>;
319 ####
320 # \x{}
321 my $foo = "Ab\x{100}\200\x{200}\237Cd\000Ef\x{1000}\cA\x{2000}\cZ";
322 ####
323 # s///e
324 s/x/'y';/e;
325 ####
326 # block
327 { my $x; }
328 ####
329 # while 1
330 while (1) { my $k; }
331 ####
332 # trailing for
333 my ($x,@a);
334 $x=1 for @a;
335 >>>>
336 my($x, @a);
337 $x = 1 foreach (@a);
338 ####
339 # 2 arguments in a 3 argument for
340 for (my $i = 0; $i < 2;) {
341     my $z = 1;
342 }
343 ####
344 # 3 argument for
345 for (my $i = 0; $i < 2; ++$i) {
346     my $z = 1;
347 }
348 ####
349 # 3 argument for again
350 for (my $i = 0; $i < 2; ++$i) {
351     my $z = 1;
352 }
353 ####
354 # while/continue
355 my $i;
356 while ($i) { my $z = 1; } continue { $i = 99; }
357 ####
358 # foreach with my
359 foreach my $i (1, 2) {
360     my $z = 1;
361 }
362 ####
363 # foreach
364 my $i;
365 foreach $i (1, 2) {
366     my $z = 1;
367 }
368 ####
369 # foreach, 2 mys
370 my $i;
371 foreach my $i (1, 2) {
372     my $z = 1;
373 }
374 ####
375 # foreach
376 foreach my $i (1, 2) {
377     my $z = 1;
378 }
379 ####
380 # foreach with our
381 foreach our $i (1, 2) {
382     my $z = 1;
383 }
384 ####
385 # foreach with my and our
386 my $i;
387 foreach our $i (1, 2) {
388     my $z = 1;
389 }
390 ####
391 # reverse sort
392 my @x;
393 print reverse sort(@x);
394 ####
395 # sort with cmp
396 my @x;
397 print((sort {$b cmp $a} @x));
398 ####
399 # reverse sort with block
400 my @x;
401 print((reverse sort {$b <=> $a} @x));
402 ####
403 # foreach reverse
404 our @a;
405 print $_ foreach (reverse @a);
406 ####
407 # foreach reverse (not inplace)
408 our @a;
409 print $_ foreach (reverse 1, 2..5);
410 ####
411 # bug #38684
412 our @ary;
413 @ary = split(' ', 'foo', 0);
414 ####
415 # bug #40055
416 do { () }; 
417 ####
418 # bug #40055
419 do { my $x = 1; $x }; 
420 ####
421 # <20061012113037.GJ25805@c4.convolution.nl>
422 my $f = sub {
423     +{[]};
424 } ;
425 ####
426 # bug #43010
427 '!@$%'->();
428 ####
429 # bug #43010
430 ::();
431 ####
432 # bug #43010
433 '::::'->();
434 ####
435 # bug #43010
436 &::::;
437 ####
438 # [perl #77172]
439 package rt77172;
440 sub foo {} foo & & & foo;
441 >>>>
442 package rt77172;
443 foo(&{&} & foo());
444 ####
445 # variables as method names
446 my $bar;
447 'Foo'->$bar('orz');
448 'Foo'->$bar('orz') = 'a stranger stranger than before';
449 ####
450 # constants as method names
451 'Foo'->bar('orz');
452 ####
453 # constants as method names without ()
454 'Foo'->bar;
455 ####
456 # [perl #47359] "indirect" method call notation
457 our @bar;
458 foo{@bar}+1,->foo;
459 (foo{@bar}+1),foo();
460 foo{@bar}1 xor foo();
461 >>>>
462 our @bar;
463 (foo { @bar } 1)->foo;
464 (foo { @bar } 1), foo();
465 foo { @bar } 1 xor foo();
466 ####
467 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
468 # CONTEXT use feature ':5.10';
469 # say
470 say 'foo';
471 ####
472 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
473 # CONTEXT use 5.10.0;
474 # say in the context of use 5.10.0
475 say 'foo';
476 ####
477 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
478 # say with use 5.10.0
479 use 5.10.0;
480 say 'foo';
481 >>>>
482 no feature;
483 use feature ':5.10';
484 say 'foo';
485 ####
486 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
487 # say with use feature ':5.10';
488 use feature ':5.10';
489 say 'foo';
490 >>>>
491 use feature 'say', 'state', 'switch';
492 say 'foo';
493 ####
494 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
495 # CONTEXT use feature ':5.10';
496 # say with use 5.10.0 in the context of use feature
497 use 5.10.0;
498 say 'foo';
499 >>>>
500 no feature;
501 use feature ':5.10';
502 say 'foo';
503 ####
504 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
505 # CONTEXT use 5.10.0;
506 # say with use feature ':5.10' in the context of use 5.10.0
507 use feature ':5.10';
508 say 'foo';
509 >>>>
510 say 'foo';
511 ####
512 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
513 # CONTEXT use feature ':5.15';
514 # __SUB__
515 __SUB__;
516 ####
517 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
518 # CONTEXT use 5.15.0;
519 # __SUB__ in the context of use 5.15.0
520 __SUB__;
521 ####
522 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
523 # __SUB__ with use 5.15.0
524 use 5.15.0;
525 __SUB__;
526 >>>>
527 no feature;
528 use feature ':5.16';
529 __SUB__;
530 ####
531 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
532 # __SUB__ with use feature ':5.15';
533 use feature ':5.15';
534 __SUB__;
535 >>>>
536 use feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval';
537 __SUB__;
538 ####
539 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
540 # CONTEXT use feature ':5.15';
541 # __SUB__ with use 5.15.0 in the context of use feature
542 use 5.15.0;
543 __SUB__;
544 >>>>
545 no feature;
546 use feature ':5.16';
547 __SUB__;
548 ####
549 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
550 # CONTEXT use 5.15.0;
551 # __SUB__ with use feature ':5.15' in the context of use 5.15.0
552 use feature ':5.15';
553 __SUB__;
554 >>>>
555 __SUB__;
556 ####
557 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
558 # CONTEXT use feature ':5.10';
559 # state vars
560 state $x = 42;
561 ####
562 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
563 # CONTEXT use feature ':5.10';
564 # state var assignment
565 {
566     my $y = (state $x = 42);
567 }
568 ####
569 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
570 # CONTEXT use feature ':5.10';
571 # state vars in anonymous subroutines
572 $a = sub {
573     state $x;
574     return $x++;
575 }
576 ;
577 ####
578 # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
579 # each @array;
580 each @ARGV;
581 each @$a;
582 ####
583 # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
584 # keys @array; values @array
585 keys @$a if keys @ARGV;
586 values @ARGV if values @$a;
587 ####
588 # Anonymous arrays and hashes, and references to them
589 my $a = {};
590 my $b = \{};
591 my $c = [];
592 my $d = \[];
593 ####
594 # SKIP ?$] < 5.010 && "smartmatch and given/when not implemented on this Perl version"
595 # CONTEXT use feature ':5.10';
596 # implicit smartmatch in given/when
597 given ('foo') {
598     when ('bar') { continue; }
599     when ($_ ~~ 'quux') { continue; }
600     default { 0; }
601 }
602 ####
603 # conditions in elsifs (regression in change #33710 which fixed bug #37302)
604 if ($a) { x(); }
605 elsif ($b) { x(); }
606 elsif ($a and $b) { x(); }
607 elsif ($a or $b) { x(); }
608 else { x(); }
609 ####
610 # interpolation in regexps
611 my($y, $t);
612 /x${y}z$t/;
613 ####
614 # TODO new undocumented cpan-bug #33708
615 # cpan-bug #33708
616 %{$_ || {}}
617 ####
618 # TODO hash constants not yet fixed
619 # cpan-bug #33708
620 use constant H => { "#" => 1 }; H->{"#"}
621 ####
622 # TODO optimized away 0 not yet fixed
623 # cpan-bug #33708
624 foreach my $i (@_) { 0 }
625 ####
626 # tests with not, not optimized
627 my $c;
628 x() unless $a;
629 x() if not $a and $b;
630 x() if $a and not $b;
631 x() unless not $a and $b;
632 x() unless $a and not $b;
633 x() if not $a or $b;
634 x() if $a or not $b;
635 x() unless not $a or $b;
636 x() unless $a or not $b;
637 x() if $a and not $b and $c;
638 x() if not $a and $b and not $c;
639 x() unless $a and not $b and $c;
640 x() unless not $a and $b and not $c;
641 x() if $a or not $b or $c;
642 x() if not $a or $b or not $c;
643 x() unless $a or not $b or $c;
644 x() unless not $a or $b or not $c;
645 ####
646 # tests with not, optimized
647 my $c;
648 x() if not $a;
649 x() unless not $a;
650 x() if not $a and not $b;
651 x() unless not $a and not $b;
652 x() if not $a or not $b;
653 x() unless not $a or not $b;
654 x() if not $a and not $b and $c;
655 x() unless not $a and not $b and $c;
656 x() if not $a or not $b or $c;
657 x() unless not $a or not $b or $c;
658 x() if not $a and not $b and not $c;
659 x() unless not $a and not $b and not $c;
660 x() if not $a or not $b or not $c;
661 x() unless not $a or not $b or not $c;
662 x() unless not $a or not $b or not $c;
663 >>>>
664 my $c;
665 x() unless $a;
666 x() if $a;
667 x() unless $a or $b;
668 x() if $a or $b;
669 x() unless $a and $b;
670 x() if $a and $b;
671 x() if not $a || $b and $c;
672 x() unless not $a || $b and $c;
673 x() if not $a && $b or $c;
674 x() unless not $a && $b or $c;
675 x() unless $a or $b or $c;
676 x() if $a or $b or $c;
677 x() unless $a and $b and $c;
678 x() if $a and $b and $c;
679 x() unless not $a && $b && $c;
680 ####
681 # tests that should be constant folded
682 x() if 1;
683 x() if GLIPP;
684 x() if !GLIPP;
685 x() if GLIPP && GLIPP;
686 x() if !GLIPP || GLIPP;
687 x() if do { GLIPP };
688 x() if do { no warnings 'void'; 5; GLIPP };
689 x() if do { !GLIPP };
690 if (GLIPP) { x() } else { z() }
691 if (!GLIPP) { x() } else { z() }
692 if (GLIPP) { x() } elsif (GLIPP) { z() }
693 if (!GLIPP) { x() } elsif (GLIPP) { z() }
694 if (GLIPP) { x() } elsif (!GLIPP) { z() }
695 if (!GLIPP) { x() } elsif (!GLIPP) { z() }
696 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (GLIPP) { t() }
697 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
698 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
699 >>>>
700 x();
701 x();
702 '???';
703 x();
704 x();
705 x();
706 x();
707 do {
708     '???'
709 };
710 do {
711     x()
712 };
713 do {
714     z()
715 };
716 do {
717     x()
718 };
719 do {
720     z()
721 };
722 do {
723     x()
724 };
725 '???';
726 do {
727     t()
728 };
729 '???';
730 !1;
731 ####
732 # TODO constant deparsing has been backed out for 5.12
733 # XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
734 # tests that shouldn't be constant folded
735 # It might be fundamentally impossible to make this work on ithreads, in which
736 # case the TODO should become a SKIP
737 x() if $a;
738 if ($a == 1) { x() } elsif ($b == 2) { z() }
739 if (do { foo(); GLIPP }) { x() }
740 if (do { $a++; GLIPP }) { x() }
741 >>>>
742 x() if $a;
743 if ($a == 1) { x(); } elsif ($b == 2) { z(); }
744 if (do { foo(); GLIPP }) { x(); }
745 if (do { ++$a; GLIPP }) { x(); }
746 ####
747 # TODO constant deparsing has been backed out for 5.12
748 # tests for deparsing constants
749 warn PI;
750 ####
751 # TODO constant deparsing has been backed out for 5.12
752 # tests for deparsing imported constants
753 warn O_TRUNC;
754 ####
755 # TODO constant deparsing has been backed out for 5.12
756 # tests for deparsing re-exported constants
757 warn O_CREAT;
758 ####
759 # TODO constant deparsing has been backed out for 5.12
760 # tests for deparsing imported constants that got deleted from the original namespace
761 warn O_APPEND;
762 ####
763 # TODO constant deparsing has been backed out for 5.12
764 # XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
765 # tests for deparsing constants which got turned into full typeglobs
766 # It might be fundamentally impossible to make this work on ithreads, in which
767 # case the TODO should become a SKIP
768 warn O_EXCL;
769 eval '@Fcntl::O_EXCL = qw/affe tiger/;';
770 warn O_EXCL;
771 ####
772 # TODO constant deparsing has been backed out for 5.12
773 # tests for deparsing of blessed constant with overloaded numification
774 warn OVERLOADED_NUMIFICATION;
775 ####
776 # strict
777 no strict;
778 print $x;
779 use strict 'vars';
780 print $main::x;
781 use strict 'subs';
782 print $main::x;
783 use strict 'refs';
784 print $main::x;
785 no strict 'vars';
786 $x;
787 ####
788 # TODO Subsets of warnings could be encoded textually, rather than as bitflips.
789 # subsets of warnings
790 no warnings 'deprecated';
791 my $x;
792 ####
793 # TODO Better test for CPAN #33708 - the deparsed code has different behaviour
794 # CPAN #33708
795 use strict;
796 no warnings;
797
798 foreach (0..3) {
799     my $x = 2;
800     {
801         my $x if 0;
802         print ++$x, "\n";
803     }
804 }
805 ####
806 # no attribute list
807 my $pi = 4;
808 ####
809 # SKIP ?$] > 5.013006 && ":= is now a syntax error"
810 # := treated as an empty attribute list
811 no warnings;
812 my $pi := 4;
813 >>>>
814 no warnings;
815 my $pi = 4;
816 ####
817 # : = empty attribute list
818 my $pi : = 4;
819 >>>>
820 my $pi = 4;
821 ####
822 # in place sort
823 our @a;
824 my @b;
825 @a = sort @a;
826 @b = sort @b;
827 ();
828 ####
829 # in place reverse
830 our @a;
831 my @b;
832 @a = reverse @a;
833 @b = reverse @b;
834 ();
835 ####
836 # #71870 Use of uninitialized value in bitwise and B::Deparse
837 my($r, $s, @a);
838 @a = split(/foo/, $s, 0);
839 $r = qr/foo/;
840 @a = split(/$r/, $s, 0);
841 ();
842 ####
843 # package declaration before label
844 {
845     package Foo;
846     label: print 123;
847 }
848 ####
849 # shift optimisation
850 shift;
851 >>>>
852 shift();
853 ####
854 # shift optimisation
855 shift @_;
856 ####
857 # shift optimisation
858 pop;
859 >>>>
860 pop();
861 ####
862 # shift optimisation
863 pop @_;
864 ####
865 #[perl #20444]
866 "foo" =~ (1 ? /foo/ : /bar/);
867 "foo" =~ (1 ? y/foo// : /bar/);
868 "foo" =~ (1 ? y/foo//r : /bar/);
869 "foo" =~ (1 ? s/foo// : /bar/);
870 >>>>
871 'foo' =~ ($_ =~ /foo/);
872 'foo' =~ ($_ =~ tr/fo//);
873 'foo' =~ ($_ =~ tr/fo//r);
874 'foo' =~ ($_ =~ s/foo//);
875 ####
876 # The fix for [perl #20444] broke this.
877 'foo' =~ do { () };
878 ####
879 # [perl #81424] match against aelemfast_lex
880 my @s;
881 print /$s[1]/;
882 ####
883 # /$#a/
884 print /$#main::a/;
885 ####
886 # [perl #91318] /regexp/applaud
887 print /a/a, s/b/c/a;
888 print /a/aa, s/b/c/aa;
889 print /a/p, s/b/c/p;
890 print /a/l, s/b/c/l;
891 print /a/u, s/b/c/u;
892 {
893     use feature "unicode_strings";
894     print /a/d, s/b/c/d;
895 }
896 {
897     use re "/u";
898     print /a/d, s/b/c/d;
899 }
900 {
901     use 5.012;
902     print /a/d, s/b/c/d;
903 }
904 >>>>
905 print /a/a, s/b/c/a;
906 print /a/aa, s/b/c/aa;
907 print /a/p, s/b/c/p;
908 print /a/l, s/b/c/l;
909 print /a/u, s/b/c/u;
910 {
911     use feature 'unicode_strings';
912     print /a/d, s/b/c/d;
913 }
914 {
915     BEGIN { $^H{'reflags'}         = '0';
916             $^H{'reflags_charset'} = '2'; }
917     print /a/d, s/b/c/d;
918 }
919 {
920     no feature;
921     use feature ':5.12';
922     print /a/d, s/b/c/d;
923 }
924 ####
925 # Test @threadsv_names under 5005threads
926 foreach $' (1, 2) {
927     sleep $';
928 }
929 ####
930 # y///r
931 tr/a/b/r;
932 ####
933 # y/uni/code/
934 tr/\x{345}/\x{370}/;
935 ####
936 # [perl #90898]
937 <a,>;
938 ####
939 # [perl #91008]
940 each $@;
941 keys $~;
942 values $!;
943 ####
944 # readpipe with complex expression
945 readpipe $a + $b;
946 ####
947 # aelemfast
948 $b::a[0] = 1;
949 ####
950 # aelemfast for a lexical
951 my @a;
952 $a[0] = 1;
953 ####
954 # feature features without feature
955 CORE::state $x;
956 CORE::say $x;
957 CORE::given ($x) {
958     CORE::when (3) {
959         continue;
960     }
961     CORE::default {
962         CORE::break;
963     }
964 }
965 CORE::evalbytes '';
966 () = CORE::__SUB__;
967 () = CORE::fc $x;
968 ####
969 # feature features when feature has been disabled by use VERSION
970 use feature (sprintf(":%vd", $^V));
971 use 1;
972 CORE::state $x;
973 CORE::say $x;
974 CORE::given ($x) {
975     CORE::when (3) {
976         continue;
977     }
978     CORE::default {
979         CORE::break;
980     }
981 }
982 CORE::evalbytes '';
983 () = CORE::__SUB__;
984 >>>>
985 CORE::state $x;
986 CORE::say $x;
987 CORE::given ($x) {
988     CORE::when (3) {
989         continue;
990     }
991     CORE::default {
992         CORE::break;
993     }
994 }
995 CORE::evalbytes '';
996 () = CORE::__SUB__;
997 ####
998 # (the above test with CONTEXT, and the output is equivalent but different)
999 # CONTEXT use feature ':5.10';
1000 # feature features when feature has been disabled by use VERSION
1001 use feature (sprintf(":%vd", $^V));
1002 use 1;
1003 CORE::state $x;
1004 CORE::say $x;
1005 CORE::given ($x) {
1006     CORE::when (3) {
1007         continue;
1008     }
1009     CORE::default {
1010         CORE::break;
1011     }
1012 }
1013 CORE::evalbytes '';
1014 () = CORE::__SUB__;
1015 >>>>
1016 no feature;
1017 use feature ':default';
1018 CORE::state $x;
1019 CORE::say $x;
1020 CORE::given ($x) {
1021     CORE::when (3) {
1022         continue;
1023     }
1024     CORE::default {
1025         CORE::break;
1026     }
1027 }
1028 CORE::evalbytes '';
1029 () = CORE::__SUB__;
1030 ####
1031 # Feature hints
1032 use feature 'current_sub', 'evalbytes';
1033 print;
1034 use 1;
1035 print;
1036 use 5.014;
1037 print;
1038 no feature 'unicode_strings';
1039 print;
1040 >>>>
1041 use feature 'current_sub', 'evalbytes';
1042 print $_;
1043 no feature;
1044 use feature ':default';
1045 print $_;
1046 no feature;
1047 use feature ':5.12';
1048 print $_;
1049 no feature 'unicode_strings';
1050 print $_;
1051 ####
1052 # $#- $#+ $#{%} etc.
1053 my @x;
1054 @x = ($#{`}, $#{~}, $#{!}, $#{@}, $#{$}, $#{%}, $#{^}, $#{&}, $#{*});
1055 @x = ($#{(}, $#{)}, $#{[}, $#{{}, $#{]}, $#{}}, $#{'}, $#{"}, $#{,});
1056 @x = ($#{<}, $#{.}, $#{>}, $#{/}, $#{?}, $#{=}, $#+, $#{\}, $#{|}, $#-);
1057 @x = ($#{;}, $#{:});
1058 ####
1059 # ${#} interpolated (the first line magically disables the warning)
1060 () = *#;
1061 () = "${#}a";
1062 ####
1063 # [perl #86060] $( $| $) in regexps need braces
1064 /${(}/;
1065 /${|}/;
1066 /${)}/;
1067 /${(}${|}${)}/;
1068 ####
1069 # ()[...]
1070 my(@a) = ()[()];
1071 ####
1072 # sort(foo(bar))
1073 # sort(foo(bar)) is interpreted as sort &foo(bar)
1074 # sort foo(bar) is interpreted as sort foo bar
1075 # parentheses are not optional in this case
1076 print sort(foo('bar'));
1077 >>>>
1078 print sort(foo('bar'));
1079 ####
1080 # substr assignment
1081 substr(my $a, 0, 0) = (foo(), bar());
1082 $a++;
1083 ####
1084 # This following line works around an unfixed bug that we are not trying to 
1085 # test for here:
1086 # CONTEXT BEGIN { $^H{a} = "b"; delete $^H{a} } # make %^H localised
1087 # hint hash
1088 BEGIN { $^H{'foo'} = undef; }
1089 {
1090  BEGIN { $^H{'bar'} = undef; }
1091  {
1092   BEGIN { $^H{'baz'} = undef; }
1093   {
1094    print $_;
1095   }
1096   print $_;
1097  }
1098  print $_;
1099 }
1100 BEGIN { $^H{q[']} = '('; }
1101 print $_;
1102 ####
1103 # This following line works around an unfixed bug that we are not trying to 
1104 # test for here:
1105 # CONTEXT BEGIN { $^H{a} = "b"; delete $^H{a} } # make %^H localised
1106 # hint hash changes that serialise the same way with sort %hh
1107 BEGIN { $^H{'a'} = 'b'; }
1108 {
1109  BEGIN { $^H{'b'} = 'a'; delete $^H{'a'}; }
1110  print $_;
1111 }
1112 print $_;
1113 ####
1114 # [perl #47361] do({}) and do +{} (variants of do-file)
1115 do({});
1116 do +{};
1117 sub foo::do {}
1118 package foo;
1119 CORE::do({});
1120 CORE::do +{};
1121 >>>>
1122 do({});
1123 do({});
1124 package foo;
1125 CORE::do({});
1126 CORE::do({});
1127 ####
1128 # [perl #77096] functions that do not follow the llafr
1129 () = (return 1) + time;
1130 () = (return ($1 + $2) * $3) + time;
1131 () = (return ($a xor $b)) + time;
1132 () = (do 'file') + time;
1133 () = (do ($1 + $2) * $3) + time;
1134 () = (do ($1 xor $2)) + time;
1135 () = (goto 1) + 3;
1136 () = (require 'foo') + 3;
1137 () = (require foo) + 3;
1138 () = (CORE::dump 1) + 3;
1139 () = (last 1) + 3;
1140 () = (next 1) + 3;
1141 () = (redo 1) + 3;
1142 () = (-R $_) + 3;
1143 () = (-W $_) + 3;
1144 () = (-X $_) + 3;
1145 () = (-r $_) + 3;
1146 () = (-w $_) + 3;
1147 () = (-x $_) + 3;
1148 ####
1149 # [perl #97476] not() *does* follow the llafr
1150 $_ = ($a xor not +($1 || 2) ** 2);
1151 ####
1152 # Precedence conundrums with argument-less function calls
1153 () = (eof) + 1;
1154 () = (return) + 1;
1155 () = (return, 1);
1156 () = warn;
1157 () = warn() + 1;
1158 () = setpgrp() + 1;
1159 ####
1160 # loopexes have assignment prec
1161 () = (CORE::dump a) | 'b';
1162 () = (goto a) | 'b';
1163 () = (last a) | 'b';
1164 () = (next a) | 'b';
1165 () = (redo a) | 'b';
1166 ####
1167 # [perl #63558] open local(*FH)
1168 open local *FH;
1169 pipe local *FH, local *FH;
1170 ####
1171 # [perl #91416] open "string"
1172 open 'open';
1173 open '####';
1174 open '^A';
1175 open "\ca";
1176 >>>>
1177 open *open;
1178 open '####';
1179 open '^A';
1180 open *^A;
1181 ####
1182 # "string"->[] ->{}
1183 no strict 'vars';
1184 () = 'open'->[0]; #aelemfast
1185 () = '####'->[0];
1186 () = '^A'->[0];
1187 () = "\ca"->[0];
1188 () = 'a::]b'->[0];
1189 () = 'open'->[$_]; #aelem
1190 () = '####'->[$_];
1191 () = '^A'->[$_];
1192 () = "\ca"->[$_];
1193 () = 'a::]b'->[$_];
1194 () = 'open'->{0}; #helem
1195 () = '####'->{0};
1196 () = '^A'->{0};
1197 () = "\ca"->{0};
1198 () = 'a::]b'->{0};
1199 >>>>
1200 no strict 'vars';
1201 () = $open[0];
1202 () = '####'->[0];
1203 () = '^A'->[0];
1204 () = $^A[0];
1205 () = 'a::]b'->[0];
1206 () = $open[$_];
1207 () = '####'->[$_];
1208 () = '^A'->[$_];
1209 () = $^A[$_];
1210 () = 'a::]b'->[$_];
1211 () = $open{'0'};
1212 () = '####'->{'0'};
1213 () = '^A'->{'0'};
1214 () = $^A{'0'};
1215 () = 'a::]b'->{'0'};
1216 ####
1217 # [perl #74740] -(f()) vs -f()
1218 $_ = -(f());
1219 ####
1220 # require <binop>
1221 require 'a' . $1;
1222 ####
1223 #[perl #30504] foreach-my postfix/prefix difference
1224 $_ = 'foo' foreach my ($foo1, $bar1, $baz1);
1225 foreach (my ($foo2, $bar2, $baz2)) { $_ = 'foo' }
1226 foreach my $i (my ($foo3, $bar3, $baz3)) { $i = 'foo' }
1227 >>>>
1228 $_ = 'foo' foreach (my($foo1, $bar1, $baz1));
1229 foreach $_ (my($foo2, $bar2, $baz2)) {
1230     $_ = 'foo';
1231 }
1232 foreach my $i (my($foo3, $bar3, $baz3)) {
1233     $i = 'foo';
1234 }
1235 ####
1236 #[perl #108224] foreach with continue block
1237 foreach (1 .. 3) { print } continue { print "\n" }
1238 foreach (1 .. 3) { } continue { }
1239 foreach my $i (1 .. 3) { print $i } continue { print "\n" }
1240 foreach my $i (1 .. 3) { } continue { }
1241 >>>>
1242 foreach $_ (1 .. 3) {
1243     print $_;
1244 }
1245 continue {
1246     print "\n";
1247 }
1248 foreach $_ (1 .. 3) {
1249     ();
1250 }
1251 continue {
1252     ();
1253 }
1254 foreach my $i (1 .. 3) {
1255     print $i;
1256 }
1257 continue {
1258     print "\n";
1259 }
1260 foreach my $i (1 .. 3) {
1261     ();
1262 }
1263 continue {
1264     ();
1265 }