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');
24 my $deparse = B::Deparse->new();
25 isa_ok($deparse, 'B::Deparse', 'instantiate a B::Deparse object');
27 # Tell B::Deparse about our ambient pragmas
28 { my ($hint_bits, $warning_bits, $hinthash);
29 BEGIN { ($hint_bits, $warning_bits, $hinthash) = ($^H, ${^WARNING_BITS}, \%^H); }
30 $deparse->ambient_pragmas (
31 hint_bits => $hint_bits,
32 warning_bits => $warning_bits,
40 # This code is pinched from the t/lib/common.pl for TODO.
41 # It's not clear how to avoid duplication
42 # Now tweaked a bit to do skip or todo
44 foreach my $what (qw(skip todo)) {
45 s/^#\s*\U$what\E\s*(.*)\n//m and $reason{$what} = $1;
46 # If the SKIP reason starts ? then it's taken as a code snippet to
47 # evaluate. This provides the flexibility to have conditional SKIPs
48 if ($reason{$what} && $reason{$what} =~ s/^\?//) {
49 my $temp = eval $reason{$what};
51 die "# In \U$what\E code reason:\n# $reason{$what}\n$@";
53 $reason{$what} = $temp;
59 die "Missing name in test $_" unless defined $desc;
62 # Like this to avoid needing a label SKIP:
63 Test::More->builder->skip($reason{skip});
67 my ($input, $expected);
68 if (/(.*)\n>>>>\n(.*)/s) {
69 ($input, $expected) = ($1, $2);
72 ($input, $expected) = ($_, $_);
75 my $coderef = eval "sub {$input}";
78 is($@, "", "compilation of $desc");
81 my $deparsed = $deparse->coderef2text( $coderef );
82 my $regex = $expected;
83 $regex =~ s/(\S+)/\Q$1/g;
84 $regex =~ s/\s+/\\s+/g;
85 $regex = '^\{\s*' . $regex . '\s*\}$';
87 local $::TODO = $reason{todo};
88 like($deparsed, qr/$regex/, $desc);
92 use constant 'c', 'stuff';
93 is((eval "sub ".$deparse->coderef2text(\&c))->(), 'stuff',
94 'the subroutine generated by use constant deparses');
97 is($deparse->coderef2text(sub{(-1) ** $a }), "{\n (-1) ** \$a;\n}",
98 'anon sub capturing an external lexical');
100 use constant cr => ['hello'];
101 my $string = "sub " . $deparse->coderef2text(\&cr);
102 my $val = (eval $string)->() or diag $string;
103 is(ref($val), 'ARRAY', 'constant array references deparse');
104 is($val->[0], 'hello', 'and return the correct value');
106 my $path = join " ", map { qq["-I$_"] } @INC;
108 $a = `$^X $path "-MO=Deparse" -anlwi.bak -e 1 2>&1`;
109 $a =~ s/-e syntax OK\n//g;
110 $a =~ s/.*possible typo.*\n//; # Remove warning line
111 $a =~ s{\\340\\242}{\\s} if (ord("\\") == 224); # EBCDIC, cp 1047 or 037
112 $a =~ s{\\274\\242}{\\s} if (ord("\\") == 188); # $^O eq 'posix-bc'
114 BEGIN { $^I = ".bak"; }
116 BEGIN { $/ = "\n"; $\ = "\n"; }
117 LINE: while (defined($_ = <ARGV>)) {
119 our(@F) = split(' ', $_, 0);
124 'command line flags deparse as BEGIN blocks setting control variables');
126 $a = `$^X $path "-MO=Deparse" -e "use constant PI => 4" 2>&1`;
127 $a =~ s/-e syntax OK\n//g;
128 is($a, "use constant ('PI', 4);\n",
129 "Proxy Constant Subroutines must not show up as (incorrect) prototypes");
131 #Re: perlbug #35857, patch #24505
132 #handle warnings::register-ed packages properly.
133 package B::Deparse::Wrapper;
136 use warnings::register;
138 my $deparser = B::Deparse->new();
139 return $deparser->coderef2text(shift);
143 use overload '0+' => sub { 42 };
148 use constant GLIPP => 'glipp';
149 use constant PI => 4;
150 use constant OVERLOADED_NUMIFICATION => bless({}, 'Moo');
151 use Fcntl qw/O_TRUNC O_APPEND O_EXCL/;
152 BEGIN { delete $::Fcntl::{O_APPEND}; }
153 use POSIX qw/O_CREAT/;
156 my $res = B::Deparse::Wrapper::getcode($val);
157 like($res, qr/use warnings/,
158 '[perl #35857] [PATCH] B::Deparse doesnt handle warnings register properly');
161 my $x=sub { ++$q,++$p };
163 eval <<EOFCODE and test($x);
167 use warnings::register;
172 # Exotic sub declarations
173 $a = `$^X $path "-MO=Deparse" -e "sub ::::{}sub ::::::{}" 2>&1`;
174 $a =~ s/-e syntax OK\n//g;
175 is($a, <<'EOCODG', "sub :::: and sub ::::::");
186 my $code = <<"EOCODE";
188 our \$\x{1e1f}\x{14d}\x{14d};
192 = $deparse->coderef2text(eval "sub { our \$\x{1e1f}\x{14d}\x{14d} }" );
193 s/$ \n//x for $deparsed, $code;
194 is $deparsed, $code, 'our $funny_Unicode_chars';
199 `$^X $path "-MO=Deparse" -e "BEGIN{*CORE::GLOBAL::require=sub{1}}" 2>&1`;
200 $a =~ s/-e syntax OK\n//g;
201 is($a, <<'EOCODF', "CORE::GLOBAL::require override causing panick");
203 *CORE::GLOBAL::require = sub {
212 is($deparse->coderef2text(sub{ print "@{*}" }),
215 }>, 'curly around to interpolate "@{*}"');
216 is($deparse->coderef2text(sub{ print "@{-}" }),
219 }>, 'no need to curly around to interpolate "@-"');
227 # Constants in a block
234 # Lexical and simple arithmetic
236 ++$test and $test /= 2;
239 $test /= 2 if ++$test;
246 my $test = sub : lvalue {
254 my $test = sub : method {
260 # block with continue
268 # lexical and package scalars
272 # lexical and package arrays
276 # lexical and package hashes
282 $_ .= <ARGV> . <$foo>;
285 my $foo = "Ab\x{100}\200\x{200}\237Cd\000Ef\x{1000}\cA\x{2000}\cZ";
303 # 2 arguments in a 3 argument for
304 for (my $i = 0; $i < 2;) {
309 for (my $i = 0; $i < 2; ++$i) {
313 # 3 argument for again
314 for (my $i = 0; $i < 2; ++$i) {
320 while ($i) { my $z = 1; } continue { $i = 99; }
323 foreach my $i (1, 2) {
335 foreach my $i (1, 2) {
340 foreach my $i (1, 2) {
345 foreach our $i (1, 2) {
349 # foreach with my and our
351 foreach our $i (1, 2) {
357 print reverse sort(@x);
361 print((sort {$b cmp $a} @x));
363 # reverse sort with block
365 print((reverse sort {$b <=> $a} @x));
369 print $_ foreach (reverse @a);
371 # foreach reverse (not inplace)
373 print $_ foreach (reverse 1, 2..5);
377 @ary = split(' ', 'foo', 0);
383 do { my $x = 1; $x };
385 # <20061012113037.GJ25805@c4.convolution.nl>
404 sub foo {} foo & & & foo;
409 # variables as method names
412 'Foo'->$bar('orz') = 'a stranger stranger than before';
414 # constants as method names
417 # constants as method names without ()
420 # [perl #47359] "indirect" method call notation
424 foo{@bar}1 xor foo();
427 (foo { @bar } 1)->foo;
428 (foo { @bar } 1), foo();
429 foo { @bar } 1 xor foo();
431 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
435 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
439 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
440 # state var assignment
442 my $y = (state $x = 42);
445 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
446 # state vars in anonymous subroutines
453 # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
458 # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
459 # keys @array; values @array
460 keys @$a if keys @ARGV;
461 values @ARGV if values @$a;
463 # Anonymous arrays and hashes, and references to them
469 # SKIP ?$] < 5.010 && "smartmatch and given/when not implemented on this Perl version"
470 # implicit smartmatch in given/when
472 when ('bar') { continue; }
473 when ($_ ~~ 'quux') { continue; }
477 # conditions in elsifs (regression in change #33710 which fixed bug #37302)
480 elsif ($a and $b) { x(); }
481 elsif ($a or $b) { x(); }
484 # interpolation in regexps
488 # TODO new undocumented cpan-bug #33708
492 # TODO hash constants not yet fixed
494 use constant H => { "#" => 1 }; H->{"#"}
496 # TODO optimized away 0 not yet fixed
498 foreach my $i (@_) { 0 }
500 # tests with not, not optimized
503 x() if not $a and $b;
504 x() if $a and not $b;
505 x() unless not $a and $b;
506 x() unless $a and not $b;
509 x() unless not $a or $b;
510 x() unless $a or not $b;
511 x() if $a and not $b and $c;
512 x() if not $a and $b and not $c;
513 x() unless $a and not $b and $c;
514 x() unless not $a and $b and not $c;
515 x() if $a or not $b or $c;
516 x() if not $a or $b or not $c;
517 x() unless $a or not $b or $c;
518 x() unless not $a or $b or not $c;
520 # tests with not, optimized
524 x() if not $a and not $b;
525 x() unless not $a and not $b;
526 x() if not $a or not $b;
527 x() unless not $a or not $b;
528 x() if not $a and not $b and $c;
529 x() unless not $a and not $b and $c;
530 x() if not $a or not $b or $c;
531 x() unless not $a or not $b or $c;
532 x() if not $a and not $b and not $c;
533 x() unless not $a and not $b and not $c;
534 x() if not $a or not $b or not $c;
535 x() unless not $a or not $b or not $c;
536 x() unless not $a or not $b or not $c;
543 x() unless $a and $b;
545 x() if not $a || $b and $c;
546 x() unless not $a || $b and $c;
547 x() if not $a && $b or $c;
548 x() unless not $a && $b or $c;
549 x() unless $a or $b or $c;
550 x() if $a or $b or $c;
551 x() unless $a and $b and $c;
552 x() if $a and $b and $c;
553 x() unless not $a && $b && $c;
555 # tests that should be constant folded
559 x() if GLIPP && GLIPP;
560 x() if !GLIPP || GLIPP;
562 x() if do { no warnings 'void'; 5; GLIPP };
563 x() if do { !GLIPP };
564 if (GLIPP) { x() } else { z() }
565 if (!GLIPP) { x() } else { z() }
566 if (GLIPP) { x() } elsif (GLIPP) { z() }
567 if (!GLIPP) { x() } elsif (GLIPP) { z() }
568 if (GLIPP) { x() } elsif (!GLIPP) { z() }
569 if (!GLIPP) { x() } elsif (!GLIPP) { z() }
570 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (GLIPP) { t() }
571 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
572 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
606 # TODO constant deparsing has been backed out for 5.12
607 # XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
608 # tests that shouldn't be constant folded
609 # It might be fundamentally impossible to make this work on ithreads, in which
610 # case the TODO should become a SKIP
612 if ($a == 1) { x() } elsif ($b == 2) { z() }
613 if (do { foo(); GLIPP }) { x() }
614 if (do { $a++; GLIPP }) { x() }
617 if ($a == 1) { x(); } elsif ($b == 2) { z(); }
618 if (do { foo(); GLIPP }) { x(); }
619 if (do { ++$a; GLIPP }) { x(); }
621 # TODO constant deparsing has been backed out for 5.12
622 # tests for deparsing constants
625 # TODO constant deparsing has been backed out for 5.12
626 # tests for deparsing imported constants
629 # TODO constant deparsing has been backed out for 5.12
630 # tests for deparsing re-exported constants
633 # TODO constant deparsing has been backed out for 5.12
634 # tests for deparsing imported constants that got deleted from the original namespace
637 # TODO constant deparsing has been backed out for 5.12
638 # XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
639 # tests for deparsing constants which got turned into full typeglobs
640 # It might be fundamentally impossible to make this work on ithreads, in which
641 # case the TODO should become a SKIP
643 eval '@Fcntl::O_EXCL = qw/affe tiger/;';
646 # TODO constant deparsing has been backed out for 5.12
647 # tests for deparsing of blessed constant with overloaded numification
648 warn OVERLOADED_NUMIFICATION;
650 # TODO Only strict 'refs' currently supported
655 # TODO Subsets of warnings could be encoded textually, rather than as bitflips.
656 # subsets of warnings
657 no warnings 'deprecated';
660 # TODO Better test for CPAN #33708 - the deparsed code has different behaviour
676 # SKIP ?$] > 5.013006 && ":= is now a syntax error"
677 # := treated as an empty attribute list
684 # : = empty attribute list
703 # #71870 Use of uninitialized value in bitwise and B::Deparse
705 @a = split(/foo/, $s, 0);
707 @a = split(/$r/, $s, 0);
710 # package declaration before label
733 "foo" =~ (1 ? /foo/ : /bar/);
734 "foo" =~ (1 ? y/foo// : /bar/);
735 "foo" =~ (1 ? y/foo//r : /bar/);
736 "foo" =~ (1 ? s/foo// : /bar/);
738 'foo' =~ ($_ =~ /foo/);
739 'foo' =~ ($_ =~ tr/fo//);
740 'foo' =~ ($_ =~ tr/fo//r);
741 'foo' =~ ($_ =~ s/foo//);
743 # The fix for [perl #20444] broke this.
746 # Test @threadsv_names under 5005threads
765 # readpipe with complex expression
771 # aelemfast for a lexical
775 # feature features without feature
777 delete $^H{'feature_say'};
778 delete $^H{'feature_state'};
779 delete $^H{'feature_switch'};
796 @x = ($#{`}, $#{~}, $#{!}, $#{@}, $#{$}, $#{%}, $#{^}, $#{&}, $#{*});
797 @x = ($#{(}, $#{)}, $#{[}, $#{{}, $#{]}, $#{}}, $#{'}, $#{"}, $#{,});
798 @x = ($#{<}, $#{.}, $#{>}, $#{/}, $#{?}, $#{=}, $#+, $#{\}, $#{|}, $#-);
801 # ${#} interpolated (the first line magically disables the warning)
809 # sort(foo(bar)) is interpreted as sort &foo(bar)
810 # sort foo(bar) is interpreted as sort foo bar
811 # parentheses are not optional in this case
812 print sort(foo('bar'));
814 print sort(foo('bar'));
817 substr(my $a, 0, 0) = (foo(), bar());
821 BEGIN { $^H{'foo'} = undef; }
823 BEGIN { $^H{'bar'} = undef; }
825 BEGIN { $^H{'baz'} = undef; }
833 BEGIN { $^H{q[']} = '('; }
836 # hint hash changes that serialise the same way with sort %hh
837 BEGIN { $^H{'a'} = 'b'; }
839 BEGIN { $^H{'b'} = 'a'; delete $^H{'a'}; }
844 # [perl #47361] do({}) and do +{} (variants of do-file)
858 # [perl #77096] functions that do not follow the llafr
859 () = (return 1) + time;
860 () = (return ($1 + $2) * $3) + time;
861 () = (return ($a xor $b)) + time;
862 () = (do 'file') + time;
863 () = (do ($1 + $2) * $3) + time;
864 () = (do ($1 xor $2)) + time;
866 () = (require 'foo') + 3;
867 () = (require foo) + 3;
868 () = (CORE::dump 1) + 3;
879 # Precedence conundrums with argument-less function calls
887 # [perl #63558] open local(*FH)
889 pipe local *FH, local *FH;
891 # [perl #74740] -(f()) vs -f()