6 if (($Config::Config{'extensions'} !~ /\bB\b/) ){
7 print "1..0 # Skip -- Perl configured without B module\n";
16 my $tests = 17; # 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 done_testing($tests);
252 # Constants in a block
259 # Lexical and simple arithmetic
261 ++$test and $test /= 2;
264 $test /= 2 if ++$test;
271 my $test = sub : lvalue {
279 my $test = sub : method {
285 # block with continue
293 # lexical and package scalars
297 # lexical and package arrays
301 # lexical and package hashes
307 $_ .= <ARGV> . <$foo>;
310 my $foo = "Ab\x{100}\200\x{200}\237Cd\000Ef\x{1000}\cA\x{2000}\cZ";
328 # 2 arguments in a 3 argument for
329 for (my $i = 0; $i < 2;) {
334 for (my $i = 0; $i < 2; ++$i) {
338 # 3 argument for again
339 for (my $i = 0; $i < 2; ++$i) {
345 while ($i) { my $z = 1; } continue { $i = 99; }
348 foreach my $i (1, 2) {
360 foreach my $i (1, 2) {
365 foreach my $i (1, 2) {
370 foreach our $i (1, 2) {
374 # foreach with my and our
376 foreach our $i (1, 2) {
382 print reverse sort(@x);
386 print((sort {$b cmp $a} @x));
388 # reverse sort with block
390 print((reverse sort {$b <=> $a} @x));
394 print $_ foreach (reverse @a);
396 # foreach reverse (not inplace)
398 print $_ foreach (reverse 1, 2..5);
402 @ary = split(' ', 'foo', 0);
408 do { my $x = 1; $x };
410 # <20061012113037.GJ25805@c4.convolution.nl>
429 sub foo {} foo & & & foo;
434 # variables as method names
437 'Foo'->$bar('orz') = 'a stranger stranger than before';
439 # constants as method names
442 # constants as method names without ()
445 # [perl #47359] "indirect" method call notation
449 foo{@bar}1 xor foo();
452 (foo { @bar } 1)->foo;
453 (foo { @bar } 1), foo();
454 foo { @bar } 1 xor foo();
456 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
457 # CONTEXT use feature ':5.10';
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
466 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
467 # say with use 5.10.0
475 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
476 # say with use feature ':5.10';
480 use feature 'say', 'state', 'switch';
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
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
501 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
502 # CONTEXT use feature ':5.15';
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
511 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
512 # __SUB__ with use 5.15.0
520 # SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
521 # __SUB__ with use feature ':5.15';
525 use feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval';
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
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
546 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
547 # CONTEXT use feature ':5.10';
551 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
552 # CONTEXT use feature ':5.10';
553 # state var assignment
555 my $y = (state $x = 42);
558 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
559 # CONTEXT use feature ':5.10';
560 # state vars in anonymous subroutines
567 # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
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;
577 # Anonymous arrays and hashes, and references to them
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
587 when ('bar') { continue; }
588 when ($_ ~~ 'quux') { continue; }
592 # conditions in elsifs (regression in change #33710 which fixed bug #37302)
595 elsif ($a and $b) { x(); }
596 elsif ($a or $b) { x(); }
599 # interpolation in regexps
603 # TODO new undocumented cpan-bug #33708
607 # TODO hash constants not yet fixed
609 use constant H => { "#" => 1 }; H->{"#"}
611 # TODO optimized away 0 not yet fixed
613 foreach my $i (@_) { 0 }
615 # tests with not, not optimized
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;
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;
635 # tests with not, optimized
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;
658 x() unless $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;
670 # tests that should be constant folded
674 x() if GLIPP && GLIPP;
675 x() if !GLIPP || 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() }
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
727 if ($a == 1) { x() } elsif ($b == 2) { z() }
728 if (do { foo(); GLIPP }) { x() }
729 if (do { $a++; GLIPP }) { x() }
732 if ($a == 1) { x(); } elsif ($b == 2) { z(); }
733 if (do { foo(); GLIPP }) { x(); }
734 if (do { ++$a; GLIPP }) { x(); }
736 # TODO constant deparsing has been backed out for 5.12
737 # tests for deparsing constants
740 # TODO constant deparsing has been backed out for 5.12
741 # tests for deparsing imported constants
744 # TODO constant deparsing has been backed out for 5.12
745 # tests for deparsing re-exported constants
748 # TODO constant deparsing has been backed out for 5.12
749 # tests for deparsing imported constants that got deleted from the original namespace
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
758 eval '@Fcntl::O_EXCL = qw/affe tiger/;';
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;
777 # TODO Subsets of warnings could be encoded textually, rather than as bitflips.
778 # subsets of warnings
779 no warnings 'deprecated';
782 # TODO Better test for CPAN #33708 - the deparsed code has different behaviour
798 # SKIP ?$] > 5.013006 && ":= is now a syntax error"
799 # := treated as an empty attribute list
806 # : = empty attribute list
825 # #71870 Use of uninitialized value in bitwise and B::Deparse
827 @a = split(/foo/, $s, 0);
829 @a = split(/$r/, $s, 0);
832 # package declaration before label
855 "foo" =~ (1 ? /foo/ : /bar/);
856 "foo" =~ (1 ? y/foo// : /bar/);
857 "foo" =~ (1 ? y/foo//r : /bar/);
858 "foo" =~ (1 ? s/foo// : /bar/);
860 'foo' =~ ($_ =~ /foo/);
861 'foo' =~ ($_ =~ tr/fo//);
862 'foo' =~ ($_ =~ tr/fo//r);
863 'foo' =~ ($_ =~ s/foo//);
865 # The fix for [perl #20444] broke this.
868 # [perl #81424] match against aelemfast_lex
875 # [perl #91318] /regexp/applaud
877 print /a/aa, s/b/c/aa;
882 use feature "unicode_strings";
895 print /a/aa, s/b/c/aa;
900 use feature 'unicode_strings';
904 BEGIN { $^H{'reflags'} = '0';
905 $^H{'reflags_charset'} = '2'; }
914 # Test @threadsv_names under 5005threads
933 # readpipe with complex expression
939 # aelemfast for a lexical
943 # feature features without feature
958 # feature features when feature has been disabled by use VERSION
959 use feature (sprintf(":%vd", $^V));
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));
1006 use feature ':default';
1021 use feature 'current_sub', 'evalbytes';
1027 no feature 'unicode_strings';
1030 use feature 'current_sub', 'evalbytes';
1033 use feature ':default';
1036 use feature ':5.12';
1038 no feature 'unicode_strings';
1041 # $#- $#+ $#{%} etc.
1043 @x = ($#{`}, $#{~}, $#{!}, $#{@}, $#{$}, $#{%}, $#{^}, $#{&}, $#{*});
1044 @x = ($#{(}, $#{)}, $#{[}, $#{{}, $#{]}, $#{}}, $#{'}, $#{"}, $#{,});
1045 @x = ($#{<}, $#{.}, $#{>}, $#{/}, $#{?}, $#{=}, $#+, $#{\}, $#{|}, $#-);
1046 @x = ($#{;}, $#{:});
1048 # ${#} interpolated (the first line magically disables the warning)
1052 # [perl #86060] $( $| $) in regexps need braces
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'));
1067 print sort(foo('bar'));
1070 substr(my $a, 0, 0) = (foo(), bar());
1073 # This following line works around an unfixed bug that we are not trying to
1075 # CONTEXT BEGIN { $^H{a} = "b"; delete $^H{a} } # make %^H localised
1077 BEGIN { $^H{'foo'} = undef; }
1079 BEGIN { $^H{'bar'} = undef; }
1081 BEGIN { $^H{'baz'} = undef; }
1089 BEGIN { $^H{q[']} = '('; }
1092 # This following line works around an unfixed bug that we are not trying to
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'; }
1098 BEGIN { $^H{'b'} = 'a'; delete $^H{'a'}; }
1103 # [perl #47361] do({}) and do +{} (variants of do-file)
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;
1125 () = (require 'foo') + 3;
1126 () = (require foo) + 3;
1127 () = (CORE::dump 1) + 3;
1138 # [perl #97476] not() *does* follow the llafr
1139 $_ = ($a xor not +($1 || 2) ** 2);
1141 # Precedence conundrums with argument-less function calls
1149 # [perl #63558] open local(*FH)
1151 pipe local *FH, local *FH;
1153 # [perl #91416] open "string"
1166 () = 'open'->[0]; #aelemfast
1171 () = 'open'->[$_]; #aelem
1176 () = 'open'->{0}; #helem
1197 () = 'a::]b'->{'0'};
1199 # [perl #74740] -(f()) vs -f()
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' }
1210 $_ = 'foo' foreach (my($foo1, $bar1, $baz1));
1211 foreach $_ (my($foo2, $bar2, $baz2)) {
1214 foreach my $i (my($foo3, $bar3, $baz3)) {
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 { }
1224 foreach $_ (1 .. 3) {
1230 foreach $_ (1 .. 3) {
1236 foreach my $i (1 .. 3) {
1242 foreach my $i (1 .. 3) {