This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add a 'context' feature to deparse.t, to better test ambient pragmas.
[perl5.git] / dist / B-Deparse / t / deparse.t
CommitLineData
87a42246
MS
1#!./perl
2
3BEGIN {
62a6bb71 4 unshift @INC, 't';
9cd8f857
NC
5 require Config;
6 if (($Config::Config{'extensions'} !~ /\bB\b/) ){
7 print "1..0 # Skip -- Perl configured without B module\n";
8 exit 0;
9 }
87a42246
MS
10}
11
87a42246
MS
12use warnings;
13use strict;
e9c69003 14BEGIN {
c4a6f826 15 # BEGIN block is actually a subroutine :-)
e9c69003
NC
16 return unless $] > 5.009;
17 require feature;
18 feature->import(':5.10');
19}
507a68aa 20use Test::More;
1bb3cfc5 21use Config ();
87a42246 22
d8cf01c3 23my $tests = 17; # not counting those in the __DATA__ section
3036b99c 24
87a42246 25use B::Deparse;
09d856fb 26my $deparse = B::Deparse->new();
507a68aa 27isa_ok($deparse, 'B::Deparse', 'instantiate a B::Deparse object');
87a42246 28
ad46c0be
RH
29$/ = "\n####\n";
30while (<DATA>) {
31 chomp;
d8cf01c3 32 $tests ++;
e9c69003
NC
33 # This code is pinched from the t/lib/common.pl for TODO.
34 # It's not clear how to avoid duplication
a6087f24
NC
35 my %meta = (context => '');
36 foreach my $what (qw(skip todo context)) {
c4a350e6 37 s/^#\s*\U$what\E\s*(.*)\n//m and $meta{$what} = $1;
b871937f
NC
38 # If the SKIP reason starts ? then it's taken as a code snippet to
39 # evaluate. This provides the flexibility to have conditional SKIPs
c4a350e6
NC
40 if ($meta{$what} && $meta{$what} =~ s/^\?//) {
41 my $temp = eval $meta{$what};
b871937f 42 if ($@) {
c4a350e6 43 die "# In \U$what\E code reason:\n# $meta{$what}\n$@";
b871937f 44 }
c4a350e6 45 $meta{$what} = $temp;
e9c69003 46 }
e9c69003
NC
47 }
48
4a4b8592 49 s/^\s*#\s*(.*)$//mg;
507a68aa
NC
50 my $desc = $1;
51 die "Missing name in test $_" unless defined $desc;
e9c69003 52
c4a350e6 53 if ($meta{skip}) {
e9c69003 54 # Like this to avoid needing a label SKIP:
c4a350e6 55 Test::More->builder->skip($meta{skip});
e9c69003
NC
56 next;
57 }
58
ad46c0be
RH
59 my ($input, $expected);
60 if (/(.*)\n>>>>\n(.*)/s) {
61 ($input, $expected) = ($1, $2);
62 }
63 else {
64 ($input, $expected) = ($_, $_);
65 }
87a42246 66
a6087f24
NC
67 my $coderef = eval "$meta{context};\n" . <<'EOC' . "sub {$input}";
68# Tell B::Deparse about our ambient pragmas
69my ($hint_bits, $warning_bits, $hinthash);
70BEGIN {
71 ($hint_bits, $warning_bits, $hinthash) = ($^H, ${^WARNING_BITS}, \%^H);
72}
73$deparse->ambient_pragmas (
74 hint_bits => $hint_bits,
75 warning_bits => $warning_bits,
76 '%^H' => $hinthash,
77);
78EOC
87a42246 79
ad46c0be 80 if ($@) {
507a68aa 81 is($@, "", "compilation of $desc");
ad46c0be
RH
82 }
83 else {
84 my $deparsed = $deparse->coderef2text( $coderef );
31c6271a
RD
85 my $regex = $expected;
86 $regex =~ s/(\S+)/\Q$1/g;
87 $regex =~ s/\s+/\\s+/g;
88 $regex = '^\{\s*' . $regex . '\s*\}$';
b871937f 89
c4a350e6 90 local $::TODO = $meta{todo};
507a68aa 91 like($deparsed, qr/$regex/, $desc);
87a42246 92 }
87a42246
MS
93}
94
87a42246 95use constant 'c', 'stuff';
507a68aa
NC
96is((eval "sub ".$deparse->coderef2text(\&c))->(), 'stuff',
97 'the subroutine generated by use constant deparses');
87a42246 98
09d856fb 99my $a = 0;
507a68aa
NC
100is($deparse->coderef2text(sub{(-1) ** $a }), "{\n (-1) ** \$a;\n}",
101 'anon sub capturing an external lexical');
87a42246 102
d989cdac
SM
103use constant cr => ['hello'];
104my $string = "sub " . $deparse->coderef2text(\&cr);
0707d6cc 105my $val = (eval $string)->() or diag $string;
507a68aa
NC
106is(ref($val), 'ARRAY', 'constant array references deparse');
107is($val->[0], 'hello', 'and return the correct value');
87a42246 108
87a42246 109my $path = join " ", map { qq["-I$_"] } @INC;
87a42246 110
7cde0a5f 111$a = `$^X $path "-MO=Deparse" -anlwi.bak -e 1 2>&1`;
e69a2255 112$a =~ s/-e syntax OK\n//g;
d2bc402e 113$a =~ s/.*possible typo.*\n//; # Remove warning line
87a42246
MS
114$a =~ s{\\340\\242}{\\s} if (ord("\\") == 224); # EBCDIC, cp 1047 or 037
115$a =~ s{\\274\\242}{\\s} if (ord("\\") == 188); # $^O eq 'posix-bc'
116$b = <<'EOF';
d2bc402e
RGS
117BEGIN { $^I = ".bak"; }
118BEGIN { $^W = 1; }
119BEGIN { $/ = "\n"; $\ = "\n"; }
87a42246
MS
120LINE: while (defined($_ = <ARGV>)) {
121 chomp $_;
f86ea535 122 our(@F) = split(' ', $_, 0);
87a42246
MS
123 '???';
124}
87a42246 125EOF
507a68aa
NC
126is($a, $b,
127 'command line flags deparse as BEGIN blocks setting control variables');
87a42246 128
5b4ee549
NC
129$a = `$^X $path "-MO=Deparse" -e "use constant PI => 4" 2>&1`;
130$a =~ s/-e syntax OK\n//g;
131is($a, "use constant ('PI', 4);\n",
132 "Proxy Constant Subroutines must not show up as (incorrect) prototypes");
133
579a54dc 134#Re: perlbug #35857, patch #24505
b3980c39
YO
135#handle warnings::register-ed packages properly.
136package B::Deparse::Wrapper;
137use strict;
138use warnings;
139use warnings::register;
140sub getcode {
579a54dc 141 my $deparser = B::Deparse->new();
b3980c39
YO
142 return $deparser->coderef2text(shift);
143}
144
2990415a
FR
145package Moo;
146use overload '0+' => sub { 42 };
147
b3980c39
YO
148package main;
149use strict;
150use warnings;
71c4dbc3 151use constant GLIPP => 'glipp';
2990415a
FR
152use constant PI => 4;
153use constant OVERLOADED_NUMIFICATION => bless({}, 'Moo');
3779476a 154use Fcntl qw/O_TRUNC O_APPEND O_EXCL/;
aaf9c2b2 155BEGIN { delete $::Fcntl::{O_APPEND}; }
2990415a 156use POSIX qw/O_CREAT/;
b3980c39 157sub test {
579a54dc
RGS
158 my $val = shift;
159 my $res = B::Deparse::Wrapper::getcode($val);
507a68aa
NC
160 like($res, qr/use warnings/,
161 '[perl #35857] [PATCH] B::Deparse doesnt handle warnings register properly');
b3980c39
YO
162}
163my ($q,$p);
164my $x=sub { ++$q,++$p };
165test($x);
166eval <<EOFCODE and test($x);
167 package bar;
168 use strict;
169 use warnings;
170 use warnings::register;
171 package main;
172 1
173EOFCODE
174
d1dc589d
FC
175# Exotic sub declarations
176$a = `$^X $path "-MO=Deparse" -e "sub ::::{}sub ::::::{}" 2>&1`;
177$a =~ s/-e syntax OK\n//g;
178is($a, <<'EOCODG', "sub :::: and sub ::::::");
179sub :::: {
180
181}
182sub :::::: {
183
184}
185EOCODG
186
640d5d41
FC
187# [perl #33752]
188{
189 my $code = <<"EOCODE";
190{
191 our \$\x{1e1f}\x{14d}\x{14d};
192}
193EOCODE
194 my $deparsed
195 = $deparse->coderef2text(eval "sub { our \$\x{1e1f}\x{14d}\x{14d} }" );
196 s/$ \n//x for $deparsed, $code;
197 is $deparsed, $code, 'our $funny_Unicode_chars';
198}
199
bdabb2d5
FC
200# [perl #62500]
201$a =
202 `$^X $path "-MO=Deparse" -e "BEGIN{*CORE::GLOBAL::require=sub{1}}" 2>&1`;
203$a =~ s/-e syntax OK\n//g;
204is($a, <<'EOCODF', "CORE::GLOBAL::require override causing panick");
205sub BEGIN {
206 *CORE::GLOBAL::require = sub {
207 1;
208 }
209 ;
210}
211EOCODF
212
894e98ac
FC
213# [perl #91384]
214$a =
215 `$^X $path "-MO=Deparse" -e "BEGIN{*Acme::Acme:: = *Acme::}" 2>&1`;
216like($a, qr/-e syntax OK/,
217 "Deparse does not hang when traversing stash circularities");
218
bb8996b8 219# [perl #93990]
5c19ffe6 220@* = ();
bb8996b8
HY
221is($deparse->coderef2text(sub{ print "@{*}" }),
222q<{
223 print "@{*}";
224}>, 'curly around to interpolate "@{*}"');
225is($deparse->coderef2text(sub{ print "@{-}" }),
226q<{
227 print "@-";
228}>, 'no need to curly around to interpolate "@-"');
229
1c74777c
FC
230# Strict hints in %^H are mercilessly suppressed
231$a =
232 `$^X $path "-MO=Deparse" -e "use strict; print;" 2>&1`;
233unlike($a, qr/BEGIN/,
234 "Deparse does not emit strict hh hints");
235
3036b99c
FC
236# ambient_pragmas should not mess with strict settings.
237SKIP: {
238 skip "requires 5.11", 1 unless $] >= 5.011;
239 eval q`
240 no strict;
241 BEGIN {
242 # Clear out the strict hints from %^H
243 %^H = ();
244 new B::Deparse -> ambient_pragmas(strict => 'all');
245 }
246 use 5.011; # should enable strict
247 ok !eval '$do_noT_create_a_variable_with_this_name = 1',
248 'ambient_pragmas do not mess with compiling scope';
249 `;
250}
251
d8cf01c3 252done_testing($tests);
507a68aa 253
ad46c0be 254__DATA__
507a68aa 255# A constant
ad46c0be
RH
2561;
257####
507a68aa 258# Constants in a block
ad46c0be
RH
259{
260 no warnings;
261 '???';
262 2;
263}
264####
507a68aa 265# Lexical and simple arithmetic
ad46c0be
RH
266my $test;
267++$test and $test /= 2;
268>>>>
269my $test;
270$test /= 2 if ++$test;
271####
507a68aa 272# list x
ad46c0be
RH
273-((1, 2) x 2);
274####
507a68aa 275# lvalue sub
ad46c0be
RH
276{
277 my $test = sub : lvalue {
278 my $x;
279 }
280 ;
281}
282####
507a68aa 283# method
ad46c0be
RH
284{
285 my $test = sub : method {
286 my $x;
287 }
288 ;
289}
290####
507a68aa 291# block with continue
87a42246 292{
ad46c0be 293 234;
f99a63a2 294}
ad46c0be
RH
295continue {
296 123;
87a42246 297}
ce4e655d 298####
507a68aa 299# lexical and package scalars
ce4e655d
RH
300my $x;
301print $main::x;
302####
507a68aa 303# lexical and package arrays
ce4e655d
RH
304my @x;
305print $main::x[1];
14a55f98 306####
507a68aa 307# lexical and package hashes
14a55f98
RH
308my %x;
309$x{warn()};
ad8caead 310####
507a68aa 311# <>
ad8caead
RGS
312my $foo;
313$_ .= <ARGV> . <$foo>;
cef22867 314####
507a68aa 315# \x{}
11454c59 316my $foo = "Ab\x{100}\200\x{200}\237Cd\000Ef\x{1000}\cA\x{2000}\cZ";
4ae52e81 317####
507a68aa 318# s///e
4ae52e81 319s/x/'y';/e;
241416b8 320####
507a68aa 321# block
241416b8
DM
322{ my $x; }
323####
507a68aa 324# while 1
241416b8
DM
325while (1) { my $k; }
326####
507a68aa 327# trailing for
241416b8
DM
328my ($x,@a);
329$x=1 for @a;
330>>>>
331my($x, @a);
0bb5f065 332$x = 1 foreach (@a);
241416b8 333####
507a68aa 334# 2 arguments in a 3 argument for
241416b8
DM
335for (my $i = 0; $i < 2;) {
336 my $z = 1;
337}
338####
507a68aa 339# 3 argument for
241416b8
DM
340for (my $i = 0; $i < 2; ++$i) {
341 my $z = 1;
342}
343####
507a68aa 344# 3 argument for again
241416b8
DM
345for (my $i = 0; $i < 2; ++$i) {
346 my $z = 1;
347}
348####
507a68aa 349# while/continue
241416b8
DM
350my $i;
351while ($i) { my $z = 1; } continue { $i = 99; }
352####
507a68aa 353# foreach with my
09d856fb 354foreach my $i (1, 2) {
241416b8
DM
355 my $z = 1;
356}
357####
507a68aa 358# foreach
241416b8
DM
359my $i;
360foreach $i (1, 2) {
361 my $z = 1;
362}
363####
507a68aa 364# foreach, 2 mys
241416b8
DM
365my $i;
366foreach my $i (1, 2) {
367 my $z = 1;
368}
369####
507a68aa 370# foreach
241416b8
DM
371foreach my $i (1, 2) {
372 my $z = 1;
373}
374####
507a68aa 375# foreach with our
241416b8
DM
376foreach our $i (1, 2) {
377 my $z = 1;
378}
379####
507a68aa 380# foreach with my and our
241416b8
DM
381my $i;
382foreach our $i (1, 2) {
383 my $z = 1;
384}
3ac6e0f9 385####
507a68aa 386# reverse sort
3ac6e0f9
RGS
387my @x;
388print reverse sort(@x);
389####
507a68aa 390# sort with cmp
3ac6e0f9
RGS
391my @x;
392print((sort {$b cmp $a} @x));
393####
507a68aa 394# reverse sort with block
3ac6e0f9
RGS
395my @x;
396print((reverse sort {$b <=> $a} @x));
36d57d93 397####
507a68aa 398# foreach reverse
36d57d93
RGS
399our @a;
400print $_ foreach (reverse @a);
aae53c41 401####
507a68aa 402# foreach reverse (not inplace)
aae53c41
RGS
403our @a;
404print $_ foreach (reverse 1, 2..5);
f86ea535 405####
507a68aa 406# bug #38684
f86ea535
SM
407our @ary;
408@ary = split(' ', 'foo', 0);
31c6271a 409####
507a68aa 410# bug #40055
31c6271a
RD
411do { () };
412####
507a68aa 413# bug #40055
31c6271a 414do { my $x = 1; $x };
d9002312 415####
507a68aa 416# <20061012113037.GJ25805@c4.convolution.nl>
d9002312
SM
417my $f = sub {
418 +{[]};
419} ;
8b2d6640 420####
507a68aa 421# bug #43010
8b2d6640
FC
422'!@$%'->();
423####
507a68aa 424# bug #43010
8b2d6640
FC
425::();
426####
507a68aa 427# bug #43010
8b2d6640
FC
428'::::'->();
429####
507a68aa 430# bug #43010
8b2d6640 431&::::;
09d856fb 432####
1b38d782
FC
433# [perl #77172]
434package rt77172;
435sub foo {} foo & & & foo;
436>>>>
437package rt77172;
438foo(&{&} & foo());
439####
507a68aa 440# variables as method names
09d856fb
CK
441my $bar;
442'Foo'->$bar('orz');
35a99a08 443'Foo'->$bar('orz') = 'a stranger stranger than before';
09d856fb 444####
507a68aa 445# constants as method names
09d856fb
CK
446'Foo'->bar('orz');
447####
507a68aa 448# constants as method names without ()
09d856fb 449'Foo'->bar;
0ced6c29 450####
28bfcb02 451# [perl #47359] "indirect" method call notation
1bf8bbb0
FC
452our @bar;
453foo{@bar}+1,->foo;
454(foo{@bar}+1),foo();
455foo{@bar}1 xor foo();
456>>>>
457our @bar;
458(foo { @bar } 1)->foo;
459(foo { @bar } 1), foo();
460foo { @bar } 1 xor foo();
461####
e9c69003 462# SKIP ?$] < 5.010 && "say not implemented on this Perl version"
507a68aa 463# say
7ddd1a01
NC
464say 'foo';
465####
e9c69003 466# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
507a68aa 467# state vars
0ced6c29
RGS
468state $x = 42;
469####
e9c69003 470# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
507a68aa 471# state var assignment
7ddd1a01
NC
472{
473 my $y = (state $x = 42);
474}
475####
e9c69003 476# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
c4a6f826 477# state vars in anonymous subroutines
7ddd1a01
NC
478$a = sub {
479 state $x;
480 return $x++;
481}
482;
644741fd
NC
483####
484# SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
507a68aa 485# each @array;
644741fd
NC
486each @ARGV;
487each @$a;
488####
489# SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
507a68aa 490# keys @array; values @array
644741fd
NC
491keys @$a if keys @ARGV;
492values @ARGV if values @$a;
35925e80 493####
507a68aa 494# Anonymous arrays and hashes, and references to them
35925e80
RGS
495my $a = {};
496my $b = \{};
497my $c = [];
498my $d = \[];
9210de83
FR
499####
500# SKIP ?$] < 5.010 && "smartmatch and given/when not implemented on this Perl version"
507a68aa 501# implicit smartmatch in given/when
9210de83
FR
502given ('foo') {
503 when ('bar') { continue; }
504 when ($_ ~~ 'quux') { continue; }
505 default { 0; }
506}
7ecdd211 507####
507a68aa 508# conditions in elsifs (regression in change #33710 which fixed bug #37302)
7ecdd211
PJ
509if ($a) { x(); }
510elsif ($b) { x(); }
511elsif ($a and $b) { x(); }
512elsif ($a or $b) { x(); }
513else { x(); }
03b22f1b 514####
507a68aa 515# interpolation in regexps
03b22f1b
RGS
516my($y, $t);
517/x${y}z$t/;
227375e1 518####
4a4b8592 519# TODO new undocumented cpan-bug #33708
507a68aa 520# cpan-bug #33708
227375e1
RU
521%{$_ || {}}
522####
4a4b8592 523# TODO hash constants not yet fixed
507a68aa 524# cpan-bug #33708
227375e1
RU
525use constant H => { "#" => 1 }; H->{"#"}
526####
4a4b8592 527# TODO optimized away 0 not yet fixed
507a68aa 528# cpan-bug #33708
227375e1 529foreach my $i (@_) { 0 }
edbe35ea 530####
507a68aa 531# tests with not, not optimized
07f3cdf5 532my $c;
edbe35ea
VP
533x() unless $a;
534x() if not $a and $b;
535x() if $a and not $b;
536x() unless not $a and $b;
537x() unless $a and not $b;
538x() if not $a or $b;
539x() if $a or not $b;
540x() unless not $a or $b;
541x() unless $a or not $b;
07f3cdf5
VP
542x() if $a and not $b and $c;
543x() if not $a and $b and not $c;
544x() unless $a and not $b and $c;
545x() unless not $a and $b and not $c;
546x() if $a or not $b or $c;
547x() if not $a or $b or not $c;
548x() unless $a or not $b or $c;
549x() unless not $a or $b or not $c;
edbe35ea 550####
507a68aa 551# tests with not, optimized
07f3cdf5 552my $c;
edbe35ea
VP
553x() if not $a;
554x() unless not $a;
555x() if not $a and not $b;
556x() unless not $a and not $b;
557x() if not $a or not $b;
558x() unless not $a or not $b;
07f3cdf5
VP
559x() if not $a and not $b and $c;
560x() unless not $a and not $b and $c;
561x() if not $a or not $b or $c;
562x() unless not $a or not $b or $c;
563x() if not $a and not $b and not $c;
564x() unless not $a and not $b and not $c;
565x() if not $a or not $b or not $c;
566x() unless not $a or not $b or not $c;
567x() unless not $a or not $b or not $c;
edbe35ea 568>>>>
07f3cdf5 569my $c;
edbe35ea
VP
570x() unless $a;
571x() if $a;
572x() unless $a or $b;
573x() if $a or $b;
574x() unless $a and $b;
07f3cdf5
VP
575x() if $a and $b;
576x() if not $a || $b and $c;
577x() unless not $a || $b and $c;
578x() if not $a && $b or $c;
579x() unless not $a && $b or $c;
580x() unless $a or $b or $c;
581x() if $a or $b or $c;
582x() unless $a and $b and $c;
583x() if $a and $b and $c;
584x() unless not $a && $b && $c;
71c4dbc3 585####
507a68aa 586# tests that should be constant folded
71c4dbc3
VP
587x() if 1;
588x() if GLIPP;
589x() if !GLIPP;
590x() if GLIPP && GLIPP;
591x() if !GLIPP || GLIPP;
592x() if do { GLIPP };
593x() if do { no warnings 'void'; 5; GLIPP };
594x() if do { !GLIPP };
595if (GLIPP) { x() } else { z() }
596if (!GLIPP) { x() } else { z() }
597if (GLIPP) { x() } elsif (GLIPP) { z() }
598if (!GLIPP) { x() } elsif (GLIPP) { z() }
599if (GLIPP) { x() } elsif (!GLIPP) { z() }
600if (!GLIPP) { x() } elsif (!GLIPP) { z() }
601if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (GLIPP) { t() }
602if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
603if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
604>>>>
605x();
606x();
607'???';
608x();
609x();
610x();
611x();
612do {
613 '???'
614};
615do {
616 x()
617};
618do {
619 z()
620};
621do {
622 x()
623};
624do {
625 z()
626};
627do {
628 x()
629};
630'???';
631do {
632 t()
633};
634'???';
635!1;
636####
719c50dc
RGS
637# TODO constant deparsing has been backed out for 5.12
638# XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
507a68aa 639# tests that shouldn't be constant folded
ac0f1413
NC
640# It might be fundamentally impossible to make this work on ithreads, in which
641# case the TODO should become a SKIP
71c4dbc3
VP
642x() if $a;
643if ($a == 1) { x() } elsif ($b == 2) { z() }
644if (do { foo(); GLIPP }) { x() }
645if (do { $a++; GLIPP }) { x() }
646>>>>
647x() if $a;
648if ($a == 1) { x(); } elsif ($b == 2) { z(); }
2990415a
FR
649if (do { foo(); GLIPP }) { x(); }
650if (do { ++$a; GLIPP }) { x(); }
651####
0fa4a265 652# TODO constant deparsing has been backed out for 5.12
507a68aa 653# tests for deparsing constants
2990415a
FR
654warn PI;
655####
0fa4a265 656# TODO constant deparsing has been backed out for 5.12
507a68aa 657# tests for deparsing imported constants
3779476a 658warn O_TRUNC;
2990415a 659####
0fa4a265 660# TODO constant deparsing has been backed out for 5.12
507a68aa 661# tests for deparsing re-exported constants
2990415a
FR
662warn O_CREAT;
663####
0fa4a265 664# TODO constant deparsing has been backed out for 5.12
507a68aa 665# tests for deparsing imported constants that got deleted from the original namespace
aaf9c2b2 666warn O_APPEND;
2990415a 667####
0fa4a265
DM
668# TODO constant deparsing has been backed out for 5.12
669# XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
507a68aa 670# tests for deparsing constants which got turned into full typeglobs
ac0f1413
NC
671# It might be fundamentally impossible to make this work on ithreads, in which
672# case the TODO should become a SKIP
2990415a
FR
673warn O_EXCL;
674eval '@Fcntl::O_EXCL = qw/affe tiger/;';
675warn O_EXCL;
676####
0fa4a265 677# TODO constant deparsing has been backed out for 5.12
507a68aa 678# tests for deparsing of blessed constant with overloaded numification
2990415a 679warn OVERLOADED_NUMIFICATION;
79289e05 680####
507a68aa 681# strict
79289e05 682no strict;
415d4c68
FC
683print $x;
684use strict 'vars';
685print $main::x;
686use strict 'subs';
687print $main::x;
688use strict 'refs';
689print $main::x;
690no strict 'vars';
79289e05
NC
691$x;
692####
693# TODO Subsets of warnings could be encoded textually, rather than as bitflips.
507a68aa 694# subsets of warnings
79289e05
NC
695no warnings 'deprecated';
696my $x;
697####
698# TODO Better test for CPAN #33708 - the deparsed code has different behaviour
507a68aa 699# CPAN #33708
79289e05
NC
700use strict;
701no warnings;
702
703foreach (0..3) {
704 my $x = 2;
705 {
706 my $x if 0;
707 print ++$x, "\n";
708 }
709}
d83f38d8 710####
507a68aa 711# no attribute list
d83f38d8
NC
712my $pi = 4;
713####
2dc78664
NC
714# SKIP ?$] > 5.013006 && ":= is now a syntax error"
715# := treated as an empty attribute list
d83f38d8
NC
716no warnings;
717my $pi := 4;
718>>>>
719no warnings;
720my $pi = 4;
721####
507a68aa 722# : = empty attribute list
d83f38d8
NC
723my $pi : = 4;
724>>>>
725my $pi = 4;
689e417f 726####
507a68aa 727# in place sort
689e417f
VP
728our @a;
729my @b;
730@a = sort @a;
731@b = sort @b;
732();
733####
507a68aa 734# in place reverse
689e417f
VP
735our @a;
736my @b;
737@a = reverse @a;
738@b = reverse @b;
739();
06fc6867 740####
507a68aa 741# #71870 Use of uninitialized value in bitwise and B::Deparse
06fc6867
VP
742my($r, $s, @a);
743@a = split(/foo/, $s, 0);
744$r = qr/foo/;
745@a = split(/$r/, $s, 0);
746();
98a1a137 747####
507a68aa 748# package declaration before label
98a1a137
Z
749{
750 package Foo;
751 label: print 123;
752}
538f5756 753####
507a68aa 754# shift optimisation
538f5756
RZ
755shift;
756>>>>
757shift();
758####
507a68aa 759# shift optimisation
538f5756
RZ
760shift @_;
761####
507a68aa 762# shift optimisation
538f5756
RZ
763pop;
764>>>>
765pop();
766####
507a68aa 767# shift optimisation
538f5756 768pop @_;
a539498a 769####
507a68aa 770#[perl #20444]
a539498a
FC
771"foo" =~ (1 ? /foo/ : /bar/);
772"foo" =~ (1 ? y/foo// : /bar/);
5e5a1632 773"foo" =~ (1 ? y/foo//r : /bar/);
a539498a
FC
774"foo" =~ (1 ? s/foo// : /bar/);
775>>>>
776'foo' =~ ($_ =~ /foo/);
777'foo' =~ ($_ =~ tr/fo//);
5e5a1632 778'foo' =~ ($_ =~ tr/fo//r);
a539498a 779'foo' =~ ($_ =~ s/foo//);
e0ab66ad 780####
5e5a1632
FC
781# The fix for [perl #20444] broke this.
782'foo' =~ do { () };
783####
4b58603b
FC
784# [perl #81424] match against aelemfast_lex
785my @s;
786print /$s[1]/;
787####
36727b53
FC
788# /$#a/
789print /$#main::a/;
790####
b9bc576f 791# [perl #91318] /regexp/applaud
09622ee2
FC
792print /a/a, s/b/c/a;
793print /a/aa, s/b/c/aa;
794print /a/p, s/b/c/p;
795print /a/l, s/b/c/l;
796print /a/u, s/b/c/u;
b9bc576f
FC
797{
798 use feature "unicode_strings";
09622ee2 799 print /a/d, s/b/c/d;
b9bc576f
FC
800}
801{
802 use re "/u";
09622ee2 803 print /a/d, s/b/c/d;
b9bc576f 804}
dff5ffe4
FC
805{
806 use 5.012;
807 print /a/d, s/b/c/d;
808}
b9bc576f 809>>>>
09622ee2
FC
810print /a/a, s/b/c/a;
811print /a/aa, s/b/c/aa;
812print /a/p, s/b/c/p;
813print /a/l, s/b/c/l;
814print /a/u, s/b/c/u;
b9bc576f 815{
a8095af7 816 use feature 'unicode_strings';
09622ee2 817 print /a/d, s/b/c/d;
b9bc576f
FC
818}
819{
0bb01b05
FC
820 BEGIN { $^H{'reflags'} = '0';
821 $^H{'reflags_charset'} = '2'; }
09622ee2 822 print /a/d, s/b/c/d;
b9bc576f 823}
dff5ffe4
FC
824{
825 no feature;
826 use feature ':5.12';
827 print /a/d, s/b/c/d;
828}
b9bc576f 829####
e0ab66ad
NC
830# Test @threadsv_names under 5005threads
831foreach $' (1, 2) {
832 sleep $';
833}
e7afc405
FC
834####
835# y///r
836tr/a/b/r;
cb8157e3
FC
837####
838# y/uni/code/
839tr/\x{345}/\x{370}/;
cb8578ff
FC
840####
841# [perl #90898]
f4002a4b 842<a,>;
09dcfa7d
FC
843####
844# [perl #91008]
845each $@;
846keys $~;
847values $!;
5d8c42c2
FC
848####
849# readpipe with complex expression
850readpipe $a + $b;
93bad3fd
NC
851####
852# aelemfast
853$b::a[0] = 1;
854####
855# aelemfast for a lexical
856my @a;
857$a[0] = 1;
80e3f4ad
FC
858####
859# feature features without feature
a8095af7 860no feature 'say', 'state', 'switch';
80e3f4ad 861CORE::state $x;
223b1722
FC
862CORE::say $x;
863CORE::given ($x) {
864 CORE::when (3) {
865 continue;
866 }
867 CORE::default {
868 CORE::break;
869 }
870}
871CORE::evalbytes '';
872() = CORE::__SUB__;
838f2281 873() = CORE::fc $x;
223b1722
FC
874####
875# feature features when feature has been disabled by use VERSION
876use feature (sprintf(":%vd", $^V));
877use 1;
878CORE::state $x;
879CORE::say $x;
880CORE::given ($x) {
881 CORE::when (3) {
882 continue;
883 }
884 CORE::default {
885 CORE::break;
886 }
887}
888CORE::evalbytes '';
889() = CORE::__SUB__;
890>>>>
0bb01b05
FC
891no feature;
892use feature ':default';
223b1722 893CORE::state $x;
80e3f4ad
FC
894CORE::say $x;
895CORE::given ($x) {
896 CORE::when (3) {
897 continue;
898 }
899 CORE::default {
e36901c8 900 CORE::break;
80e3f4ad
FC
901 }
902}
7d789282 903CORE::evalbytes '';
84ed0108 904() = CORE::__SUB__;
6ec73527 905####
0bb01b05
FC
906# Feature hints
907use feature 'current_sub', 'evalbytes';
908print;
909use 1;
910print;
911use 5.014;
912print;
913no feature 'unicode_strings';
914print;
915>>>>
a8095af7 916use feature 'current_sub', 'evalbytes';
0bb01b05
FC
917print $_;
918no feature;
919use feature ':default';
920print $_;
921no feature;
922use feature ':5.12';
923print $_;
a8095af7 924no feature 'unicode_strings';
0bb01b05
FC
925print $_;
926####
6ec73527
FC
927# $#- $#+ $#{%} etc.
928my @x;
929@x = ($#{`}, $#{~}, $#{!}, $#{@}, $#{$}, $#{%}, $#{^}, $#{&}, $#{*});
930@x = ($#{(}, $#{)}, $#{[}, $#{{}, $#{]}, $#{}}, $#{'}, $#{"}, $#{,});
931@x = ($#{<}, $#{.}, $#{>}, $#{/}, $#{?}, $#{=}, $#+, $#{\}, $#{|}, $#-);
932@x = ($#{;}, $#{:});
61154ac0
FC
933####
934# ${#} interpolated (the first line magically disables the warning)
935() = *#;
936() = "${#}a";
958ed56b 937####
337d7381
FC
938# [perl #86060] $( $| $) in regexps need braces
939/${(}/;
940/${|}/;
941/${)}/;
942/${(}${|}${)}/;
943####
958ed56b
FC
944# ()[...]
945my(@a) = ()[()];
521795fe
FC
946####
947# sort(foo(bar))
948# sort(foo(bar)) is interpreted as sort &foo(bar)
949# sort foo(bar) is interpreted as sort foo bar
950# parentheses are not optional in this case
951print sort(foo('bar'));
952>>>>
953print sort(foo('bar'));
24fcb59f
FC
954####
955# substr assignment
956substr(my $a, 0, 0) = (foo(), bar());
957$a++;
04be0204
FC
958####
959# hint hash
960BEGIN { $^H{'foo'} = undef; }
961{
962 BEGIN { $^H{'bar'} = undef; }
963 {
964 BEGIN { $^H{'baz'} = undef; }
965 {
966 print $_;
967 }
968 print $_;
969 }
970 print $_;
971}
035146a3
FC
972BEGIN { $^H{q[']} = '('; }
973print $_;
c306e834
FC
974####
975# hint hash changes that serialise the same way with sort %hh
976BEGIN { $^H{'a'} = 'b'; }
977{
978 BEGIN { $^H{'b'} = 'a'; delete $^H{'a'}; }
979 print $_;
980}
981print $_;
94bb57f9
FC
982####
983# [perl #47361] do({}) and do +{} (variants of do-file)
984do({});
985do +{};
8b46c09b
FC
986sub foo::do {}
987package foo;
988CORE::do({});
989CORE::do +{};
94bb57f9
FC
990>>>>
991do({});
992do({});
8b46c09b
FC
993package foo;
994CORE::do({});
995CORE::do({});
9c56d9ea
FC
996####
997# [perl #77096] functions that do not follow the llafr
998() = (return 1) + time;
999() = (return ($1 + $2) * $3) + time;
1000() = (return ($a xor $b)) + time;
1001() = (do 'file') + time;
1002() = (do ($1 + $2) * $3) + time;
1003() = (do ($1 xor $2)) + time;
41df74e3
FC
1004() = (goto 1) + 3;
1005() = (require 'foo') + 3;
1006() = (require foo) + 3;
266da325 1007() = (CORE::dump 1) + 3;
41df74e3
FC
1008() = (last 1) + 3;
1009() = (next 1) + 3;
1010() = (redo 1) + 3;
5830412d
FC
1011() = (-R $_) + 3;
1012() = (-W $_) + 3;
1013() = (-X $_) + 3;
1014() = (-r $_) + 3;
1015() = (-w $_) + 3;
1016() = (-x $_) + 3;
2462c3cc 1017####
1cabb3b3
FC
1018# [perl #97476] not() *does* follow the llafr
1019$_ = ($a xor not +($1 || 2) ** 2);
1020####
4d8ac5c7
FC
1021# Precedence conundrums with argument-less function calls
1022() = (eof) + 1;
1023() = (return) + 1;
1024() = (return, 1);
7bc8c979
FC
1025() = warn;
1026() = warn() + 1;
4d8ac5c7
FC
1027() = setpgrp() + 1;
1028####
2462c3cc
FC
1029# [perl #63558] open local(*FH)
1030open local *FH;
564cd6cb 1031pipe local *FH, local *FH;
843b15cc 1032####
b89b7257
FC
1033# [perl #91416] open "string"
1034open 'open';
1035open '####';
1036open '^A';
1037open "\ca";
1038>>>>
1039open *open;
1040open '####';
1041open '^A';
1042open *^A;
1043####
be6cf5cf
FC
1044# "string"->[] ->{}
1045no strict 'vars';
1046() = 'open'->[0]; #aelemfast
1047() = '####'->[0];
1048() = '^A'->[0];
1049() = "\ca"->[0];
b861b87f 1050() = 'a::]b'->[0];
10e8e32b
FC
1051() = 'open'->[$_]; #aelem
1052() = '####'->[$_];
1053() = '^A'->[$_];
1054() = "\ca"->[$_];
b861b87f 1055() = 'a::]b'->[$_];
10e8e32b
FC
1056() = 'open'->{0}; #helem
1057() = '####'->{0};
1058() = '^A'->{0};
1059() = "\ca"->{0};
b861b87f 1060() = 'a::]b'->{0};
be6cf5cf 1061>>>>
415d4c68 1062no strict 'vars';
be6cf5cf
FC
1063() = $open[0];
1064() = '####'->[0];
1065() = '^A'->[0];
1066() = $^A[0];
b861b87f 1067() = 'a::]b'->[0];
10e8e32b
FC
1068() = $open[$_];
1069() = '####'->[$_];
1070() = '^A'->[$_];
1071() = $^A[$_];
b861b87f 1072() = 'a::]b'->[$_];
10e8e32b
FC
1073() = $open{'0'};
1074() = '####'->{'0'};
1075() = '^A'->{'0'};
1076() = $^A{'0'};
b861b87f 1077() = 'a::]b'->{'0'};
be6cf5cf 1078####
843b15cc
FC
1079# [perl #74740] -(f()) vs -f()
1080$_ = -(f());
c75b4828
FC
1081####
1082# require <binop>
1083require 'a' . $1;
afb60448
HY
1084####
1085#[perl #30504] foreach-my postfix/prefix difference
1086$_ = 'foo' foreach my ($foo1, $bar1, $baz1);
1087foreach (my ($foo2, $bar2, $baz2)) { $_ = 'foo' }
1088foreach my $i (my ($foo3, $bar3, $baz3)) { $i = 'foo' }
1089>>>>
1090$_ = 'foo' foreach (my($foo1, $bar1, $baz1));
1091foreach $_ (my($foo2, $bar2, $baz2)) {
1092 $_ = 'foo';
1093}
1094foreach my $i (my($foo3, $bar3, $baz3)) {
1095 $i = 'foo';
1096}
1097####
1098#[perl #108224] foreach with continue block
1099foreach (1 .. 3) { print } continue { print "\n" }
1100foreach (1 .. 3) { } continue { }
1101foreach my $i (1 .. 3) { print $i } continue { print "\n" }
1102foreach my $i (1 .. 3) { } continue { }
1103>>>>
1104foreach $_ (1 .. 3) {
1105 print $_;
1106}
1107continue {
1108 print "\n";
1109}
1110foreach $_ (1 .. 3) {
1111 ();
1112}
1113continue {
1114 ();
1115}
1116foreach my $i (1 .. 3) {
1117 print $i;
1118}
1119continue {
1120 print "\n";
1121}
1122foreach my $i (1 .. 3) {
1123 ();
1124}
1125continue {
1126 ();
1127}