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";
326 s/x/complex_expression();/e;
341 # 2 arguments in a 3 argument for
342 for (my $i = 0; $i < 2;) {
347 for (my $i = 0; $i < 2; ++$i) {
351 # 3 argument for again
352 for (my $i = 0; $i < 2; ++$i) {
358 while ($i) { my $z = 1; } continue { $i = 99; }
361 foreach my $i (1, 2) {
373 foreach my $i (1, 2) {
378 foreach my $i (1, 2) {
383 foreach our $i (1, 2) {
387 # foreach with my and our
389 foreach our $i (1, 2) {
395 print reverse sort(@x);
399 print((sort {$b cmp $a} @x));
401 # reverse sort with block
403 print((reverse sort {$b <=> $a} @x));
407 print $_ foreach (reverse @a);
409 # foreach reverse (not inplace)
411 print $_ foreach (reverse 1, 2..5);
415 @ary = split(' ', 'foo', 0);
421 do { my $x = 1; $x };
423 # <20061012113037.GJ25805@c4.convolution.nl>
442 sub foo {} foo & & & foo;
447 # variables as method names
450 'Foo'->$bar('orz') = 'a stranger stranger than before';
452 # constants as method names
455 # constants as method names without ()
458 # [perl #47359] "indirect" method call notation
462 foo{@bar}1 xor foo();
465 (foo { @bar } 1)->foo;
466 (foo { @bar } 1), foo();
467 foo { @bar } 1 xor foo();
469 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
470 # CONTEXT use feature ':5.10';
474 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
475 # CONTEXT use 5.10.0;
476 # say in the context of use 5.10.0
479 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
480 # say with use 5.10.0
488 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
489 # say with use feature ':5.10';
493 use feature 'say', 'state', 'switch';
496 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
497 # CONTEXT use feature ':5.10';
498 # say with use 5.10.0 in the context of use feature
506 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
507 # CONTEXT use 5.10.0;
508 # say with use feature ':5.10' in the context of use 5.10.0
514 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
515 # CONTEXT use feature ':5.15';
519 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
520 # CONTEXT use 5.15.0;
521 # __SUB__ in the context of use 5.15.0
524 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
525 # __SUB__ with use 5.15.0
533 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
534 # __SUB__ with use feature ':5.15';
538 use feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval';
541 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
542 # CONTEXT use feature ':5.15';
543 # __SUB__ with use 5.15.0 in the context of use feature
551 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
552 # CONTEXT use 5.15.0;
553 # __SUB__ with use feature ':5.15' in the context of use 5.15.0
559 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
560 # CONTEXT use feature ':5.10';
564 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
565 # CONTEXT use feature ':5.10';
566 # state var assignment
568 my $y = (state $x = 42);
571 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
572 # CONTEXT use feature ':5.10';
573 # state vars in anonymous subroutines
580 # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
585 # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
586 # keys @array; values @array
587 keys @$a if keys @ARGV;
588 values @ARGV if values @$a;
590 # Anonymous arrays and hashes, and references to them
596 # SKIP ?$] < 5.010 && "smartmatch and given/when not implemented on this Perl version"
597 # CONTEXT use feature ':5.10';
598 # implicit smartmatch in given/when
600 when ('bar') { continue; }
601 when ($_ ~~ 'quux') { continue; }
605 # conditions in elsifs (regression in change #33710 which fixed bug #37302)
608 elsif ($a and $b) { x(); }
609 elsif ($a or $b) { x(); }
612 # interpolation in regexps
616 # TODO new undocumented cpan-bug #33708
620 # TODO hash constants not yet fixed
622 use constant H => { "#" => 1 }; H->{"#"}
624 # TODO optimized away 0 not yet fixed
626 foreach my $i (@_) { 0 }
628 # tests with not, not optimized
631 x() if not $a and $b;
632 x() if $a and not $b;
633 x() unless not $a and $b;
634 x() unless $a and not $b;
637 x() unless not $a or $b;
638 x() unless $a or not $b;
639 x() if $a and not $b and $c;
640 x() if not $a and $b and not $c;
641 x() unless $a and not $b and $c;
642 x() unless not $a and $b and not $c;
643 x() if $a or not $b or $c;
644 x() if not $a or $b or not $c;
645 x() unless $a or not $b or $c;
646 x() unless not $a or $b or not $c;
648 # tests with not, optimized
652 x() if not $a and not $b;
653 x() unless not $a and not $b;
654 x() if not $a or not $b;
655 x() unless not $a or not $b;
656 x() if not $a and not $b and $c;
657 x() unless not $a and not $b and $c;
658 x() if not $a or not $b or $c;
659 x() unless not $a or not $b or $c;
660 x() if not $a and not $b and not $c;
661 x() unless not $a and not $b and not $c;
662 x() if not $a or not $b or not $c;
663 x() unless not $a or not $b or not $c;
664 x() unless not $a or not $b or not $c;
671 x() unless $a and $b;
673 x() if not $a || $b and $c;
674 x() unless not $a || $b and $c;
675 x() if not $a && $b or $c;
676 x() unless not $a && $b or $c;
677 x() unless $a or $b or $c;
678 x() if $a or $b or $c;
679 x() unless $a and $b and $c;
680 x() if $a and $b and $c;
681 x() unless not $a && $b && $c;
683 # tests that should be constant folded
687 x() if GLIPP && GLIPP;
688 x() if !GLIPP || GLIPP;
690 x() if do { no warnings 'void'; 5; GLIPP };
691 x() if do { !GLIPP };
692 if (GLIPP) { x() } else { z() }
693 if (!GLIPP) { x() } else { z() }
694 if (GLIPP) { x() } elsif (GLIPP) { z() }
695 if (!GLIPP) { x() } elsif (GLIPP) { z() }
696 if (GLIPP) { x() } elsif (!GLIPP) { z() }
697 if (!GLIPP) { x() } elsif (!GLIPP) { z() }
698 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (GLIPP) { t() }
699 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
700 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
734 # TODO constant deparsing has been backed out for 5.12
735 # XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
736 # tests that shouldn't be constant folded
737 # It might be fundamentally impossible to make this work on ithreads, in which
738 # case the TODO should become a SKIP
740 if ($a == 1) { x() } elsif ($b == 2) { z() }
741 if (do { foo(); GLIPP }) { x() }
742 if (do { $a++; GLIPP }) { x() }
745 if ($a == 1) { x(); } elsif ($b == 2) { z(); }
746 if (do { foo(); GLIPP }) { x(); }
747 if (do { ++$a; GLIPP }) { x(); }
749 # TODO constant deparsing has been backed out for 5.12
750 # tests for deparsing constants
753 # TODO constant deparsing has been backed out for 5.12
754 # tests for deparsing imported constants
757 # TODO constant deparsing has been backed out for 5.12
758 # tests for deparsing re-exported constants
761 # TODO constant deparsing has been backed out for 5.12
762 # tests for deparsing imported constants that got deleted from the original namespace
765 # TODO constant deparsing has been backed out for 5.12
766 # XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
767 # tests for deparsing constants which got turned into full typeglobs
768 # It might be fundamentally impossible to make this work on ithreads, in which
769 # case the TODO should become a SKIP
771 eval '@Fcntl::O_EXCL = qw/affe tiger/;';
774 # TODO constant deparsing has been backed out for 5.12
775 # tests for deparsing of blessed constant with overloaded numification
776 warn OVERLOADED_NUMIFICATION;
790 # TODO Subsets of warnings could be encoded textually, rather than as bitflips.
791 # subsets of warnings
792 no warnings 'deprecated';
795 # TODO Better test for CPAN #33708 - the deparsed code has different behaviour
811 # SKIP ?$] > 5.013006 && ":= is now a syntax error"
812 # := treated as an empty attribute list
819 # : = empty attribute list
838 # #71870 Use of uninitialized value in bitwise and B::Deparse
840 @a = split(/foo/, $s, 0);
842 @a = split(/$r/, $s, 0);
845 # package declaration before label
868 "foo" =~ (1 ? /foo/ : /bar/);
869 "foo" =~ (1 ? y/foo// : /bar/);
870 "foo" =~ (1 ? y/foo//r : /bar/);
871 "foo" =~ (1 ? s/foo// : /bar/);
873 'foo' =~ ($_ =~ /foo/);
874 'foo' =~ ($_ =~ tr/fo//);
875 'foo' =~ ($_ =~ tr/fo//r);
876 'foo' =~ ($_ =~ s/foo//);
878 # The fix for [perl #20444] broke this.
881 # [perl #81424] match against aelemfast_lex
888 # [perl #91318] /regexp/applaud
890 print /a/aa, s/b/c/aa;
895 use feature "unicode_strings";
908 print /a/aa, s/b/c/aa;
913 use feature 'unicode_strings';
917 BEGIN { $^H{'reflags'} = '0';
918 $^H{'reflags_charset'} = '2'; }
927 # Test @threadsv_names under 5005threads
946 # readpipe with complex expression
952 # aelemfast for a lexical
956 # feature features without feature
971 # feature features when feature has been disabled by use VERSION
972 use feature (sprintf(":%vd", $^V));
1000 # (the above test with CONTEXT, and the output is equivalent but different)
1001 # CONTEXT use feature ':5.10';
1002 # feature features when feature has been disabled by use VERSION
1003 use feature (sprintf(":%vd", $^V));
1019 use feature ':default';
1034 use feature 'current_sub', 'evalbytes';
1040 no feature 'unicode_strings';
1043 use feature 'current_sub', 'evalbytes';
1046 use feature ':default';
1049 use feature ':5.12';
1051 no feature 'unicode_strings';
1054 # $#- $#+ $#{%} etc.
1056 @x = ($#{`}, $#{~}, $#{!}, $#{@}, $#{$}, $#{%}, $#{^}, $#{&}, $#{*});
1057 @x = ($#{(}, $#{)}, $#{[}, $#{{}, $#{]}, $#{}}, $#{'}, $#{"}, $#{,});
1058 @x = ($#{<}, $#{.}, $#{>}, $#{/}, $#{?}, $#{=}, $#+, $#{\}, $#{|}, $#-);
1059 @x = ($#{;}, $#{:});
1061 # ${#} interpolated (the first line magically disables the warning)
1065 # [perl #86060] $( $| $) in regexps need braces
1075 # sort(foo(bar)) is interpreted as sort &foo(bar)
1076 # sort foo(bar) is interpreted as sort foo bar
1077 # parentheses are not optional in this case
1078 print sort(foo('bar'));
1080 print sort(foo('bar'));
1083 substr(my $a, 0, 0) = (foo(), bar());
1086 # This following line works around an unfixed bug that we are not trying to
1088 # CONTEXT BEGIN { $^H{a} = "b"; delete $^H{a} } # make %^H localised
1090 BEGIN { $^H{'foo'} = undef; }
1092 BEGIN { $^H{'bar'} = undef; }
1094 BEGIN { $^H{'baz'} = undef; }
1102 BEGIN { $^H{q[']} = '('; }
1105 # This following line works around an unfixed bug that we are not trying to
1107 # CONTEXT BEGIN { $^H{a} = "b"; delete $^H{a} } # make %^H localised
1108 # hint hash changes that serialise the same way with sort %hh
1109 BEGIN { $^H{'a'} = 'b'; }
1111 BEGIN { $^H{'b'} = 'a'; delete $^H{'a'}; }
1116 # [perl #47361] do({}) and do +{} (variants of do-file)
1130 # [perl #77096] functions that do not follow the llafr
1131 () = (return 1) + time;
1132 () = (return ($1 + $2) * $3) + time;
1133 () = (return ($a xor $b)) + time;
1134 () = (do 'file') + time;
1135 () = (do ($1 + $2) * $3) + time;
1136 () = (do ($1 xor $2)) + time;
1138 () = (require 'foo') + 3;
1139 () = (require foo) + 3;
1140 () = (CORE::dump 1) + 3;
1151 # [perl #97476] not() *does* follow the llafr
1152 $_ = ($a xor not +($1 || 2) ** 2);
1154 # Precedence conundrums with argument-less function calls
1162 # loopexes have assignment prec
1163 () = (CORE::dump a) | 'b';
1164 () = (goto a) | 'b';
1165 () = (last a) | 'b';
1166 () = (next a) | 'b';
1167 () = (redo a) | 'b';
1169 # [perl #63558] open local(*FH)
1171 pipe local *FH, local *FH;
1173 # [perl #91416] open "string"
1186 () = 'open'->[0]; #aelemfast
1191 () = 'open'->[$_]; #aelem
1196 () = 'open'->{0}; #helem
1217 () = 'a::]b'->{'0'};
1219 # [perl #74740] -(f()) vs -f()
1225 #[perl #30504] foreach-my postfix/prefix difference
1226 $_ = 'foo' foreach my ($foo1, $bar1, $baz1);
1227 foreach (my ($foo2, $bar2, $baz2)) { $_ = 'foo' }
1228 foreach my $i (my ($foo3, $bar3, $baz3)) { $i = 'foo' }
1230 $_ = 'foo' foreach (my($foo1, $bar1, $baz1));
1231 foreach $_ (my($foo2, $bar2, $baz2)) {
1234 foreach my $i (my($foo3, $bar3, $baz3)) {
1238 #[perl #108224] foreach with continue block
1239 foreach (1 .. 3) { print } continue { print "\n" }
1240 foreach (1 .. 3) { } continue { }
1241 foreach my $i (1 .. 3) { print $i } continue { print "\n" }
1242 foreach my $i (1 .. 3) { } continue { }
1244 foreach $_ (1 .. 3) {
1250 foreach $_ (1 .. 3) {
1256 foreach my $i (1 .. 3) {
1262 foreach my $i (1 .. 3) {
1283 # 'my' works with padrange op
1290 ($m3, $m4) = (1, 2);
1292 my($m5, $m6) = (1, 2);
1293 my($m7, undef, $m8) = (1, 2, 3);
1294 @z = ($m7, undef, $m8);
1295 ($m7, undef, $m8) = (1, 2, 3);
1297 # 'our/local' works with padrange op
1308 ($o3, $o4) = (1, 2);
1309 local($o3, $o4) = (1, 2);
1311 @z = local($o3, $o4);
1312 our($o5, $o6) = (1, 2);
1313 our($o7, undef, $o8) = (1, 2, 3);
1314 @z = ($o7, undef, $o8);
1315 @z = local($o7, undef, $o8);
1316 ($o7, undef, $o8) = (1, 2, 3);
1317 local($o7, undef, $o8) = (1, 2, 3);
1319 # 'state' works with padrange op
1321 use feature 'state';
1328 ($s3, $s4) = (1, 2);
1330 # assignment of state lists isn't implemented yet
1331 #state($s5, $s6) = (1, 2);
1332 #state($s7, undef, $s8) = (1, 2, 3);
1333 #@z = ($s7, undef, $s8);
1334 ($s7, undef, $s8) = (1, 2, 3);
1336 # anon lists with padrange
1341 # slices with padrange
1347 # binops with padrange
1361 my($a, $b, $c, $d, @e);
1366 @e = ($a, $b, $c) x $d;