DRAFT perlpacktut.pod - minor diffs to v0.1
[p5sagit/p5-mst-13.2.git] / ext / B / t / deparse.t
CommitLineData
87a42246 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 if ($^O eq 'MacOS') {
6 @INC = qw(: ::lib ::macos:lib);
7 } else {
8 @INC = '.';
9 push @INC, '../lib';
10 }
11}
12
13$| = 1;
14use warnings;
15use strict;
16use Config;
17
14a55f98 18print "1..15\n";
87a42246 19
20use B::Deparse;
21my $deparse = B::Deparse->new() or print "not ";
ad46c0be 22my $i=1;
d4a0c6f3 23print "ok " . $i++ . "\n";
ad46c0be 24
87a42246 25
26# Tell B::Deparse about our ambient pragmas
27{ my ($hint_bits, $warning_bits);
28 BEGIN {($hint_bits, $warning_bits) = ($^H, ${^WARNING_BITS})}
29 $deparse->ambient_pragmas (
30 hint_bits => $hint_bits,
31 warning_bits => $warning_bits,
32 '$[' => 0 + $[
33 );
34}
35
ad46c0be 36$/ = "\n####\n";
37while (<DATA>) {
38 chomp;
39 s/#.*$//mg;
87a42246 40
ad46c0be 41 my ($input, $expected);
42 if (/(.*)\n>>>>\n(.*)/s) {
43 ($input, $expected) = ($1, $2);
44 }
45 else {
46 ($input, $expected) = ($_, $_);
47 }
87a42246 48
ad46c0be 49 my $coderef = eval "sub {$input}";
87a42246 50
ad46c0be 51 if ($@) {
d4a0c6f3 52 print "not ok " . $i++ . "\n";
ad46c0be 53 print "# $@";
54 }
55 else {
56 my $deparsed = $deparse->coderef2text( $coderef );
57 my $regex = quotemeta($expected);
58 do {
59 no warnings 'misc';
60 $regex =~ s/\s+/\s+/g;
61 };
62
63 my $ok = ($deparsed =~ /^\{\s*$regex\s*\}$/);
d4a0c6f3 64 print (($ok ? "ok " : "not ok ") . $i++ . "\n");
ad46c0be 65 if (!$ok) {
66 print "# EXPECTED:\n";
67 $regex =~ s/^/# /mg;
68 print "$regex\n";
69
70 print "\n# GOT: \n";
71 $deparsed =~ s/^/# /mg;
72 print "$deparsed\n";
73 }
87a42246 74 }
87a42246 75}
76
87a42246 77use constant 'c', 'stuff';
78print "not " if (eval "sub ".$deparse->coderef2text(\&c))->() ne 'stuff';
d4a0c6f3 79print "ok " . $i++ . "\n";
87a42246 80
81$a = 0;
82print "not " if "{\n (-1) ** \$a;\n}"
83 ne $deparse->coderef2text(sub{(-1) ** $a });
d4a0c6f3 84print "ok " . $i++ . "\n";
87a42246 85
86# XXX ToDo - constsub that returns a reference
87#use constant cr => ['hello'];
88#my $string = "sub " . $deparse->coderef2text(\&cr);
89#my $val = (eval $string)->();
90#print "not " if ref($val) ne 'ARRAY' || $val->[0] ne 'hello';
d4a0c6f3 91#print "ok " . $i++ . "\n";
87a42246 92
93my $a;
94my $Is_VMS = $^O eq 'VMS';
95my $Is_MacOS = $^O eq 'MacOS';
96
97my $path = join " ", map { qq["-I$_"] } @INC;
be708cc0 98$path .= " -MMac::err=unix" if $Is_MacOS;
87a42246 99my $redir = $Is_MacOS ? "" : "2>&1";
100
101$a = `$^X $path "-MO=Deparse" -anle 1 $redir`;
be708cc0 102$a =~ s/(?:# )?-e syntax OK\n//g; # "# " for Mac OS
87a42246 103$a =~ s{\\340\\242}{\\s} if (ord("\\") == 224); # EBCDIC, cp 1047 or 037
104$a =~ s{\\274\\242}{\\s} if (ord("\\") == 188); # $^O eq 'posix-bc'
105$b = <<'EOF';
87a42246 106LINE: while (defined($_ = <ARGV>)) {
107 chomp $_;
14a55f98 108 our(@F) = split(" ", $_, 0);
87a42246 109 '???';
110}
87a42246 111EOF
f99a63a2 112print "# [$a]\n\# vs expected\n# [$b]\nnot " if $a ne $b;
d4a0c6f3 113print "ok " . $i++ . "\n";
87a42246 114
ad46c0be 115__DATA__
14a55f98 116# 2
ad46c0be 1171;
118####
14a55f98 119# 3
ad46c0be 120{
121 no warnings;
122 '???';
123 2;
124}
125####
14a55f98 126# 4
ad46c0be 127my $test;
128++$test and $test /= 2;
129>>>>
130my $test;
131$test /= 2 if ++$test;
132####
14a55f98 133# 5
ad46c0be 134-((1, 2) x 2);
135####
14a55f98 136# 6
ad46c0be 137{
138 my $test = sub : lvalue {
139 my $x;
140 }
141 ;
142}
143####
14a55f98 144# 7
ad46c0be 145{
146 my $test = sub : method {
147 my $x;
148 }
149 ;
150}
151####
14a55f98 152# 8
ad46c0be 153{
154 my $test = sub : locked method {
155 my $x;
156 }
157 ;
158}
159####
14a55f98 160# 9
87a42246 161{
ad46c0be 162 234;
f99a63a2 163}
ad46c0be 164continue {
165 123;
87a42246 166}
ce4e655d 167####
14a55f98 168# 10
ce4e655d 169my $x;
170print $main::x;
171####
14a55f98 172# 11
ce4e655d 173my @x;
174print $main::x[1];
14a55f98 175####
176# 12
177my %x;
178$x{warn()};