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');
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;
175 my $code = <<"EOCODE";
177 our \$\x{1e1f}\x{14d}\x{14d};
181 = $deparse->coderef2text(eval "sub { our \$\x{1e1f}\x{14d}\x{14d} }" );
182 s/$ \n//x for $deparsed, $code;
183 is $deparsed, $code, 'our $funny_Unicode_chars';
192 # Constants in a block
199 # Lexical and simple arithmetic
201 ++$test and $test /= 2;
204 $test /= 2 if ++$test;
211 my $test = sub : lvalue {
219 my $test = sub : method {
225 # block with continue
233 # lexical and package scalars
237 # lexical and package arrays
241 # lexical and package hashes
247 $_ .= <ARGV> . <$foo>;
250 my $foo = "Ab\x{100}\200\x{200}\377Cd\000Ef\x{1000}\cA\x{2000}\cZ";
268 # 2 arguments in a 3 argument for
269 for (my $i = 0; $i < 2;) {
274 for (my $i = 0; $i < 2; ++$i) {
278 # 3 argument for again
279 for (my $i = 0; $i < 2; ++$i) {
285 while ($i) { my $z = 1; } continue { $i = 99; }
288 foreach my $i (1, 2) {
300 foreach my $i (1, 2) {
305 foreach my $i (1, 2) {
310 foreach our $i (1, 2) {
314 # foreach with my and our
316 foreach our $i (1, 2) {
322 print reverse sort(@x);
326 print((sort {$b cmp $a} @x));
328 # reverse sort with block
330 print((reverse sort {$b <=> $a} @x));
334 print $_ foreach (reverse @a);
336 # foreach reverse (not inplace)
338 print $_ foreach (reverse 1, 2..5);
342 @ary = split(' ', 'foo', 0);
348 do { my $x = 1; $x };
350 # <20061012113037.GJ25805@c4.convolution.nl>
367 # variables as method names
371 # constants as method names
374 # constants as method names without ()
377 # SKIP ?$] < 5.010 && "say not implemented on this Perl version"
381 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
385 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
386 # state var assignment
388 my $y = (state $x = 42);
391 # SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
392 # state vars in anoymous subroutines
399 # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
404 # SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
405 # keys @array; values @array
406 keys @$a if keys @ARGV;
407 values @ARGV if values @$a;
409 # Anonymous arrays and hashes, and references to them
415 # SKIP ?$] < 5.010 && "smartmatch and given/when not implemented on this Perl version"
416 # implicit smartmatch in given/when
418 when ('bar') { continue; }
419 when ($_ ~~ 'quux') { continue; }
423 # conditions in elsifs (regression in change #33710 which fixed bug #37302)
426 elsif ($a and $b) { x(); }
427 elsif ($a or $b) { x(); }
430 # interpolation in regexps
434 # TODO new undocumented cpan-bug #33708
438 # TODO hash constants not yet fixed
440 use constant H => { "#" => 1 }; H->{"#"}
442 # TODO optimized away 0 not yet fixed
444 foreach my $i (@_) { 0 }
446 # tests with not, not optimized
449 x() if not $a and $b;
450 x() if $a and not $b;
451 x() unless not $a and $b;
452 x() unless $a and not $b;
455 x() unless not $a or $b;
456 x() unless $a or not $b;
457 x() if $a and not $b and $c;
458 x() if not $a and $b and not $c;
459 x() unless $a and not $b and $c;
460 x() unless not $a and $b and not $c;
461 x() if $a or not $b or $c;
462 x() if not $a or $b or not $c;
463 x() unless $a or not $b or $c;
464 x() unless not $a or $b or not $c;
466 # tests with not, optimized
470 x() if not $a and not $b;
471 x() unless not $a and not $b;
472 x() if not $a or not $b;
473 x() unless not $a or not $b;
474 x() if not $a and not $b and $c;
475 x() unless not $a and not $b and $c;
476 x() if not $a or not $b or $c;
477 x() unless not $a or not $b or $c;
478 x() if not $a and not $b and not $c;
479 x() unless not $a and not $b and not $c;
480 x() if not $a or not $b or not $c;
481 x() unless not $a or not $b or not $c;
482 x() unless not $a or not $b or not $c;
489 x() unless $a and $b;
491 x() if not $a || $b and $c;
492 x() unless not $a || $b and $c;
493 x() if not $a && $b or $c;
494 x() unless not $a && $b or $c;
495 x() unless $a or $b or $c;
496 x() if $a or $b or $c;
497 x() unless $a and $b and $c;
498 x() if $a and $b and $c;
499 x() unless not $a && $b && $c;
501 # tests that should be constant folded
505 x() if GLIPP && GLIPP;
506 x() if !GLIPP || GLIPP;
508 x() if do { no warnings 'void'; 5; GLIPP };
509 x() if do { !GLIPP };
510 if (GLIPP) { x() } else { z() }
511 if (!GLIPP) { x() } else { z() }
512 if (GLIPP) { x() } elsif (GLIPP) { z() }
513 if (!GLIPP) { x() } elsif (GLIPP) { z() }
514 if (GLIPP) { x() } elsif (!GLIPP) { z() }
515 if (!GLIPP) { x() } elsif (!GLIPP) { z() }
516 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (GLIPP) { t() }
517 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
518 if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
552 # TODO constant deparsing has been backed out for 5.12
553 # XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
554 # tests that shouldn't be constant folded
555 # It might be fundamentally impossible to make this work on ithreads, in which
556 # case the TODO should become a SKIP
558 if ($a == 1) { x() } elsif ($b == 2) { z() }
559 if (do { foo(); GLIPP }) { x() }
560 if (do { $a++; GLIPP }) { x() }
563 if ($a == 1) { x(); } elsif ($b == 2) { z(); }
564 if (do { foo(); GLIPP }) { x(); }
565 if (do { ++$a; GLIPP }) { x(); }
567 # TODO constant deparsing has been backed out for 5.12
568 # tests for deparsing constants
571 # TODO constant deparsing has been backed out for 5.12
572 # tests for deparsing imported constants
575 # TODO constant deparsing has been backed out for 5.12
576 # tests for deparsing re-exported constants
579 # TODO constant deparsing has been backed out for 5.12
580 # tests for deparsing imported constants that got deleted from the original namespace
583 # TODO constant deparsing has been backed out for 5.12
584 # XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
585 # tests for deparsing constants which got turned into full typeglobs
586 # It might be fundamentally impossible to make this work on ithreads, in which
587 # case the TODO should become a SKIP
589 eval '@Fcntl::O_EXCL = qw/affe tiger/;';
592 # TODO constant deparsing has been backed out for 5.12
593 # tests for deparsing of blessed constant with overloaded numification
594 warn OVERLOADED_NUMIFICATION;
596 # TODO Only strict 'refs' currently supported
601 # TODO Subsets of warnings could be encoded textually, rather than as bitflips.
602 # subsets of warnings
603 no warnings 'deprecated';
606 # TODO Better test for CPAN #33708 - the deparsed code has different behaviour
622 # SKIP ?$] > 5.013006 && ":= is now a syntax error"
623 # := treated as an empty attribute list
630 # : = empty attribute list
649 # #71870 Use of uninitialized value in bitwise and B::Deparse
651 @a = split(/foo/, $s, 0);
653 @a = split(/$r/, $s, 0);
656 # package declaration before label
679 "foo" =~ (1 ? /foo/ : /bar/);
680 "foo" =~ (1 ? y/foo// : /bar/);
681 "foo" =~ (1 ? s/foo// : /bar/);
683 'foo' =~ ($_ =~ /foo/);
684 'foo' =~ ($_ =~ tr/fo//);
685 'foo' =~ ($_ =~ s/foo//);
687 # Test @threadsv_names under 5005threads