This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
04d64df683f5cc40e8711260b17f554166c130f5
[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 = 17; # 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 done_testing($tests);
247
248 __DATA__
249 # A constant
250 1;
251 ####
252 # Constants in a block
253 {
254     no warnings;
255     '???';
256     2;
257 }
258 ####
259 # Lexical and simple arithmetic
260 my $test;
261 ++$test and $test /= 2;
262 >>>>
263 my $test;
264 $test /= 2 if ++$test;
265 ####
266 # list x
267 -((1, 2) x 2);
268 ####
269 # lvalue sub
270 {
271     my $test = sub : lvalue {
272         my $x;
273     }
274     ;
275 }
276 ####
277 # method
278 {
279     my $test = sub : method {
280         my $x;
281     }
282     ;
283 }
284 ####
285 # block with continue
286 {
287     234;
288 }
289 continue {
290     123;
291 }
292 ####
293 # lexical and package scalars
294 my $x;
295 print $main::x;
296 ####
297 # lexical and package arrays
298 my @x;
299 print $main::x[1];
300 ####
301 # lexical and package hashes
302 my %x;
303 $x{warn()};
304 ####
305 # <>
306 my $foo;
307 $_ .= <ARGV> . <$foo>;
308 ####
309 # \x{}
310 my $foo = "Ab\x{100}\200\x{200}\237Cd\000Ef\x{1000}\cA\x{2000}\cZ";
311 ####
312 # s///e
313 s/x/'y';/e;
314 ####
315 # block
316 { my $x; }
317 ####
318 # while 1
319 while (1) { my $k; }
320 ####
321 # trailing for
322 my ($x,@a);
323 $x=1 for @a;
324 >>>>
325 my($x, @a);
326 $x = 1 foreach (@a);
327 ####
328 # 2 arguments in a 3 argument for
329 for (my $i = 0; $i < 2;) {
330     my $z = 1;
331 }
332 ####
333 # 3 argument for
334 for (my $i = 0; $i < 2; ++$i) {
335     my $z = 1;
336 }
337 ####
338 # 3 argument for again
339 for (my $i = 0; $i < 2; ++$i) {
340     my $z = 1;
341 }
342 ####
343 # while/continue
344 my $i;
345 while ($i) { my $z = 1; } continue { $i = 99; }
346 ####
347 # foreach with my
348 foreach my $i (1, 2) {
349     my $z = 1;
350 }
351 ####
352 # foreach
353 my $i;
354 foreach $i (1, 2) {
355     my $z = 1;
356 }
357 ####
358 # foreach, 2 mys
359 my $i;
360 foreach my $i (1, 2) {
361     my $z = 1;
362 }
363 ####
364 # foreach
365 foreach my $i (1, 2) {
366     my $z = 1;
367 }
368 ####
369 # foreach with our
370 foreach our $i (1, 2) {
371     my $z = 1;
372 }
373 ####
374 # foreach with my and our
375 my $i;
376 foreach our $i (1, 2) {
377     my $z = 1;
378 }
379 ####
380 # reverse sort
381 my @x;
382 print reverse sort(@x);
383 ####
384 # sort with cmp
385 my @x;
386 print((sort {$b cmp $a} @x));
387 ####
388 # reverse sort with block
389 my @x;
390 print((reverse sort {$b <=> $a} @x));
391 ####
392 # foreach reverse
393 our @a;
394 print $_ foreach (reverse @a);
395 ####
396 # foreach reverse (not inplace)
397 our @a;
398 print $_ foreach (reverse 1, 2..5);
399 ####
400 # bug #38684
401 our @ary;
402 @ary = split(' ', 'foo', 0);
403 ####
404 # bug #40055
405 do { () }; 
406 ####
407 # bug #40055
408 do { my $x = 1; $x }; 
409 ####
410 # <20061012113037.GJ25805@c4.convolution.nl>
411 my $f = sub {
412     +{[]};
413 } ;
414 ####
415 # bug #43010
416 '!@$%'->();
417 ####
418 # bug #43010
419 ::();
420 ####
421 # bug #43010
422 '::::'->();
423 ####
424 # bug #43010
425 &::::;
426 ####
427 # [perl #77172]
428 package rt77172;
429 sub foo {} foo & & & foo;
430 >>>>
431 package rt77172;
432 foo(&{&} & foo());
433 ####
434 # variables as method names
435 my $bar;
436 'Foo'->$bar('orz');
437 'Foo'->$bar('orz') = 'a stranger stranger than before';
438 ####
439 # constants as method names
440 'Foo'->bar('orz');
441 ####
442 # constants as method names without ()
443 'Foo'->bar;
444 ####
445 # [perl #47359] "indirect" method call notation
446 our @bar;
447 foo{@bar}+1,->foo;
448 (foo{@bar}+1),foo();
449 foo{@bar}1 xor foo();
450 >>>>
451 our @bar;
452 (foo { @bar } 1)->foo;
453 (foo { @bar } 1), foo();
454 foo { @bar } 1 xor foo();
455 ####
456 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
457 # CONTEXT use feature ':5.10';
458 # say
459 say 'foo';
460 ####
461 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
462 # CONTEXT use 5.10.0;
463 # say in the context of use 5.10.0
464 say 'foo';
465 ####
466 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
467 # say with use 5.10.0
468 use 5.10.0;
469 say 'foo';
470 >>>>
471 no feature;
472 use feature ':5.10';
473 say 'foo';
474 ####
475 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
476 # say with use feature ':5.10';
477 use feature ':5.10';
478 say 'foo';
479 >>>>
480 use feature 'say', 'state', 'switch';
481 say 'foo';
482 ####
483 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
484 # CONTEXT use feature ':5.10';
485 # say with use 5.10.0 in the context of use feature
486 use 5.10.0;
487 say 'foo';
488 >>>>
489 no feature;
490 use feature ':5.10';
491 say 'foo';
492 ####
493 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
494 # CONTEXT use 5.10.0;
495 # say with use feature ':5.10' in the context of use 5.10.0
496 use feature ':5.10';
497 say 'foo';
498 >>>>
499 say 'foo';
500 ####
501 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
502 # CONTEXT use feature ':5.15';
503 # __SUB__
504 __SUB__;
505 ####
506 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
507 # CONTEXT use 5.15.0;
508 # __SUB__ in the context of use 5.15.0
509 __SUB__;
510 ####
511 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
512 # __SUB__ with use 5.15.0
513 use 5.15.0;
514 __SUB__;
515 >>>>
516 no feature;
517 use feature ':5.16';
518 __SUB__;
519 ####
520 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
521 # __SUB__ with use feature ':5.15';
522 use feature ':5.15';
523 __SUB__;
524 >>>>
525 use feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval';
526 __SUB__;
527 ####
528 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
529 # CONTEXT use feature ':5.15';
530 # __SUB__ with use 5.15.0 in the context of use feature
531 use 5.15.0;
532 __SUB__;
533 >>>>
534 no feature;
535 use feature ':5.16';
536 __SUB__;
537 ####
538 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
539 # CONTEXT use 5.15.0;
540 # __SUB__ with use feature ':5.15' in the context of use 5.15.0
541 use feature ':5.15';
542 __SUB__;
543 >>>>
544 __SUB__;
545 ####
546 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
547 # CONTEXT use feature ':5.10';
548 # state vars
549 state $x = 42;
550 ####
551 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
552 # CONTEXT use feature ':5.10';
553 # state var assignment
554 {
555     my $y = (state $x = 42);
556 }
557 ####
558 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
559 # CONTEXT use feature ':5.10';
560 # state vars in anonymous subroutines
561 $a = sub {
562     state $x;
563     return $x++;
564 }
565 ;
566 ####
567 # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
568 # each @array;
569 each @ARGV;
570 each @$a;
571 ####
572 # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
573 # keys @array; values @array
574 keys @$a if keys @ARGV;
575 values @ARGV if values @$a;
576 ####
577 # Anonymous arrays and hashes, and references to them
578 my $a = {};
579 my $b = \{};
580 my $c = [];
581 my $d = \[];
582 ####
583 # SKIP ?$] < 5.010 && "smartmatch and given/when not implemented on this Perl version"
584 # CONTEXT use feature ':5.10';
585 # implicit smartmatch in given/when
586 given ('foo') {
587     when ('bar') { continue; }
588     when ($_ ~~ 'quux') { continue; }
589     default { 0; }
590 }
591 ####
592 # conditions in elsifs (regression in change #33710 which fixed bug #37302)
593 if ($a) { x(); }
594 elsif ($b) { x(); }
595 elsif ($a and $b) { x(); }
596 elsif ($a or $b) { x(); }
597 else { x(); }
598 ####
599 # interpolation in regexps
600 my($y, $t);
601 /x${y}z$t/;
602 ####
603 # TODO new undocumented cpan-bug #33708
604 # cpan-bug #33708
605 %{$_ || {}}
606 ####
607 # TODO hash constants not yet fixed
608 # cpan-bug #33708
609 use constant H => { "#" => 1 }; H->{"#"}
610 ####
611 # TODO optimized away 0 not yet fixed
612 # cpan-bug #33708
613 foreach my $i (@_) { 0 }
614 ####
615 # tests with not, not optimized
616 my $c;
617 x() unless $a;
618 x() if not $a and $b;
619 x() if $a and not $b;
620 x() unless not $a and $b;
621 x() unless $a and not $b;
622 x() if not $a or $b;
623 x() if $a or not $b;
624 x() unless not $a or $b;
625 x() unless $a or not $b;
626 x() if $a and not $b and $c;
627 x() if not $a and $b and not $c;
628 x() unless $a and not $b and $c;
629 x() unless not $a and $b and not $c;
630 x() if $a or not $b or $c;
631 x() if not $a or $b or not $c;
632 x() unless $a or not $b or $c;
633 x() unless not $a or $b or not $c;
634 ####
635 # tests with not, optimized
636 my $c;
637 x() if not $a;
638 x() unless not $a;
639 x() if not $a and not $b;
640 x() unless not $a and not $b;
641 x() if not $a or not $b;
642 x() unless not $a or not $b;
643 x() if not $a and not $b and $c;
644 x() unless not $a and not $b and $c;
645 x() if not $a or not $b or $c;
646 x() unless not $a or not $b or $c;
647 x() if not $a and not $b and not $c;
648 x() unless not $a and not $b and not $c;
649 x() if not $a or not $b or not $c;
650 x() unless not $a or not $b or not $c;
651 x() unless not $a or not $b or not $c;
652 >>>>
653 my $c;
654 x() unless $a;
655 x() if $a;
656 x() unless $a or $b;
657 x() if $a or $b;
658 x() unless $a and $b;
659 x() if $a and $b;
660 x() if not $a || $b and $c;
661 x() unless not $a || $b and $c;
662 x() if not $a && $b or $c;
663 x() unless not $a && $b or $c;
664 x() unless $a or $b or $c;
665 x() if $a or $b or $c;
666 x() unless $a and $b and $c;
667 x() if $a and $b and $c;
668 x() unless not $a && $b && $c;
669 ####
670 # tests that should be constant folded
671 x() if 1;
672 x() if GLIPP;
673 x() if !GLIPP;
674 x() if GLIPP && GLIPP;
675 x() if !GLIPP || GLIPP;
676 x() if do { GLIPP };
677 x() if do { no warnings 'void'; 5; GLIPP };
678 x() if do { !GLIPP };
679 if (GLIPP) { x() } else { z() }
680 if (!GLIPP) { x() } else { z() }
681 if (GLIPP) { x() } elsif (GLIPP) { z() }
682 if (!GLIPP) { x() } elsif (GLIPP) { z() }
683 if (GLIPP) { x() } elsif (!GLIPP) { z() }
684 if (!GLIPP) { x() } elsif (!GLIPP) { z() }
685 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (GLIPP) { t() }
686 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
687 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
688 >>>>
689 x();
690 x();
691 '???';
692 x();
693 x();
694 x();
695 x();
696 do {
697     '???'
698 };
699 do {
700     x()
701 };
702 do {
703     z()
704 };
705 do {
706     x()
707 };
708 do {
709     z()
710 };
711 do {
712     x()
713 };
714 '???';
715 do {
716     t()
717 };
718 '???';
719 !1;
720 ####
721 # TODO constant deparsing has been backed out for 5.12
722 # XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
723 # tests that shouldn't be constant folded
724 # It might be fundamentally impossible to make this work on ithreads, in which
725 # case the TODO should become a SKIP
726 x() if $a;
727 if ($a == 1) { x() } elsif ($b == 2) { z() }
728 if (do { foo(); GLIPP }) { x() }
729 if (do { $a++; GLIPP }) { x() }
730 >>>>
731 x() if $a;
732 if ($a == 1) { x(); } elsif ($b == 2) { z(); }
733 if (do { foo(); GLIPP }) { x(); }
734 if (do { ++$a; GLIPP }) { x(); }
735 ####
736 # TODO constant deparsing has been backed out for 5.12
737 # tests for deparsing constants
738 warn PI;
739 ####
740 # TODO constant deparsing has been backed out for 5.12
741 # tests for deparsing imported constants
742 warn O_TRUNC;
743 ####
744 # TODO constant deparsing has been backed out for 5.12
745 # tests for deparsing re-exported constants
746 warn O_CREAT;
747 ####
748 # TODO constant deparsing has been backed out for 5.12
749 # tests for deparsing imported constants that got deleted from the original namespace
750 warn O_APPEND;
751 ####
752 # TODO constant deparsing has been backed out for 5.12
753 # XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
754 # tests for deparsing constants which got turned into full typeglobs
755 # It might be fundamentally impossible to make this work on ithreads, in which
756 # case the TODO should become a SKIP
757 warn O_EXCL;
758 eval '@Fcntl::O_EXCL = qw/affe tiger/;';
759 warn O_EXCL;
760 ####
761 # TODO constant deparsing has been backed out for 5.12
762 # tests for deparsing of blessed constant with overloaded numification
763 warn OVERLOADED_NUMIFICATION;
764 ####
765 # strict
766 no strict;
767 print $x;
768 use strict 'vars';
769 print $main::x;
770 use strict 'subs';
771 print $main::x;
772 use strict 'refs';
773 print $main::x;
774 no strict 'vars';
775 $x;
776 ####
777 # TODO Subsets of warnings could be encoded textually, rather than as bitflips.
778 # subsets of warnings
779 no warnings 'deprecated';
780 my $x;
781 ####
782 # TODO Better test for CPAN #33708 - the deparsed code has different behaviour
783 # CPAN #33708
784 use strict;
785 no warnings;
786
787 foreach (0..3) {
788     my $x = 2;
789     {
790         my $x if 0;
791         print ++$x, "\n";
792     }
793 }
794 ####
795 # no attribute list
796 my $pi = 4;
797 ####
798 # SKIP ?$] > 5.013006 && ":= is now a syntax error"
799 # := treated as an empty attribute list
800 no warnings;
801 my $pi := 4;
802 >>>>
803 no warnings;
804 my $pi = 4;
805 ####
806 # : = empty attribute list
807 my $pi : = 4;
808 >>>>
809 my $pi = 4;
810 ####
811 # in place sort
812 our @a;
813 my @b;
814 @a = sort @a;
815 @b = sort @b;
816 ();
817 ####
818 # in place reverse
819 our @a;
820 my @b;
821 @a = reverse @a;
822 @b = reverse @b;
823 ();
824 ####
825 # #71870 Use of uninitialized value in bitwise and B::Deparse
826 my($r, $s, @a);
827 @a = split(/foo/, $s, 0);
828 $r = qr/foo/;
829 @a = split(/$r/, $s, 0);
830 ();
831 ####
832 # package declaration before label
833 {
834     package Foo;
835     label: print 123;
836 }
837 ####
838 # shift optimisation
839 shift;
840 >>>>
841 shift();
842 ####
843 # shift optimisation
844 shift @_;
845 ####
846 # shift optimisation
847 pop;
848 >>>>
849 pop();
850 ####
851 # shift optimisation
852 pop @_;
853 ####
854 #[perl #20444]
855 "foo" =~ (1 ? /foo/ : /bar/);
856 "foo" =~ (1 ? y/foo// : /bar/);
857 "foo" =~ (1 ? y/foo//r : /bar/);
858 "foo" =~ (1 ? s/foo// : /bar/);
859 >>>>
860 'foo' =~ ($_ =~ /foo/);
861 'foo' =~ ($_ =~ tr/fo//);
862 'foo' =~ ($_ =~ tr/fo//r);
863 'foo' =~ ($_ =~ s/foo//);
864 ####
865 # The fix for [perl #20444] broke this.
866 'foo' =~ do { () };
867 ####
868 # [perl #81424] match against aelemfast_lex
869 my @s;
870 print /$s[1]/;
871 ####
872 # /$#a/
873 print /$#main::a/;
874 ####
875 # [perl #91318] /regexp/applaud
876 print /a/a, s/b/c/a;
877 print /a/aa, s/b/c/aa;
878 print /a/p, s/b/c/p;
879 print /a/l, s/b/c/l;
880 print /a/u, s/b/c/u;
881 {
882     use feature "unicode_strings";
883     print /a/d, s/b/c/d;
884 }
885 {
886     use re "/u";
887     print /a/d, s/b/c/d;
888 }
889 {
890     use 5.012;
891     print /a/d, s/b/c/d;
892 }
893 >>>>
894 print /a/a, s/b/c/a;
895 print /a/aa, s/b/c/aa;
896 print /a/p, s/b/c/p;
897 print /a/l, s/b/c/l;
898 print /a/u, s/b/c/u;
899 {
900     use feature 'unicode_strings';
901     print /a/d, s/b/c/d;
902 }
903 {
904     BEGIN { $^H{'reflags'}         = '0';
905             $^H{'reflags_charset'} = '2'; }
906     print /a/d, s/b/c/d;
907 }
908 {
909     no feature;
910     use feature ':5.12';
911     print /a/d, s/b/c/d;
912 }
913 ####
914 # Test @threadsv_names under 5005threads
915 foreach $' (1, 2) {
916     sleep $';
917 }
918 ####
919 # y///r
920 tr/a/b/r;
921 ####
922 # y/uni/code/
923 tr/\x{345}/\x{370}/;
924 ####
925 # [perl #90898]
926 <a,>;
927 ####
928 # [perl #91008]
929 each $@;
930 keys $~;
931 values $!;
932 ####
933 # readpipe with complex expression
934 readpipe $a + $b;
935 ####
936 # aelemfast
937 $b::a[0] = 1;
938 ####
939 # aelemfast for a lexical
940 my @a;
941 $a[0] = 1;
942 ####
943 # feature features without feature
944 CORE::state $x;
945 CORE::say $x;
946 CORE::given ($x) {
947     CORE::when (3) {
948         continue;
949     }
950     CORE::default {
951         CORE::break;
952     }
953 }
954 CORE::evalbytes '';
955 () = CORE::__SUB__;
956 () = CORE::fc $x;
957 ####
958 # feature features when feature has been disabled by use VERSION
959 use feature (sprintf(":%vd", $^V));
960 use 1;
961 CORE::state $x;
962 CORE::say $x;
963 CORE::given ($x) {
964     CORE::when (3) {
965         continue;
966     }
967     CORE::default {
968         CORE::break;
969     }
970 }
971 CORE::evalbytes '';
972 () = CORE::__SUB__;
973 >>>>
974 CORE::state $x;
975 CORE::say $x;
976 CORE::given ($x) {
977     CORE::when (3) {
978         continue;
979     }
980     CORE::default {
981         CORE::break;
982     }
983 }
984 CORE::evalbytes '';
985 () = CORE::__SUB__;
986 ####
987 # (the above test with CONTEXT, and the output is equivalent but different)
988 # CONTEXT use feature ':5.10';
989 # feature features when feature has been disabled by use VERSION
990 use feature (sprintf(":%vd", $^V));
991 use 1;
992 CORE::state $x;
993 CORE::say $x;
994 CORE::given ($x) {
995     CORE::when (3) {
996         continue;
997     }
998     CORE::default {
999         CORE::break;
1000     }
1001 }
1002 CORE::evalbytes '';
1003 () = CORE::__SUB__;
1004 >>>>
1005 no feature;
1006 use feature ':default';
1007 CORE::state $x;
1008 CORE::say $x;
1009 CORE::given ($x) {
1010     CORE::when (3) {
1011         continue;
1012     }
1013     CORE::default {
1014         CORE::break;
1015     }
1016 }
1017 CORE::evalbytes '';
1018 () = CORE::__SUB__;
1019 ####
1020 # Feature hints
1021 use feature 'current_sub', 'evalbytes';
1022 print;
1023 use 1;
1024 print;
1025 use 5.014;
1026 print;
1027 no feature 'unicode_strings';
1028 print;
1029 >>>>
1030 use feature 'current_sub', 'evalbytes';
1031 print $_;
1032 no feature;
1033 use feature ':default';
1034 print $_;
1035 no feature;
1036 use feature ':5.12';
1037 print $_;
1038 no feature 'unicode_strings';
1039 print $_;
1040 ####
1041 # $#- $#+ $#{%} etc.
1042 my @x;
1043 @x = ($#{`}, $#{~}, $#{!}, $#{@}, $#{$}, $#{%}, $#{^}, $#{&}, $#{*});
1044 @x = ($#{(}, $#{)}, $#{[}, $#{{}, $#{]}, $#{}}, $#{'}, $#{"}, $#{,});
1045 @x = ($#{<}, $#{.}, $#{>}, $#{/}, $#{?}, $#{=}, $#+, $#{\}, $#{|}, $#-);
1046 @x = ($#{;}, $#{:});
1047 ####
1048 # ${#} interpolated (the first line magically disables the warning)
1049 () = *#;
1050 () = "${#}a";
1051 ####
1052 # [perl #86060] $( $| $) in regexps need braces
1053 /${(}/;
1054 /${|}/;
1055 /${)}/;
1056 /${(}${|}${)}/;
1057 ####
1058 # ()[...]
1059 my(@a) = ()[()];
1060 ####
1061 # sort(foo(bar))
1062 # sort(foo(bar)) is interpreted as sort &foo(bar)
1063 # sort foo(bar) is interpreted as sort foo bar
1064 # parentheses are not optional in this case
1065 print sort(foo('bar'));
1066 >>>>
1067 print sort(foo('bar'));
1068 ####
1069 # substr assignment
1070 substr(my $a, 0, 0) = (foo(), bar());
1071 $a++;
1072 ####
1073 # This following line works around an unfixed bug that we are not trying to 
1074 # test for here:
1075 # CONTEXT BEGIN { $^H{a} = "b"; delete $^H{a} } # make %^H localised
1076 # hint hash
1077 BEGIN { $^H{'foo'} = undef; }
1078 {
1079  BEGIN { $^H{'bar'} = undef; }
1080  {
1081   BEGIN { $^H{'baz'} = undef; }
1082   {
1083    print $_;
1084   }
1085   print $_;
1086  }
1087  print $_;
1088 }
1089 BEGIN { $^H{q[']} = '('; }
1090 print $_;
1091 ####
1092 # This following line works around an unfixed bug that we are not trying to 
1093 # test for here:
1094 # CONTEXT BEGIN { $^H{a} = "b"; delete $^H{a} } # make %^H localised
1095 # hint hash changes that serialise the same way with sort %hh
1096 BEGIN { $^H{'a'} = 'b'; }
1097 {
1098  BEGIN { $^H{'b'} = 'a'; delete $^H{'a'}; }
1099  print $_;
1100 }
1101 print $_;
1102 ####
1103 # [perl #47361] do({}) and do +{} (variants of do-file)
1104 do({});
1105 do +{};
1106 sub foo::do {}
1107 package foo;
1108 CORE::do({});
1109 CORE::do +{};
1110 >>>>
1111 do({});
1112 do({});
1113 package foo;
1114 CORE::do({});
1115 CORE::do({});
1116 ####
1117 # [perl #77096] functions that do not follow the llafr
1118 () = (return 1) + time;
1119 () = (return ($1 + $2) * $3) + time;
1120 () = (return ($a xor $b)) + time;
1121 () = (do 'file') + time;
1122 () = (do ($1 + $2) * $3) + time;
1123 () = (do ($1 xor $2)) + time;
1124 () = (goto 1) + 3;
1125 () = (require 'foo') + 3;
1126 () = (require foo) + 3;
1127 () = (CORE::dump 1) + 3;
1128 () = (last 1) + 3;
1129 () = (next 1) + 3;
1130 () = (redo 1) + 3;
1131 () = (-R $_) + 3;
1132 () = (-W $_) + 3;
1133 () = (-X $_) + 3;
1134 () = (-r $_) + 3;
1135 () = (-w $_) + 3;
1136 () = (-x $_) + 3;
1137 ####
1138 # [perl #97476] not() *does* follow the llafr
1139 $_ = ($a xor not +($1 || 2) ** 2);
1140 ####
1141 # Precedence conundrums with argument-less function calls
1142 () = (eof) + 1;
1143 () = (return) + 1;
1144 () = (return, 1);
1145 () = warn;
1146 () = warn() + 1;
1147 () = setpgrp() + 1;
1148 ####
1149 # [perl #63558] open local(*FH)
1150 open local *FH;
1151 pipe local *FH, local *FH;
1152 ####
1153 # [perl #91416] open "string"
1154 open 'open';
1155 open '####';
1156 open '^A';
1157 open "\ca";
1158 >>>>
1159 open *open;
1160 open '####';
1161 open '^A';
1162 open *^A;
1163 ####
1164 # "string"->[] ->{}
1165 no strict 'vars';
1166 () = 'open'->[0]; #aelemfast
1167 () = '####'->[0];
1168 () = '^A'->[0];
1169 () = "\ca"->[0];
1170 () = 'a::]b'->[0];
1171 () = 'open'->[$_]; #aelem
1172 () = '####'->[$_];
1173 () = '^A'->[$_];
1174 () = "\ca"->[$_];
1175 () = 'a::]b'->[$_];
1176 () = 'open'->{0}; #helem
1177 () = '####'->{0};
1178 () = '^A'->{0};
1179 () = "\ca"->{0};
1180 () = 'a::]b'->{0};
1181 >>>>
1182 no strict 'vars';
1183 () = $open[0];
1184 () = '####'->[0];
1185 () = '^A'->[0];
1186 () = $^A[0];
1187 () = 'a::]b'->[0];
1188 () = $open[$_];
1189 () = '####'->[$_];
1190 () = '^A'->[$_];
1191 () = $^A[$_];
1192 () = 'a::]b'->[$_];
1193 () = $open{'0'};
1194 () = '####'->{'0'};
1195 () = '^A'->{'0'};
1196 () = $^A{'0'};
1197 () = 'a::]b'->{'0'};
1198 ####
1199 # [perl #74740] -(f()) vs -f()
1200 $_ = -(f());
1201 ####
1202 # require <binop>
1203 require 'a' . $1;
1204 ####
1205 #[perl #30504] foreach-my postfix/prefix difference
1206 $_ = 'foo' foreach my ($foo1, $bar1, $baz1);
1207 foreach (my ($foo2, $bar2, $baz2)) { $_ = 'foo' }
1208 foreach my $i (my ($foo3, $bar3, $baz3)) { $i = 'foo' }
1209 >>>>
1210 $_ = 'foo' foreach (my($foo1, $bar1, $baz1));
1211 foreach $_ (my($foo2, $bar2, $baz2)) {
1212     $_ = 'foo';
1213 }
1214 foreach my $i (my($foo3, $bar3, $baz3)) {
1215     $i = 'foo';
1216 }
1217 ####
1218 #[perl #108224] foreach with continue block
1219 foreach (1 .. 3) { print } continue { print "\n" }
1220 foreach (1 .. 3) { } continue { }
1221 foreach my $i (1 .. 3) { print $i } continue { print "\n" }
1222 foreach my $i (1 .. 3) { } continue { }
1223 >>>>
1224 foreach $_ (1 .. 3) {
1225     print $_;
1226 }
1227 continue {
1228     print "\n";
1229 }
1230 foreach $_ (1 .. 3) {
1231     ();
1232 }
1233 continue {
1234     ();
1235 }
1236 foreach my $i (1 .. 3) {
1237     print $i;
1238 }
1239 continue {
1240     print "\n";
1241 }
1242 foreach my $i (1 .. 3) {
1243     ();
1244 }
1245 continue {
1246     ();
1247 }