6 if (($Config::Config{'extensions'} !~ /\bB\b/) ){
7 print "1..0 # Skip -- Perl configured without B module\n";
15 # BEGIN block is acutally a subroutine :-)
16 return unless $] > 5.009;
18 feature->import(':5.10');
20 use Test::More tests => 94;
24 my $deparse = B::Deparse->new();
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;
59 my ($num, $testname) = $1 =~ m/(\d+)\s*(.*)/;
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 diag("$num deparsed: $@");
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/, $testname);
93 use constant 'c', 'stuff';
94 is((eval "sub ".$deparse->coderef2text(\&c))->(), 'stuff');
97 is("{\n (-1) ** \$a;\n}", $deparse->coderef2text(sub{(-1) ** $a }));
99 use constant cr => ['hello'];
100 my $string = "sub " . $deparse->coderef2text(\&cr);
101 my $val = (eval $string)->() or diag $string;
102 is(ref($val), 'ARRAY');
103 is($val->[0], 'hello');
105 my $path = join " ", map { qq["-I$_"] } @INC;
107 $a = `$^X $path "-MO=Deparse" -anlwi.bak -e 1 2>&1`;
108 $a =~ s/-e syntax OK\n//g;
109 $a =~ s/.*possible typo.*\n//; # Remove warning line
110 $a =~ s{\\340\\242}{\\s} if (ord("\\") == 224); # EBCDIC, cp 1047 or 037
111 $a =~ s{\\274\\242}{\\s} if (ord("\\") == 188); # $^O eq 'posix-bc'
113 BEGIN { $^I = ".bak"; }
115 BEGIN { $/ = "\n"; $\ = "\n"; }
116 LINE: while (defined($_ = <ARGV>)) {
118 our(@F) = split(' ', $_, 0);
124 $a = `$^X $path "-MO=Deparse" -e "use constant PI => 4" 2>&1`;
125 $a =~ s/-e syntax OK\n//g;
126 is($a, "use constant ('PI', 4);\n",
127 "Proxy Constant Subroutines must not show up as (incorrect) prototypes");
129 #Re: perlbug #35857, patch #24505
130 #handle warnings::register-ed packages properly.
131 package B::Deparse::Wrapper;
134 use warnings::register;
136 my $deparser = B::Deparse->new();
137 return $deparser->coderef2text(shift);
141 use overload '0+' => sub { 42 };
146 use constant GLIPP => 'glipp';
147 use constant PI => 4;
148 use constant OVERLOADED_NUMIFICATION => bless({}, 'Moo');
149 use Fcntl qw/O_TRUNC O_APPEND O_EXCL/;
150 BEGIN { delete $::Fcntl::{O_APPEND}; }
151 use POSIX qw/O_CREAT/;
154 my $res = B::Deparse::Wrapper::getcode($val);
155 like( $res, qr/use warnings/);
158 my $x=sub { ++$q,++$p };
160 eval <<EOFCODE and test($x);
164 use warnings::register;
171 my $code = <<"EOCODE";
173 our \$\x{1e1f}\x{14d}\x{14d};
177 = $deparse->coderef2text(eval "sub { our \$\x{1e1f}\x{14d}\x{14d} }" );
178 s/$ \n//x for $deparsed, $code;
179 is $deparsed, $code, 'our $funny_Unicode_chars';
195 ++$test and $test /= 2;
198 $test /= 2 if ++$test;
205 my $test = sub : lvalue {
213 my $test = sub : method {
220 # Was sub : locked method { ... }
221 # This number could be re-used.
245 $_ .= <ARGV> . <$foo>;
248 my $foo = "Ab\x{100}\200\x{200}\377Cd\000Ef\x{1000}\cA\x{2000}\cZ";
253 # 16 - various lypes of loop
267 for (my $i = 0; $i < 2;) {
272 for (my $i = 0; $i < 2; ++$i) {
277 for (my $i = 0; $i < 2; ++$i) {
283 while ($i) { my $z = 1; } continue { $i = 99; }
286 foreach my $i (1, 2) {
298 foreach my $i (1, 2) {
303 foreach my $i (1, 2) {
308 foreach our $i (1, 2) {
314 foreach our $i (1, 2) {
320 print reverse sort(@x);
324 print((sort {$b cmp $a} @x));
328 print((reverse sort {$b <=> $a} @x));
332 print $_ foreach (reverse @a);
336 print $_ foreach (reverse 1, 2..5);
340 @ary = split(' ', 'foo', 0);
346 do { my $x = 1; $x };
348 # 37 <20061012113037.GJ25805@c4.convolution.nl>
375 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
379 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
383 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
384 # 47 state var assignment
386 my $y = (state $x = 42);
389 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
390 # 48 state vars in anoymous subroutines
397 # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
402 # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
403 # 50 keys @array; values @array
404 keys @$a if keys @ARGV;
405 values @ARGV if values @$a;
407 # 51 Anonymous arrays and hashes, and references to them
413 # SKIP ?$] < 5.010 && "smartmatch and given/when not implemented on this Perl version"
414 # 52 implicit smartmatch in given/when
416 when ('bar') { continue; }
417 when ($_ ~~ 'quux') { continue; }
421 # 53 conditions in elsifs (regression in change #33710 which fixed bug #37302)
424 elsif ($a and $b) { x(); }
425 elsif ($a or $b) { x(); }
428 # 54 interpolation in regexps
432 # TODO new undocumented cpan-bug #33708
433 # 55 (cpan-bug #33708)
436 # TODO hash constants not yet fixed
437 # 56 (cpan-bug #33708)
438 use constant H => { "#" => 1 }; H->{"#"}
440 # TODO optimized away 0 not yet fixed
441 # 57 (cpan-bug #33708)
442 foreach my $i (@_) { 0 }
444 # 58 tests with not, not optimized
447 x() if not $a and $b;
448 x() if $a and not $b;
449 x() unless not $a and $b;
450 x() unless $a and not $b;
453 x() unless not $a or $b;
454 x() unless $a or not $b;
455 x() if $a and not $b and $c;
456 x() if not $a and $b and not $c;
457 x() unless $a and not $b and $c;
458 x() unless not $a and $b and not $c;
459 x() if $a or not $b or $c;
460 x() if not $a or $b or not $c;
461 x() unless $a or not $b or $c;
462 x() unless not $a or $b or not $c;
464 # 59 tests with not, optimized
468 x() if not $a and not $b;
469 x() unless not $a and not $b;
470 x() if not $a or not $b;
471 x() unless not $a or not $b;
472 x() if not $a and not $b and $c;
473 x() unless not $a and not $b and $c;
474 x() if not $a or not $b or $c;
475 x() unless not $a or not $b or $c;
476 x() if not $a and not $b and not $c;
477 x() unless not $a and not $b and not $c;
478 x() if not $a or not $b or not $c;
479 x() unless not $a or not $b or not $c;
480 x() unless not $a or not $b or not $c;
487 x() unless $a and $b;
489 x() if not $a || $b and $c;
490 x() unless not $a || $b and $c;
491 x() if not $a && $b or $c;
492 x() unless not $a && $b or $c;
493 x() unless $a or $b or $c;
494 x() if $a or $b or $c;
495 x() unless $a and $b and $c;
496 x() if $a and $b and $c;
497 x() unless not $a && $b && $c;
499 # 60 tests that should be constant folded
503 x() if GLIPP && GLIPP;
504 x() if !GLIPP || GLIPP;
506 x() if do { no warnings 'void'; 5; GLIPP };
507 x() if do { !GLIPP };
508 if (GLIPP) { x() } else { z() }
509 if (!GLIPP) { x() } else { z() }
510 if (GLIPP) { x() } elsif (GLIPP) { z() }
511 if (!GLIPP) { x() } elsif (GLIPP) { z() }
512 if (GLIPP) { x() } elsif (!GLIPP) { z() }
513 if (!GLIPP) { x() } elsif (!GLIPP) { z() }
514 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (GLIPP) { t() }
515 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
516 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
550 # TODO constant deparsing has been backed out for 5.12
551 # XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
552 # 61 tests that shouldn't be constant folded
553 # It might be fundamentally impossible to make this work on ithreads, in which
554 # case the TODO should become a SKIP
556 if ($a == 1) { x() } elsif ($b == 2) { z() }
557 if (do { foo(); GLIPP }) { x() }
558 if (do { $a++; GLIPP }) { x() }
561 if ($a == 1) { x(); } elsif ($b == 2) { z(); }
562 if (do { foo(); GLIPP }) { x(); }
563 if (do { ++$a; GLIPP }) { x(); }
565 # TODO constant deparsing has been backed out for 5.12
566 # 62 tests for deparsing constants
569 # TODO constant deparsing has been backed out for 5.12
570 # 63 tests for deparsing imported constants
573 # TODO constant deparsing has been backed out for 5.12
574 # 64 tests for deparsing re-exported constants
577 # TODO constant deparsing has been backed out for 5.12
578 # 65 tests for deparsing imported constants that got deleted from the original namespace
581 # TODO constant deparsing has been backed out for 5.12
582 # XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
583 # 66 tests for deparsing constants which got turned into full typeglobs
584 # It might be fundamentally impossible to make this work on ithreads, in which
585 # case the TODO should become a SKIP
587 eval '@Fcntl::O_EXCL = qw/affe tiger/;';
590 # TODO constant deparsing has been backed out for 5.12
591 # 67 tests for deparsing of blessed constant with overloaded numification
592 warn OVERLOADED_NUMIFICATION;
594 # TODO Only strict 'refs' currently supported
599 # TODO Subsets of warnings could be encoded textually, rather than as bitflips.
600 no warnings 'deprecated';
603 # TODO Better test for CPAN #33708 - the deparsed code has different behaviour
640 @a = split(/foo/, $s, 0);
642 @a = split(/$r/, $s, 0);
663 "foo" =~ (1 ? /foo/ : /bar/);
664 "foo" =~ (1 ? y/foo// : /bar/);
665 "foo" =~ (1 ? s/foo// : /bar/);
667 'foo' =~ ($_ =~ /foo/);
668 'foo' =~ ($_ =~ tr/fo//);
669 'foo' =~ ($_ =~ s/foo//);
671 # Test @threadsv_names under 5005threads