6 if (($Config::Config{'extensions'} !~ /\bB\b/) ){
7 print "1..0 # Skip -- Perl configured without B module\n";
16 my $tests = 18; # not counting those in the __DATA__ section
19 my $deparse = B::Deparse->new();
20 isa_ok($deparse, 'B::Deparse', 'instantiate a B::Deparse object');
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};
36 die "# In \U$what\E code reason:\n# $meta{$what}\n$@";
44 die "Missing name in test $_" unless defined $desc;
47 # Like this to avoid needing a label SKIP:
48 Test::More->builder->skip($meta{skip});
52 my ($input, $expected);
53 if (/(.*)\n>>>>\n(.*)/s) {
54 ($input, $expected) = ($1, $2);
57 ($input, $expected) = ($_, $_);
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);
64 ($hint_bits, $warning_bits, $hinthash) = ($^H, ${^WARNING_BITS}, \%^H);
66 $deparse->ambient_pragmas (
67 hint_bits => $hint_bits,
68 warning_bits => $warning_bits,
74 is($@, "", "compilation of $desc");
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*\}$';
83 local $::TODO = $meta{todo};
84 like($deparsed, qr/$regex/, $desc);
88 use constant 'c', 'stuff';
89 is((eval "sub ".$deparse->coderef2text(\&c))->(), 'stuff',
90 'the subroutine generated by use constant deparses');
93 is($deparse->coderef2text(sub{(-1) ** $a }), "{\n (-1) ** \$a;\n}",
94 'anon sub capturing an external lexical');
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');
102 my $path = join " ", map { qq["-I$_"] } @INC;
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'
111 BEGIN { $^I = ".bak"; }
113 BEGIN { $/ = "\n"; $\ = "\n"; }
114 LINE: while (defined($_ = <ARGV>)) {
116 our(@F) = split(' ', $_, 0);
121 'command line flags deparse as BEGIN blocks setting control variables');
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");
128 #Re: perlbug #35857, patch #24505
129 #handle warnings::register-ed packages properly.
130 package B::Deparse::Wrapper;
133 use warnings::register;
135 my $deparser = B::Deparse->new();
136 return $deparser->coderef2text(shift);
140 use overload '0+' => sub { 42 };
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/;
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');
158 my $x=sub { ++$q,++$p };
160 eval <<EOFCODE and test($x);
164 use warnings::register;
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 ::::::");
183 my $code = <<"EOCODE";
185 our \$\x{1e1f}\x{14d}\x{14d};
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';
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");
200 *CORE::GLOBAL::require = sub {
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");
215 is($deparse->coderef2text(sub{ print "@{*}" }),
218 }>, 'curly around to interpolate "@{*}"');
219 is($deparse->coderef2text(sub{ print "@{-}" }),
222 }>, 'no need to curly around to interpolate "@-"');
224 # Strict hints in %^H are mercilessly suppressed
226 `$^X $path "-MO=Deparse" -e "use strict; print;" 2>&1`;
227 unlike($a, qr/BEGIN/,
228 "Deparse does not emit strict hh hints");
230 # ambient_pragmas should not mess with strict settings.
232 skip "requires 5.11", 1 unless $] >= 5.011;
235 # Clear out all hints
238 new B::Deparse -> ambient_pragmas(strict => 'all');
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';
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');
257 done_testing($tests);
263 # Constants in a block
270 # Lexical and simple arithmetic
272 ++$test and $test /= 2;
275 $test /= 2 if ++$test;
282 my $test = sub : lvalue {
290 my $test = sub : method {
296 # block with continue
304 # lexical and package scalars
308 # lexical and package arrays
312 # lexical and package hashes
318 $_ .= <ARGV> . <$foo>;
321 my $foo = "Ab\x{100}\200\x{200}\237Cd\000Ef\x{1000}\cA\x{2000}\cZ";
339 # 2 arguments in a 3 argument for
340 for (my $i = 0; $i < 2;) {
345 for (my $i = 0; $i < 2; ++$i) {
349 # 3 argument for again
350 for (my $i = 0; $i < 2; ++$i) {
356 while ($i) { my $z = 1; } continue { $i = 99; }
359 foreach my $i (1, 2) {
371 foreach my $i (1, 2) {
376 foreach my $i (1, 2) {
381 foreach our $i (1, 2) {
385 # foreach with my and our
387 foreach our $i (1, 2) {
393 print reverse sort(@x);
397 print((sort {$b cmp $a} @x));
399 # reverse sort with block
401 print((reverse sort {$b <=> $a} @x));
405 print $_ foreach (reverse @a);
407 # foreach reverse (not inplace)
409 print $_ foreach (reverse 1, 2..5);
413 @ary = split(' ', 'foo', 0);
419 do { my $x = 1; $x };
421 # <20061012113037.GJ25805@c4.convolution.nl>
440 sub foo {} foo & & & foo;
445 # variables as method names
448 'Foo'->$bar('orz') = 'a stranger stranger than before';
450 # constants as method names
453 # constants as method names without ()
456 # [perl #47359] "indirect" method call notation
460 foo{@bar}1 xor foo();
463 (foo { @bar } 1)->foo;
464 (foo { @bar } 1), foo();
465 foo { @bar } 1 xor foo();
467 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
468 # CONTEXT use feature ':5.10';
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
477 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
478 # say with use 5.10.0
486 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
487 # say with use feature ':5.10';
491 use feature 'say', 'state', 'switch';
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
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
512 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
513 # CONTEXT use feature ':5.15';
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
522 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
523 # __SUB__ with use 5.15.0
531 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
532 # __SUB__ with use feature ':5.15';
536 use feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval';
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
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
557 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
558 # CONTEXT use feature ':5.10';
562 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
563 # CONTEXT use feature ':5.10';
564 # state var assignment
566 my $y = (state $x = 42);
569 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
570 # CONTEXT use feature ':5.10';
571 # state vars in anonymous subroutines
578 # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
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;
588 # Anonymous arrays and hashes, and references to them
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
598 when ('bar') { continue; }
599 when ($_ ~~ 'quux') { continue; }
603 # conditions in elsifs (regression in change #33710 which fixed bug #37302)
606 elsif ($a and $b) { x(); }
607 elsif ($a or $b) { x(); }
610 # interpolation in regexps
614 # TODO new undocumented cpan-bug #33708
618 # TODO hash constants not yet fixed
620 use constant H => { "#" => 1 }; H->{"#"}
622 # TODO optimized away 0 not yet fixed
624 foreach my $i (@_) { 0 }
626 # tests with not, not optimized
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;
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;
646 # tests with not, optimized
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;
669 x() unless $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;
681 # tests that should be constant folded
685 x() if GLIPP && GLIPP;
686 x() if !GLIPP || 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() }
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
738 if ($a == 1) { x() } elsif ($b == 2) { z() }
739 if (do { foo(); GLIPP }) { x() }
740 if (do { $a++; GLIPP }) { x() }
743 if ($a == 1) { x(); } elsif ($b == 2) { z(); }
744 if (do { foo(); GLIPP }) { x(); }
745 if (do { ++$a; GLIPP }) { x(); }
747 # TODO constant deparsing has been backed out for 5.12
748 # tests for deparsing constants
751 # TODO constant deparsing has been backed out for 5.12
752 # tests for deparsing imported constants
755 # TODO constant deparsing has been backed out for 5.12
756 # tests for deparsing re-exported constants
759 # TODO constant deparsing has been backed out for 5.12
760 # tests for deparsing imported constants that got deleted from the original namespace
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
769 eval '@Fcntl::O_EXCL = qw/affe tiger/;';
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;
788 # TODO Subsets of warnings could be encoded textually, rather than as bitflips.
789 # subsets of warnings
790 no warnings 'deprecated';
793 # TODO Better test for CPAN #33708 - the deparsed code has different behaviour
809 # SKIP ?$] > 5.013006 && ":= is now a syntax error"
810 # := treated as an empty attribute list
817 # : = empty attribute list
836 # #71870 Use of uninitialized value in bitwise and B::Deparse
838 @a = split(/foo/, $s, 0);
840 @a = split(/$r/, $s, 0);
843 # package declaration before label
866 "foo" =~ (1 ? /foo/ : /bar/);
867 "foo" =~ (1 ? y/foo// : /bar/);
868 "foo" =~ (1 ? y/foo//r : /bar/);
869 "foo" =~ (1 ? s/foo// : /bar/);
871 'foo' =~ ($_ =~ /foo/);
872 'foo' =~ ($_ =~ tr/fo//);
873 'foo' =~ ($_ =~ tr/fo//r);
874 'foo' =~ ($_ =~ s/foo//);
876 # The fix for [perl #20444] broke this.
879 # [perl #81424] match against aelemfast_lex
886 # [perl #91318] /regexp/applaud
888 print /a/aa, s/b/c/aa;
893 use feature "unicode_strings";
906 print /a/aa, s/b/c/aa;
911 use feature 'unicode_strings';
915 BEGIN { $^H{'reflags'} = '0';
916 $^H{'reflags_charset'} = '2'; }
925 # Test @threadsv_names under 5005threads
944 # readpipe with complex expression
950 # aelemfast for a lexical
954 # feature features without feature
969 # feature features when feature has been disabled by use VERSION
970 use feature (sprintf(":%vd", $^V));
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));
1017 use feature ':default';
1032 use feature 'current_sub', 'evalbytes';
1038 no feature 'unicode_strings';
1041 use feature 'current_sub', 'evalbytes';
1044 use feature ':default';
1047 use feature ':5.12';
1049 no feature 'unicode_strings';
1052 # $#- $#+ $#{%} etc.
1054 @x = ($#{`}, $#{~}, $#{!}, $#{@}, $#{$}, $#{%}, $#{^}, $#{&}, $#{*});
1055 @x = ($#{(}, $#{)}, $#{[}, $#{{}, $#{]}, $#{}}, $#{'}, $#{"}, $#{,});
1056 @x = ($#{<}, $#{.}, $#{>}, $#{/}, $#{?}, $#{=}, $#+, $#{\}, $#{|}, $#-);
1057 @x = ($#{;}, $#{:});
1059 # ${#} interpolated (the first line magically disables the warning)
1063 # [perl #86060] $( $| $) in regexps need braces
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'));
1078 print sort(foo('bar'));
1081 substr(my $a, 0, 0) = (foo(), bar());
1084 # This following line works around an unfixed bug that we are not trying to
1086 # CONTEXT BEGIN { $^H{a} = "b"; delete $^H{a} } # make %^H localised
1088 BEGIN { $^H{'foo'} = undef; }
1090 BEGIN { $^H{'bar'} = undef; }
1092 BEGIN { $^H{'baz'} = undef; }
1100 BEGIN { $^H{q[']} = '('; }
1103 # This following line works around an unfixed bug that we are not trying to
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'; }
1109 BEGIN { $^H{'b'} = 'a'; delete $^H{'a'}; }
1114 # [perl #47361] do({}) and do +{} (variants of do-file)
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;
1136 () = (require 'foo') + 3;
1137 () = (require foo) + 3;
1138 () = (CORE::dump 1) + 3;
1149 # [perl #97476] not() *does* follow the llafr
1150 $_ = ($a xor not +($1 || 2) ** 2);
1152 # Precedence conundrums with argument-less function calls
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';
1167 # [perl #63558] open local(*FH)
1169 pipe local *FH, local *FH;
1171 # [perl #91416] open "string"
1184 () = 'open'->[0]; #aelemfast
1189 () = 'open'->[$_]; #aelem
1194 () = 'open'->{0}; #helem
1215 () = 'a::]b'->{'0'};
1217 # [perl #74740] -(f()) vs -f()
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' }
1228 $_ = 'foo' foreach (my($foo1, $bar1, $baz1));
1229 foreach $_ (my($foo2, $bar2, $baz2)) {
1232 foreach my $i (my($foo3, $bar3, $baz3)) {
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 { }
1242 foreach $_ (1 .. 3) {
1248 foreach $_ (1 .. 3) {
1254 foreach my $i (1 .. 3) {
1260 foreach my $i (1 .. 3) {