6 if (($Config::Config{'extensions'} !~ /\bB\b/) ){
7 print "1..0 # Skip -- Perl configured without B module\n";
15 # BEGIN block is actually a subroutine :-)
16 return unless $] > 5.009;
18 feature->import(':5.10');
23 my $tests = 17; # not counting those in the __DATA__ section
26 my $deparse = B::Deparse->new();
27 isa_ok($deparse, 'B::Deparse', 'instantiate a B::Deparse object');
29 # Tell B::Deparse about our ambient pragmas
30 { my ($hint_bits, $warning_bits, $hinthash);
31 BEGIN { ($hint_bits, $warning_bits, $hinthash) = ($^H, ${^WARNING_BITS}, \%^H); }
32 $deparse->ambient_pragmas (
33 hint_bits => $hint_bits,
34 warning_bits => $warning_bits,
43 # This code is pinched from the t/lib/common.pl for TODO.
44 # It's not clear how to avoid duplication
45 # Now tweaked a bit to do skip or todo
47 foreach my $what (qw(skip todo)) {
48 s/^#\s*\U$what\E\s*(.*)\n//m and $meta{$what} = $1;
49 # If the SKIP reason starts ? then it's taken as a code snippet to
50 # evaluate. This provides the flexibility to have conditional SKIPs
51 if ($meta{$what} && $meta{$what} =~ s/^\?//) {
52 my $temp = eval $meta{$what};
54 die "# In \U$what\E code reason:\n# $meta{$what}\n$@";
62 die "Missing name in test $_" unless defined $desc;
65 # Like this to avoid needing a label SKIP:
66 Test::More->builder->skip($meta{skip});
70 my ($input, $expected);
71 if (/(.*)\n>>>>\n(.*)/s) {
72 ($input, $expected) = ($1, $2);
75 ($input, $expected) = ($_, $_);
78 my $coderef = eval "sub {$input}";
81 is($@, "", "compilation of $desc");
84 my $deparsed = $deparse->coderef2text( $coderef );
85 my $regex = $expected;
86 $regex =~ s/(\S+)/\Q$1/g;
87 $regex =~ s/\s+/\\s+/g;
88 $regex = '^\{\s*' . $regex . '\s*\}$';
90 local $::TODO = $meta{todo};
91 like($deparsed, qr/$regex/, $desc);
95 use constant 'c', 'stuff';
96 is((eval "sub ".$deparse->coderef2text(\&c))->(), 'stuff',
97 'the subroutine generated by use constant deparses');
100 is($deparse->coderef2text(sub{(-1) ** $a }), "{\n (-1) ** \$a;\n}",
101 'anon sub capturing an external lexical');
103 use constant cr => ['hello'];
104 my $string = "sub " . $deparse->coderef2text(\&cr);
105 my $val = (eval $string)->() or diag $string;
106 is(ref($val), 'ARRAY', 'constant array references deparse');
107 is($val->[0], 'hello', 'and return the correct value');
109 my $path = join " ", map { qq["-I$_"] } @INC;
111 $a = `$^X $path "-MO=Deparse" -anlwi.bak -e 1 2>&1`;
112 $a =~ s/-e syntax OK\n//g;
113 $a =~ s/.*possible typo.*\n//; # Remove warning line
114 $a =~ s{\\340\\242}{\\s} if (ord("\\") == 224); # EBCDIC, cp 1047 or 037
115 $a =~ s{\\274\\242}{\\s} if (ord("\\") == 188); # $^O eq 'posix-bc'
117 BEGIN { $^I = ".bak"; }
119 BEGIN { $/ = "\n"; $\ = "\n"; }
120 LINE: while (defined($_ = <ARGV>)) {
122 our(@F) = split(' ', $_, 0);
127 'command line flags deparse as BEGIN blocks setting control variables');
129 $a = `$^X $path "-MO=Deparse" -e "use constant PI => 4" 2>&1`;
130 $a =~ s/-e syntax OK\n//g;
131 is($a, "use constant ('PI', 4);\n",
132 "Proxy Constant Subroutines must not show up as (incorrect) prototypes");
134 #Re: perlbug #35857, patch #24505
135 #handle warnings::register-ed packages properly.
136 package B::Deparse::Wrapper;
139 use warnings::register;
141 my $deparser = B::Deparse->new();
142 return $deparser->coderef2text(shift);
146 use overload '0+' => sub { 42 };
151 use constant GLIPP => 'glipp';
152 use constant PI => 4;
153 use constant OVERLOADED_NUMIFICATION => bless({}, 'Moo');
154 use Fcntl qw/O_TRUNC O_APPEND O_EXCL/;
155 BEGIN { delete $::Fcntl::{O_APPEND}; }
156 use POSIX qw/O_CREAT/;
159 my $res = B::Deparse::Wrapper::getcode($val);
160 like($res, qr/use warnings/,
161 '[perl #35857] [PATCH] B::Deparse doesnt handle warnings register properly');
164 my $x=sub { ++$q,++$p };
166 eval <<EOFCODE and test($x);
170 use warnings::register;
175 # Exotic sub declarations
176 $a = `$^X $path "-MO=Deparse" -e "sub ::::{}sub ::::::{}" 2>&1`;
177 $a =~ s/-e syntax OK\n//g;
178 is($a, <<'EOCODG', "sub :::: and sub ::::::");
189 my $code = <<"EOCODE";
191 our \$\x{1e1f}\x{14d}\x{14d};
195 = $deparse->coderef2text(eval "sub { our \$\x{1e1f}\x{14d}\x{14d} }" );
196 s/$ \n//x for $deparsed, $code;
197 is $deparsed, $code, 'our $funny_Unicode_chars';
202 `$^X $path "-MO=Deparse" -e "BEGIN{*CORE::GLOBAL::require=sub{1}}" 2>&1`;
203 $a =~ s/-e syntax OK\n//g;
204 is($a, <<'EOCODF', "CORE::GLOBAL::require override causing panick");
206 *CORE::GLOBAL::require = sub {
215 `$^X $path "-MO=Deparse" -e "BEGIN{*Acme::Acme:: = *Acme::}" 2>&1`;
216 like($a, qr/-e syntax OK/,
217 "Deparse does not hang when traversing stash circularities");
221 is($deparse->coderef2text(sub{ print "@{*}" }),
224 }>, 'curly around to interpolate "@{*}"');
225 is($deparse->coderef2text(sub{ print "@{-}" }),
228 }>, 'no need to curly around to interpolate "@-"');
230 # Strict hints in %^H are mercilessly suppressed
232 `$^X $path "-MO=Deparse" -e "use strict; print;" 2>&1`;
233 unlike($a, qr/BEGIN/,
234 "Deparse does not emit strict hh hints");
236 # ambient_pragmas should not mess with strict settings.
238 skip "requires 5.11", 1 unless $] >= 5.011;
242 # Clear out the strict hints from %^H
244 new B::Deparse -> ambient_pragmas(strict => 'all');
246 use 5.011; # should enable strict
247 ok !eval '$do_noT_create_a_variable_with_this_name = 1',
248 'ambient_pragmas do not mess with compiling scope';
252 done_testing($tests);
258 # Constants in a block
265 # Lexical and simple arithmetic
267 ++$test and $test /= 2;
270 $test /= 2 if ++$test;
277 my $test = sub : lvalue {
285 my $test = sub : method {
291 # block with continue
299 # lexical and package scalars
303 # lexical and package arrays
307 # lexical and package hashes
313 $_ .= <ARGV> . <$foo>;
316 my $foo = "Ab\x{100}\200\x{200}\237Cd\000Ef\x{1000}\cA\x{2000}\cZ";
334 # 2 arguments in a 3 argument for
335 for (my $i = 0; $i < 2;) {
340 for (my $i = 0; $i < 2; ++$i) {
344 # 3 argument for again
345 for (my $i = 0; $i < 2; ++$i) {
351 while ($i) { my $z = 1; } continue { $i = 99; }
354 foreach my $i (1, 2) {
366 foreach my $i (1, 2) {
371 foreach my $i (1, 2) {
376 foreach our $i (1, 2) {
380 # foreach with my and our
382 foreach our $i (1, 2) {
388 print reverse sort(@x);
392 print((sort {$b cmp $a} @x));
394 # reverse sort with block
396 print((reverse sort {$b <=> $a} @x));
400 print $_ foreach (reverse @a);
402 # foreach reverse (not inplace)
404 print $_ foreach (reverse 1, 2..5);
408 @ary = split(' ', 'foo', 0);
414 do { my $x = 1; $x };
416 # <20061012113037.GJ25805@c4.convolution.nl>
435 sub foo {} foo & & & foo;
440 # variables as method names
443 'Foo'->$bar('orz') = 'a stranger stranger than before';
445 # constants as method names
448 # constants as method names without ()
451 # [perl #47359] "indirect" method call notation
455 foo{@bar}1 xor foo();
458 (foo { @bar } 1)->foo;
459 (foo { @bar } 1), foo();
460 foo { @bar } 1 xor foo();
462 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
466 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
470 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
471 # state var assignment
473 my $y = (state $x = 42);
476 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
477 # state vars in anonymous subroutines
484 # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
489 # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
490 # keys @array; values @array
491 keys @$a if keys @ARGV;
492 values @ARGV if values @$a;
494 # Anonymous arrays and hashes, and references to them
500 # SKIP ?$] < 5.010 && "smartmatch and given/when not implemented on this Perl version"
501 # implicit smartmatch in given/when
503 when ('bar') { continue; }
504 when ($_ ~~ 'quux') { continue; }
508 # conditions in elsifs (regression in change #33710 which fixed bug #37302)
511 elsif ($a and $b) { x(); }
512 elsif ($a or $b) { x(); }
515 # interpolation in regexps
519 # TODO new undocumented cpan-bug #33708
523 # TODO hash constants not yet fixed
525 use constant H => { "#" => 1 }; H->{"#"}
527 # TODO optimized away 0 not yet fixed
529 foreach my $i (@_) { 0 }
531 # tests with not, not optimized
534 x() if not $a and $b;
535 x() if $a and not $b;
536 x() unless not $a and $b;
537 x() unless $a and not $b;
540 x() unless not $a or $b;
541 x() unless $a or not $b;
542 x() if $a and not $b and $c;
543 x() if not $a and $b and not $c;
544 x() unless $a and not $b and $c;
545 x() unless not $a and $b and not $c;
546 x() if $a or not $b or $c;
547 x() if not $a or $b or not $c;
548 x() unless $a or not $b or $c;
549 x() unless not $a or $b or not $c;
551 # tests with not, optimized
555 x() if not $a and not $b;
556 x() unless not $a and not $b;
557 x() if not $a or not $b;
558 x() unless not $a or not $b;
559 x() if not $a and not $b and $c;
560 x() unless not $a and not $b and $c;
561 x() if not $a or not $b or $c;
562 x() unless not $a or not $b or $c;
563 x() if not $a and not $b and not $c;
564 x() unless not $a and not $b and not $c;
565 x() if not $a or not $b or not $c;
566 x() unless not $a or not $b or not $c;
567 x() unless not $a or not $b or not $c;
574 x() unless $a and $b;
576 x() if not $a || $b and $c;
577 x() unless not $a || $b and $c;
578 x() if not $a && $b or $c;
579 x() unless not $a && $b or $c;
580 x() unless $a or $b or $c;
581 x() if $a or $b or $c;
582 x() unless $a and $b and $c;
583 x() if $a and $b and $c;
584 x() unless not $a && $b && $c;
586 # tests that should be constant folded
590 x() if GLIPP && GLIPP;
591 x() if !GLIPP || GLIPP;
593 x() if do { no warnings 'void'; 5; GLIPP };
594 x() if do { !GLIPP };
595 if (GLIPP) { x() } else { z() }
596 if (!GLIPP) { x() } else { z() }
597 if (GLIPP) { x() } elsif (GLIPP) { z() }
598 if (!GLIPP) { x() } elsif (GLIPP) { z() }
599 if (GLIPP) { x() } elsif (!GLIPP) { z() }
600 if (!GLIPP) { x() } elsif (!GLIPP) { z() }
601 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (GLIPP) { t() }
602 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
603 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
637 # TODO constant deparsing has been backed out for 5.12
638 # XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
639 # tests that shouldn't be constant folded
640 # It might be fundamentally impossible to make this work on ithreads, in which
641 # case the TODO should become a SKIP
643 if ($a == 1) { x() } elsif ($b == 2) { z() }
644 if (do { foo(); GLIPP }) { x() }
645 if (do { $a++; GLIPP }) { x() }
648 if ($a == 1) { x(); } elsif ($b == 2) { z(); }
649 if (do { foo(); GLIPP }) { x(); }
650 if (do { ++$a; GLIPP }) { x(); }
652 # TODO constant deparsing has been backed out for 5.12
653 # tests for deparsing constants
656 # TODO constant deparsing has been backed out for 5.12
657 # tests for deparsing imported constants
660 # TODO constant deparsing has been backed out for 5.12
661 # tests for deparsing re-exported constants
664 # TODO constant deparsing has been backed out for 5.12
665 # tests for deparsing imported constants that got deleted from the original namespace
668 # TODO constant deparsing has been backed out for 5.12
669 # XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
670 # tests for deparsing constants which got turned into full typeglobs
671 # It might be fundamentally impossible to make this work on ithreads, in which
672 # case the TODO should become a SKIP
674 eval '@Fcntl::O_EXCL = qw/affe tiger/;';
677 # TODO constant deparsing has been backed out for 5.12
678 # tests for deparsing of blessed constant with overloaded numification
679 warn OVERLOADED_NUMIFICATION;
693 # TODO Subsets of warnings could be encoded textually, rather than as bitflips.
694 # subsets of warnings
695 no warnings 'deprecated';
698 # TODO Better test for CPAN #33708 - the deparsed code has different behaviour
714 # SKIP ?$] > 5.013006 && ":= is now a syntax error"
715 # := treated as an empty attribute list
722 # : = empty attribute list
741 # #71870 Use of uninitialized value in bitwise and B::Deparse
743 @a = split(/foo/, $s, 0);
745 @a = split(/$r/, $s, 0);
748 # package declaration before label
771 "foo" =~ (1 ? /foo/ : /bar/);
772 "foo" =~ (1 ? y/foo// : /bar/);
773 "foo" =~ (1 ? y/foo//r : /bar/);
774 "foo" =~ (1 ? s/foo// : /bar/);
776 'foo' =~ ($_ =~ /foo/);
777 'foo' =~ ($_ =~ tr/fo//);
778 'foo' =~ ($_ =~ tr/fo//r);
779 'foo' =~ ($_ =~ s/foo//);
781 # The fix for [perl #20444] broke this.
784 # [perl #81424] match against aelemfast_lex
791 # [perl #91318] /regexp/applaud
793 print /a/aa, s/b/c/aa;
798 use feature "unicode_strings";
811 print /a/aa, s/b/c/aa;
816 use feature 'unicode_strings';
820 BEGIN { $^H{'reflags'} = '0';
821 $^H{'reflags_charset'} = '2'; }
830 # Test @threadsv_names under 5005threads
849 # readpipe with complex expression
855 # aelemfast for a lexical
859 # feature features without feature
860 no feature 'say', 'state', 'switch';
875 # feature features when feature has been disabled by use VERSION
876 use feature (sprintf(":%vd", $^V));
892 use feature ':default';
907 use feature 'current_sub', 'evalbytes';
913 no feature 'unicode_strings';
916 use feature 'current_sub', 'evalbytes';
919 use feature ':default';
924 no feature 'unicode_strings';
929 @x = ($#{`}, $#{~}, $#{!}, $#{@}, $#{$}, $#{%}, $#{^}, $#{&}, $#{*});
930 @x = ($#{(}, $#{)}, $#{[}, $#{{}, $#{]}, $#{}}, $#{'}, $#{"}, $#{,});
931 @x = ($#{<}, $#{.}, $#{>}, $#{/}, $#{?}, $#{=}, $#+, $#{\}, $#{|}, $#-);
934 # ${#} interpolated (the first line magically disables the warning)
938 # [perl #86060] $( $| $) in regexps need braces
948 # sort(foo(bar)) is interpreted as sort &foo(bar)
949 # sort foo(bar) is interpreted as sort foo bar
950 # parentheses are not optional in this case
951 print sort(foo('bar'));
953 print sort(foo('bar'));
956 substr(my $a, 0, 0) = (foo(), bar());
960 BEGIN { $^H{'foo'} = undef; }
962 BEGIN { $^H{'bar'} = undef; }
964 BEGIN { $^H{'baz'} = undef; }
972 BEGIN { $^H{q[']} = '('; }
975 # hint hash changes that serialise the same way with sort %hh
976 BEGIN { $^H{'a'} = 'b'; }
978 BEGIN { $^H{'b'} = 'a'; delete $^H{'a'}; }
983 # [perl #47361] do({}) and do +{} (variants of do-file)
997 # [perl #77096] functions that do not follow the llafr
998 () = (return 1) + time;
999 () = (return ($1 + $2) * $3) + time;
1000 () = (return ($a xor $b)) + time;
1001 () = (do 'file') + time;
1002 () = (do ($1 + $2) * $3) + time;
1003 () = (do ($1 xor $2)) + time;
1005 () = (require 'foo') + 3;
1006 () = (require foo) + 3;
1007 () = (CORE::dump 1) + 3;
1018 # [perl #97476] not() *does* follow the llafr
1019 $_ = ($a xor not +($1 || 2) ** 2);
1021 # Precedence conundrums with argument-less function calls
1029 # [perl #63558] open local(*FH)
1031 pipe local *FH, local *FH;
1033 # [perl #91416] open "string"
1046 () = 'open'->[0]; #aelemfast
1051 () = 'open'->[$_]; #aelem
1056 () = 'open'->{0}; #helem
1077 () = 'a::]b'->{'0'};
1079 # [perl #74740] -(f()) vs -f()
1085 #[perl #30504] foreach-my postfix/prefix difference
1086 $_ = 'foo' foreach my ($foo1, $bar1, $baz1);
1087 foreach (my ($foo2, $bar2, $baz2)) { $_ = 'foo' }
1088 foreach my $i (my ($foo3, $bar3, $baz3)) { $i = 'foo' }
1090 $_ = 'foo' foreach (my($foo1, $bar1, $baz1));
1091 foreach $_ (my($foo2, $bar2, $baz2)) {
1094 foreach my $i (my($foo3, $bar3, $baz3)) {
1098 #[perl #108224] foreach with continue block
1099 foreach (1 .. 3) { print } continue { print "\n" }
1100 foreach (1 .. 3) { } continue { }
1101 foreach my $i (1 .. 3) { print $i } continue { print "\n" }
1102 foreach my $i (1 .. 3) { } continue { }
1104 foreach $_ (1 .. 3) {
1110 foreach $_ (1 .. 3) {
1116 foreach my $i (1 .. 3) {
1122 foreach my $i (1 .. 3) {