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,
41 # This code is pinched from the t/lib/common.pl for TODO.
42 # It's not clear how to avoid duplication
43 # Now tweaked a bit to do skip or todo
45 foreach my $what (qw(skip todo)) {
46 s/^#\s*\U$what\E\s*(.*)\n//m and $reason{$what} = $1;
47 # If the SKIP reason starts ? then it's taken as a code snippet to
48 # evaluate. This provides the flexibility to have conditional SKIPs
49 if ($reason{$what} && $reason{$what} =~ s/^\?//) {
50 my $temp = eval $reason{$what};
52 die "# In \U$what\E code reason:\n# $reason{$what}\n$@";
54 $reason{$what} = $temp;
60 die "Missing name in test $_" unless defined $desc;
63 # Like this to avoid needing a label SKIP:
64 Test::More->builder->skip($reason{skip});
68 my ($input, $expected);
69 if (/(.*)\n>>>>\n(.*)/s) {
70 ($input, $expected) = ($1, $2);
73 ($input, $expected) = ($_, $_);
76 my $coderef = eval "sub {$input}";
79 is($@, "", "compilation of $desc");
82 my $deparsed = $deparse->coderef2text( $coderef );
83 my $regex = $expected;
84 $regex =~ s/(\S+)/\Q$1/g;
85 $regex =~ s/\s+/\\s+/g;
86 $regex = '^\{\s*' . $regex . '\s*\}$';
88 local $::TODO = $reason{todo};
89 like($deparsed, qr/$regex/, $desc);
93 use constant 'c', 'stuff';
94 is((eval "sub ".$deparse->coderef2text(\&c))->(), 'stuff',
95 'the subroutine generated by use constant deparses');
98 is($deparse->coderef2text(sub{(-1) ** $a }), "{\n (-1) ** \$a;\n}",
99 'anon sub capturing an external lexical');
101 use constant cr => ['hello'];
102 my $string = "sub " . $deparse->coderef2text(\&cr);
103 my $val = (eval $string)->() or diag $string;
104 is(ref($val), 'ARRAY', 'constant array references deparse');
105 is($val->[0], 'hello', 'and return the correct value');
107 my $path = join " ", map { qq["-I$_"] } @INC;
109 $a = `$^X $path "-MO=Deparse" -anlwi.bak -e 1 2>&1`;
110 $a =~ s/-e syntax OK\n//g;
111 $a =~ s/.*possible typo.*\n//; # Remove warning line
112 $a =~ s{\\340\\242}{\\s} if (ord("\\") == 224); # EBCDIC, cp 1047 or 037
113 $a =~ s{\\274\\242}{\\s} if (ord("\\") == 188); # $^O eq 'posix-bc'
115 BEGIN { $^I = ".bak"; }
117 BEGIN { $/ = "\n"; $\ = "\n"; }
118 LINE: while (defined($_ = <ARGV>)) {
120 our(@F) = split(' ', $_, 0);
125 'command line flags deparse as BEGIN blocks setting control variables');
127 $a = `$^X $path "-MO=Deparse" -e "use constant PI => 4" 2>&1`;
128 $a =~ s/-e syntax OK\n//g;
129 is($a, "use constant ('PI', 4);\n",
130 "Proxy Constant Subroutines must not show up as (incorrect) prototypes");
132 #Re: perlbug #35857, patch #24505
133 #handle warnings::register-ed packages properly.
134 package B::Deparse::Wrapper;
137 use warnings::register;
139 my $deparser = B::Deparse->new();
140 return $deparser->coderef2text(shift);
144 use overload '0+' => sub { 42 };
149 use constant GLIPP => 'glipp';
150 use constant PI => 4;
151 use constant OVERLOADED_NUMIFICATION => bless({}, 'Moo');
152 use Fcntl qw/O_TRUNC O_APPEND O_EXCL/;
153 BEGIN { delete $::Fcntl::{O_APPEND}; }
154 use POSIX qw/O_CREAT/;
157 my $res = B::Deparse::Wrapper::getcode($val);
158 like($res, qr/use warnings/,
159 '[perl #35857] [PATCH] B::Deparse doesnt handle warnings register properly');
162 my $x=sub { ++$q,++$p };
164 eval <<EOFCODE and test($x);
168 use warnings::register;
173 # Exotic sub declarations
174 $a = `$^X $path "-MO=Deparse" -e "sub ::::{}sub ::::::{}" 2>&1`;
175 $a =~ s/-e syntax OK\n//g;
176 is($a, <<'EOCODG', "sub :::: and sub ::::::");
187 my $code = <<"EOCODE";
189 our \$\x{1e1f}\x{14d}\x{14d};
193 = $deparse->coderef2text(eval "sub { our \$\x{1e1f}\x{14d}\x{14d} }" );
194 s/$ \n//x for $deparsed, $code;
195 is $deparsed, $code, 'our $funny_Unicode_chars';
200 `$^X $path "-MO=Deparse" -e "BEGIN{*CORE::GLOBAL::require=sub{1}}" 2>&1`;
201 $a =~ s/-e syntax OK\n//g;
202 is($a, <<'EOCODF', "CORE::GLOBAL::require override causing panick");
204 *CORE::GLOBAL::require = sub {
217 # Constants in a block
224 # Lexical and simple arithmetic
226 ++$test and $test /= 2;
229 $test /= 2 if ++$test;
236 my $test = sub : lvalue {
244 my $test = sub : method {
250 # block with continue
258 # lexical and package scalars
262 # lexical and package arrays
266 # lexical and package hashes
272 $_ .= <ARGV> . <$foo>;
275 my $foo = "Ab\x{100}\200\x{200}\237Cd\000Ef\x{1000}\cA\x{2000}\cZ";
293 # 2 arguments in a 3 argument for
294 for (my $i = 0; $i < 2;) {
299 for (my $i = 0; $i < 2; ++$i) {
303 # 3 argument for again
304 for (my $i = 0; $i < 2; ++$i) {
310 while ($i) { my $z = 1; } continue { $i = 99; }
313 foreach my $i (1, 2) {
325 foreach my $i (1, 2) {
330 foreach my $i (1, 2) {
335 foreach our $i (1, 2) {
339 # foreach with my and our
341 foreach our $i (1, 2) {
347 print reverse sort(@x);
351 print((sort {$b cmp $a} @x));
353 # reverse sort with block
355 print((reverse sort {$b <=> $a} @x));
359 print $_ foreach (reverse @a);
361 # foreach reverse (not inplace)
363 print $_ foreach (reverse 1, 2..5);
367 @ary = split(' ', 'foo', 0);
373 do { my $x = 1; $x };
375 # <20061012113037.GJ25805@c4.convolution.nl>
392 # variables as method names
396 # constants as method names
399 # constants as method names without ()
402 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
406 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
410 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
411 # state var assignment
413 my $y = (state $x = 42);
416 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
417 # state vars in anonymous subroutines
424 # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
429 # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
430 # keys @array; values @array
431 keys @$a if keys @ARGV;
432 values @ARGV if values @$a;
434 # Anonymous arrays and hashes, and references to them
440 # SKIP ?$] < 5.010 && "smartmatch and given/when not implemented on this Perl version"
441 # implicit smartmatch in given/when
443 when ('bar') { continue; }
444 when ($_ ~~ 'quux') { continue; }
448 # conditions in elsifs (regression in change #33710 which fixed bug #37302)
451 elsif ($a and $b) { x(); }
452 elsif ($a or $b) { x(); }
455 # interpolation in regexps
459 # TODO new undocumented cpan-bug #33708
463 # TODO hash constants not yet fixed
465 use constant H => { "#" => 1 }; H->{"#"}
467 # TODO optimized away 0 not yet fixed
469 foreach my $i (@_) { 0 }
471 # tests with not, not optimized
474 x() if not $a and $b;
475 x() if $a and not $b;
476 x() unless not $a and $b;
477 x() unless $a and not $b;
480 x() unless not $a or $b;
481 x() unless $a or not $b;
482 x() if $a and not $b and $c;
483 x() if not $a and $b and not $c;
484 x() unless $a and not $b and $c;
485 x() unless not $a and $b and not $c;
486 x() if $a or not $b or $c;
487 x() if not $a or $b or not $c;
488 x() unless $a or not $b or $c;
489 x() unless not $a or $b or not $c;
491 # tests with not, optimized
495 x() if not $a and not $b;
496 x() unless not $a and not $b;
497 x() if not $a or not $b;
498 x() unless not $a or not $b;
499 x() if not $a and not $b and $c;
500 x() unless not $a and not $b and $c;
501 x() if not $a or not $b or $c;
502 x() unless not $a or not $b or $c;
503 x() if not $a and not $b and not $c;
504 x() unless not $a and not $b and not $c;
505 x() if not $a or not $b or not $c;
506 x() unless not $a or not $b or not $c;
507 x() unless not $a or not $b or not $c;
514 x() unless $a and $b;
516 x() if not $a || $b and $c;
517 x() unless not $a || $b and $c;
518 x() if not $a && $b or $c;
519 x() unless not $a && $b or $c;
520 x() unless $a or $b or $c;
521 x() if $a or $b or $c;
522 x() unless $a and $b and $c;
523 x() if $a and $b and $c;
524 x() unless not $a && $b && $c;
526 # tests that should be constant folded
530 x() if GLIPP && GLIPP;
531 x() if !GLIPP || GLIPP;
533 x() if do { no warnings 'void'; 5; GLIPP };
534 x() if do { !GLIPP };
535 if (GLIPP) { x() } else { z() }
536 if (!GLIPP) { x() } else { z() }
537 if (GLIPP) { x() } elsif (GLIPP) { z() }
538 if (!GLIPP) { x() } elsif (GLIPP) { z() }
539 if (GLIPP) { x() } elsif (!GLIPP) { z() }
540 if (!GLIPP) { x() } elsif (!GLIPP) { z() }
541 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (GLIPP) { t() }
542 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
543 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
577 # TODO constant deparsing has been backed out for 5.12
578 # XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
579 # tests that shouldn't be constant folded
580 # It might be fundamentally impossible to make this work on ithreads, in which
581 # case the TODO should become a SKIP
583 if ($a == 1) { x() } elsif ($b == 2) { z() }
584 if (do { foo(); GLIPP }) { x() }
585 if (do { $a++; GLIPP }) { x() }
588 if ($a == 1) { x(); } elsif ($b == 2) { z(); }
589 if (do { foo(); GLIPP }) { x(); }
590 if (do { ++$a; GLIPP }) { x(); }
592 # TODO constant deparsing has been backed out for 5.12
593 # tests for deparsing constants
596 # TODO constant deparsing has been backed out for 5.12
597 # tests for deparsing imported constants
600 # TODO constant deparsing has been backed out for 5.12
601 # tests for deparsing re-exported constants
604 # TODO constant deparsing has been backed out for 5.12
605 # tests for deparsing imported constants that got deleted from the original namespace
608 # TODO constant deparsing has been backed out for 5.12
609 # XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
610 # tests for deparsing constants which got turned into full typeglobs
611 # It might be fundamentally impossible to make this work on ithreads, in which
612 # case the TODO should become a SKIP
614 eval '@Fcntl::O_EXCL = qw/affe tiger/;';
617 # TODO constant deparsing has been backed out for 5.12
618 # tests for deparsing of blessed constant with overloaded numification
619 warn OVERLOADED_NUMIFICATION;
621 # TODO Only strict 'refs' currently supported
626 # TODO Subsets of warnings could be encoded textually, rather than as bitflips.
627 # subsets of warnings
628 no warnings 'deprecated';
631 # TODO Better test for CPAN #33708 - the deparsed code has different behaviour
647 # SKIP ?$] > 5.013006 && ":= is now a syntax error"
648 # := treated as an empty attribute list
655 # : = empty attribute list
674 # #71870 Use of uninitialized value in bitwise and B::Deparse
676 @a = split(/foo/, $s, 0);
678 @a = split(/$r/, $s, 0);
681 # package declaration before label
704 "foo" =~ (1 ? /foo/ : /bar/);
705 "foo" =~ (1 ? y/foo// : /bar/);
706 "foo" =~ (1 ? s/foo// : /bar/);
708 'foo' =~ ($_ =~ /foo/);
709 'foo' =~ ($_ =~ tr/fo//);
710 'foo' =~ ($_ =~ s/foo//);
712 # Test @threadsv_names under 5005threads
731 # readpipe with complex expression