This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Deparse: mark "$a[0]\[1]" TODO
[perl5.git] / lib / B / Deparse.t
CommitLineData
87a42246
MS
1#!./perl
2
3BEGIN {
738f9dbf 4 splice @INC, 0, 0, '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 }
738f9dbf 10 require 'test.pl';
87a42246
MS
11}
12
87a42246
MS
13use warnings;
14use strict;
87a42246 15
7d9a919c 16my $tests = 52; # not counting those in the __DATA__ section
3036b99c 17
87a42246 18use B::Deparse;
09d856fb 19my $deparse = B::Deparse->new();
507a68aa 20isa_ok($deparse, 'B::Deparse', 'instantiate a B::Deparse object');
4da9a2ca 21my %deparse;
87a42246 22
ad46c0be
RH
23$/ = "\n####\n";
24while (<DATA>) {
25 chomp;
d8cf01c3 26 $tests ++;
e9c69003
NC
27 # This code is pinched from the t/lib/common.pl for TODO.
28 # It's not clear how to avoid duplication
a6087f24 29 my %meta = (context => '');
4da9a2ca 30 foreach my $what (qw(skip todo context options)) {
c4a350e6 31 s/^#\s*\U$what\E\s*(.*)\n//m and $meta{$what} = $1;
b871937f
NC
32 # If the SKIP reason starts ? then it's taken as a code snippet to
33 # evaluate. This provides the flexibility to have conditional SKIPs
c4a350e6
NC
34 if ($meta{$what} && $meta{$what} =~ s/^\?//) {
35 my $temp = eval $meta{$what};
b871937f 36 if ($@) {
c4a350e6 37 die "# In \U$what\E code reason:\n# $meta{$what}\n$@";
b871937f 38 }
c4a350e6 39 $meta{$what} = $temp;
e9c69003 40 }
e9c69003
NC
41 }
42
4a4b8592 43 s/^\s*#\s*(.*)$//mg;
507a68aa
NC
44 my $desc = $1;
45 die "Missing name in test $_" unless defined $desc;
e9c69003 46
c4a350e6 47 if ($meta{skip}) {
f9d3bdea 48 SKIP: { skip($meta{skip}) };
e9c69003
NC
49 next;
50 }
51
ad46c0be
RH
52 my ($input, $expected);
53 if (/(.*)\n>>>>\n(.*)/s) {
54 ($input, $expected) = ($1, $2);
55 }
56 else {
57 ($input, $expected) = ($_, $_);
58 }
87a42246 59
4da9a2ca
FC
60 # parse options if necessary
61 my $deparse = $meta{options}
62 ? $deparse{$meta{options}} ||=
63 new B::Deparse split /,/, $meta{options}
64 : $deparse;
65
27cbbc57 66 my $code = "$meta{context};\n" . <<'EOC' . "sub {$input\n}";
a6087f24
NC
67# Tell B::Deparse about our ambient pragmas
68my ($hint_bits, $warning_bits, $hinthash);
69BEGIN {
70 ($hint_bits, $warning_bits, $hinthash) = ($^H, ${^WARNING_BITS}, \%^H);
71}
72$deparse->ambient_pragmas (
73 hint_bits => $hint_bits,
74 warning_bits => $warning_bits,
75 '%^H' => $hinthash,
76);
77EOC
27cbbc57 78 my $coderef = eval $code;
87a42246 79
c9fa6ae9 80 local $::TODO = $meta{todo};
ad46c0be 81 if ($@) {
27cbbc57
DM
82 is($@, "", "compilation of $desc")
83 or diag "=============================================\n"
84 . "CODE:\n--------\n$code\n--------\n"
85 . "=============================================\n";
ad46c0be
RH
86 }
87 else {
88 my $deparsed = $deparse->coderef2text( $coderef );
31c6271a
RD
89 my $regex = $expected;
90 $regex =~ s/(\S+)/\Q$1/g;
91 $regex =~ s/\s+/\\s+/g;
92 $regex = '^\{\s*' . $regex . '\s*\}$';
b871937f 93
bc304ab2
DM
94 like($deparsed, qr/$regex/, $desc)
95 or diag "=============================================\n"
96 . "CODE:\n--------\n$input\n--------\n"
97 . "EXPECTED:\n--------\n{\n$expected\n}\n--------\n"
98 . "GOT:\n--------\n$deparsed\n--------\n"
99 . "=============================================\n";
87a42246 100 }
87a42246
MS
101}
102
9187b6e4
FC
103# Reset the ambient pragmas
104{
105 my ($b, $w, $h);
106 BEGIN {
107 ($b, $w, $h) = ($^H, ${^WARNING_BITS}, \%^H);
108 }
109 $deparse->ambient_pragmas (
110 hint_bits => $b,
111 warning_bits => $w,
112 '%^H' => $h,
113 );
114}
115
87a42246 116use constant 'c', 'stuff';
507a68aa
NC
117is((eval "sub ".$deparse->coderef2text(\&c))->(), 'stuff',
118 'the subroutine generated by use constant deparses');
87a42246 119
09d856fb 120my $a = 0;
507a68aa
NC
121is($deparse->coderef2text(sub{(-1) ** $a }), "{\n (-1) ** \$a;\n}",
122 'anon sub capturing an external lexical');
87a42246 123
d989cdac
SM
124use constant cr => ['hello'];
125my $string = "sub " . $deparse->coderef2text(\&cr);
0707d6cc 126my $val = (eval $string)->() or diag $string;
507a68aa
NC
127is(ref($val), 'ARRAY', 'constant array references deparse');
128is($val->[0], 'hello', 'and return the correct value');
87a42246 129
87a42246 130my $path = join " ", map { qq["-I$_"] } @INC;
87a42246 131
7cde0a5f 132$a = `$^X $path "-MO=Deparse" -anlwi.bak -e 1 2>&1`;
e69a2255 133$a =~ s/-e syntax OK\n//g;
d2bc402e 134$a =~ s/.*possible typo.*\n//; # Remove warning line
82f96200 135$a =~ s/.*-i used with no filenames.*\n//; # Remove warning line
c96cf1c0 136$b = quotemeta <<'EOF';
d2bc402e
RGS
137BEGIN { $^I = ".bak"; }
138BEGIN { $^W = 1; }
139BEGIN { $/ = "\n"; $\ = "\n"; }
18371617 140LINE: while (defined($_ = readline ARGV)) {
87a42246 141 chomp $_;
f86ea535 142 our(@F) = split(' ', $_, 0);
87a42246
MS
143 '???';
144}
87a42246 145EOF
c96cf1c0
FC
146$b =~ s/our\\\(\\\@F\\\)/our[( ]\@F\\)?/; # accept both our @F and our(@F)
147like($a, qr/$b/,
507a68aa 148 'command line flags deparse as BEGIN blocks setting control variables');
87a42246 149
5b4ee549
NC
150$a = `$^X $path "-MO=Deparse" -e "use constant PI => 4" 2>&1`;
151$a =~ s/-e syntax OK\n//g;
6436970c 152is($a, "use constant ('PI', 4);\n",
5b4ee549
NC
153 "Proxy Constant Subroutines must not show up as (incorrect) prototypes");
154
a9cafc78
FC
155$a = `$^X $path "-MO=Deparse" -e "sub foo(){1}" 2>&1`;
156$a =~ s/-e syntax OK\n//g;
157is($a, "sub foo () {\n 1;\n}\n",
158 "Main prog consisting of just a constant (via empty proto)");
159
160$a = readpipe qq|$^X $path "-MO=Deparse"|
161 .qq| -e "package F; sub f(){0} sub s{}"|
162 .qq| -e "#line 123 four-five-six"|
163 .qq| -e "package G; sub g(){0} sub s{}" 2>&1|;
164$a =~ s/-e syntax OK\n//g;
6fbf0c31 165like($a, qr/sub F::f \(\) \{\s*0;?\s*}/,
a9cafc78
FC
166 "Constant is dumped in package in which other subs are dumped");
167unlike($a, qr/sub g/,
168 "Constant is not dumped in package in which other subs are not dumped");
169
579a54dc 170#Re: perlbug #35857, patch #24505
b3980c39
YO
171#handle warnings::register-ed packages properly.
172package B::Deparse::Wrapper;
173use strict;
174use warnings;
175use warnings::register;
176sub getcode {
579a54dc 177 my $deparser = B::Deparse->new();
b3980c39
YO
178 return $deparser->coderef2text(shift);
179}
180
2990415a
FR
181package Moo;
182use overload '0+' => sub { 42 };
183
b3980c39
YO
184package main;
185use strict;
186use warnings;
71c4dbc3 187use constant GLIPP => 'glipp';
2990415a
FR
188use constant PI => 4;
189use constant OVERLOADED_NUMIFICATION => bless({}, 'Moo');
3779476a 190use Fcntl qw/O_TRUNC O_APPEND O_EXCL/;
aaf9c2b2 191BEGIN { delete $::Fcntl::{O_APPEND}; }
2990415a 192use POSIX qw/O_CREAT/;
b3980c39 193sub test {
579a54dc
RGS
194 my $val = shift;
195 my $res = B::Deparse::Wrapper::getcode($val);
507a68aa
NC
196 like($res, qr/use warnings/,
197 '[perl #35857] [PATCH] B::Deparse doesnt handle warnings register properly');
b3980c39
YO
198}
199my ($q,$p);
200my $x=sub { ++$q,++$p };
201test($x);
202eval <<EOFCODE and test($x);
203 package bar;
204 use strict;
205 use warnings;
206 use warnings::register;
207 package main;
208 1
209EOFCODE
210
d1dc589d
FC
211# Exotic sub declarations
212$a = `$^X $path "-MO=Deparse" -e "sub ::::{}sub ::::::{}" 2>&1`;
213$a =~ s/-e syntax OK\n//g;
214is($a, <<'EOCODG', "sub :::: and sub ::::::");
215sub :::: {
216
217}
218sub :::::: {
219
220}
221EOCODG
222
f2734596 223# [perl #117311]
224$a = `$^X $path "-MO=Deparse,-l" -e "map{ eval(0) }()" 2>&1`;
225$a =~ s/-e syntax OK\n//g;
226is($a, <<'EOCODH', "[perl #117311] [PATCH] -l option ('#line ...') does not emit ^Ls in the output");
227#line 1 "-e"
228map {
229#line 1 "-e"
230eval 0;} ();
231EOCODH
232
640d5d41
FC
233# [perl #33752]
234{
235 my $code = <<"EOCODE";
236{
237 our \$\x{1e1f}\x{14d}\x{14d};
238}
239EOCODE
240 my $deparsed
241 = $deparse->coderef2text(eval "sub { our \$\x{1e1f}\x{14d}\x{14d} }" );
242 s/$ \n//x for $deparsed, $code;
243 is $deparsed, $code, 'our $funny_Unicode_chars';
244}
245
bdabb2d5
FC
246# [perl #62500]
247$a =
248 `$^X $path "-MO=Deparse" -e "BEGIN{*CORE::GLOBAL::require=sub{1}}" 2>&1`;
249$a =~ s/-e syntax OK\n//g;
250is($a, <<'EOCODF', "CORE::GLOBAL::require override causing panick");
251sub BEGIN {
252 *CORE::GLOBAL::require = sub {
253 1;
254 }
255 ;
256}
257EOCODF
258
894e98ac
FC
259# [perl #91384]
260$a =
261 `$^X $path "-MO=Deparse" -e "BEGIN{*Acme::Acme:: = *Acme::}" 2>&1`;
262like($a, qr/-e syntax OK/,
263 "Deparse does not hang when traversing stash circularities");
264
bb8996b8 265# [perl #93990]
08412a26 266@] = ();
73f4c4fe 267is($deparse->coderef2text(sub{ print "foo@{]}" }),
bb8996b8 268q<{
73f4c4fe 269 print "foo@{]}";
08412a26 270}>, 'curly around to interpolate "@{]}"');
73f4c4fe 271is($deparse->coderef2text(sub{ print "foo@{-}" }),
bb8996b8 272q<{
73f4c4fe 273 print "foo@-";
bb8996b8
HY
274}>, 'no need to curly around to interpolate "@-"');
275
1c74777c
FC
276# Strict hints in %^H are mercilessly suppressed
277$a =
278 `$^X $path "-MO=Deparse" -e "use strict; print;" 2>&1`;
279unlike($a, qr/BEGIN/,
280 "Deparse does not emit strict hh hints");
281
3036b99c
FC
282# ambient_pragmas should not mess with strict settings.
283SKIP: {
284 skip "requires 5.11", 1 unless $] >= 5.011;
285 eval q`
3036b99c 286 BEGIN {
d1718a7c 287 # Clear out all hints
3036b99c 288 %^H = ();
d1718a7c 289 $^H = 0;
3036b99c
FC
290 new B::Deparse -> ambient_pragmas(strict => 'all');
291 }
292 use 5.011; # should enable strict
293 ok !eval '$do_noT_create_a_variable_with_this_name = 1',
294 'ambient_pragmas do not mess with compiling scope';
295 `;
296}
297
93a8ff62
FC
298# multiple statements on format lines
299$a = `$^X $path "-MO=Deparse" -e "format =" -e "\@" -e "x();z()" -e. 2>&1`;
300$a =~ s/-e syntax OK\n//g;
93a8ff62
FC
301is($a, <<'EOCODH', 'multiple statements on format lines');
302format STDOUT =
303@
304x(); z()
305.
306EOCODH
307
ddb55548
FC
308is runperl(stderr => 1, switches => [ '-MO=-qq,Deparse', $path, '-T' ],
309 prog => "format =\n\@\n\$;\n.\n"),
310 <<'EOCODM', '$; on format line';
311format STDOUT =
312@
313$;
314.
315EOCODM
316
e56a605e
FC
317is runperl(stderr => 1, switches => [ '-MO=-qq,Deparse,-l', $path ],
318 prog => "format =\n\@\n\$foo\n.\n"),
319 <<'EOCODM', 'formats with -l';
320format STDOUT =
321@
322$foo
323.
324EOCODM
325
8b9fb2f9
FC
326is runperl(stderr => 1, switches => [ '-MO=-qq,Deparse', $path ],
327 prog => "{ my \$x; format =\n\@\n\$x\n.\n}"),
328 <<'EOCODN', 'formats nested inside blocks';
329{
330 my $x;
331 format STDOUT =
332@
333$x
334.
335}
336EOCODN
337
7741ceed
FC
338# CORE::format
339$a = readpipe qq`$^X $path "-MO=Deparse" -e "use feature q|:all|;`
340 .qq` my sub format; CORE::format =" -e. 2>&1`;
341like($a, qr/CORE::format/, 'CORE::format when lex format sub is in scope');
342
fea7fb25
DM
343# literal big chars under 'use utf8'
344is($deparse->coderef2text(sub{ use utf8; /€/; }),
345'{
346 /\x{20ac}/;
347}',
348"qr/euro/");
349
e54915d6
FC
350# STDERR when deparsing sub calls
351# For a short while the output included 'While deparsing'
352$a = `$^X $path "-MO=Deparse" -e "foo()" 2>&1`;
353$a =~ s/-e syntax OK\n//g;
354is($a, <<'EOCODI', 'no extra output when deparsing foo()');
355foo();
356EOCODI
357
84ed3992
FC
358# Sub calls compiled before importation
359like runperl(stderr => 1, switches => [ '-MO=-qq,Deparse', $path ],
360 prog => 'BEGIN {
361 require Test::More;
362 Test::More::->import;
363 is(*foo, *foo)
364 }'),
365 qr/&is\(/,
366 'sub calls compiled before importation of prototype subs';
367
a93cf6e2
FC
368# [perl #121050] Prototypes with whitespace
369is runperl(stderr => 1, switches => [ '-MO=-qq,Deparse', $path ],
370 prog => <<'EOCODO'),
371sub _121050(\$ \$) { }
372_121050($a,$b);
373sub _121050empty( ) {}
374() = _121050empty() + 1;
375EOCODO
376 <<'EOCODP', '[perl #121050] prototypes with whitespace';
377sub _121050 (\$ \$) {
378
379}
380_121050 $a, $b;
381sub _121050empty ( ) {
382
383}
384() = _121050empty + 1;
385EOCODP
386
7741ceed
FC
387# CORE::no
388$a = readpipe qq`$^X $path "-MO=Deparse" -Xe `
389 .qq`"use feature q|:all|; my sub no; CORE::no less" 2>&1`;
e276dec7 390like($a, qr/my sub no;\n.*CORE::no less;/s,
7741ceed
FC
391 'CORE::no after my sub no');
392
393# CORE::use
394$a = readpipe qq`$^X $path "-MO=Deparse" -Xe `
395 .qq`"use feature q|:all|; my sub use; CORE::use less" 2>&1`;
e276dec7 396like($a, qr/my sub use;\n.*CORE::use less;/s,
7741ceed
FC
397 'CORE::use after my sub use');
398
399# CORE::__DATA__
400$a = readpipe qq`$^X $path "-MO=Deparse" -Xe `
401 .qq`"use feature q|:all|; my sub __DATA__; `
402 .qq`CORE::__DATA__" 2>&1`;
e276dec7 403like($a, qr/my sub __DATA__;\n.*CORE::__DATA__/s,
7741ceed
FC
404 'CORE::__DATA__ after my sub __DATA__');
405
fe8d6c18
FC
406# sub declarations
407$a = readpipe qq`$^X $path "-MO=Deparse" -e "sub foo{}" 2>&1`;
dd3f0a7a 408like($a, qr/sub foo\s*\{\s+\}/, 'sub declarations');
67359f08
FC
409like runperl(stderr => 1, switches => [ '-MO=-qq,Deparse', $path ],
410 prog => 'sub f($); sub f($){}'),
411 qr/sub f\s*\(\$\)\s*\{\s*\}/,
412 'predeclared prototyped subs';
de001ba0
FC
413like runperl(stderr => 1, switches => [ '-MO=-qq,Deparse', $path ],
414 prog => 'use Scalar::Util q-weaken-;
415 sub f($);
416 BEGIN { weaken($_=\$::{f}) }'),
417 qr/sub f\s*\(\$\)\s*;/,
418 'prototyped stub with weak reference to the stash entry';
03b8f76d
FC
419like runperl(stderr => 1, switches => [ '-MO=-qq,Deparse', $path ],
420 prog => 'sub f () { 42 }'),
421 qr/sub f\s*\(\)\s*\{\s*42;\s*\}/,
422 'constant perl sub declaration';
fe8d6c18 423
8635e3c2
FC
424# BEGIN blocks
425SKIP : {
426 skip "BEGIN output is wrong on old perls", 1 if $] < 5.021006;
427 my $prog = '
428 BEGIN { pop }
429 {
430 BEGIN { pop }
431 {
432 no overloading;
433 {
434 BEGIN { pop }
435 die
436 }
437 }
438 }';
439 $prog =~ s/\n//g;
440 $a = readpipe qq`$^X $path "-MO=Deparse" -e "$prog" 2>&1`;
441 $a =~ s/-e syntax OK\n//g;
442 is($a, <<'EOCODJ', 'BEGIN blocks');
443sub BEGIN {
444 pop @ARGV;
445}
446{
447 sub BEGIN {
448 pop @ARGV;
449 }
450 {
451 no overloading;
452 {
453 sub BEGIN {
454 pop @ARGV;
455 }
456 die;
457 }
458 }
459}
460EOCODJ
461}
34b54951
FC
462is runperl(stderr => 1, switches => [ '-MO=-qq,Deparse', $path ], prog => '
463 {
464 {
465 die;
466 BEGIN { pop }
467 }
468 BEGIN { pop }
469 }
470 BEGIN { pop }
471 '), <<'EOCODL', 'BEGIN blocks at the end of their enclosing blocks';
472{
473 {
474 die;
475 sub BEGIN {
476 pop @ARGV;
477 }
34b54951
FC
478 }
479 sub BEGIN {
480 pop @ARGV;
481 }
34b54951
FC
482}
483sub BEGIN {
484 pop @ARGV;
485}
34b54951 486EOCODL
93a8ff62 487
c310a5ab
FC
488# BEGIN blocks should not be called __ANON__
489like runperl(stderr => 1, switches => [ '-MO=-qq,Deparse', $path ],
490 prog => 'sub BEGIN { } CHECK { delete $::{BEGIN} }'),
491 qr/sub BEGIN/, 'anonymised BEGIN';
492
1f9498d0 493# [perl #115066]
1f9498d0
FC
494my $prog = 'use constant FOO => do { 1 }; no overloading; die';
495$a = readpipe qq`$^X $path "-MO=-qq,Deparse" -e "$prog" 2>&1`;
496is($a, <<'EOCODK', '[perl #115066] use statements accidentally nested');
497use constant ('FOO', do {
d88d1fe0 498 1
1f9498d0
FC
499});
500no overloading;
501die;
502EOCODK
503
f1013307
FC
504# BEGIN blocks inside predeclared subs
505like runperl(stderr => 1, switches => [ '-MO=-qq,Deparse', $path ],
506 prog => '
507 sub run_tests;
508 run_tests();
509 sub run_tests { BEGIN { } die }'),
510 qr/sub run_tests \{\s*sub BEGIN/,
511 'BEGIN block inside predeclared sub';
512
d49c3562 513like runperl(stderr => 1, switches => [ '-MO=-qq,Deparse', $path ],
d98ae4a6
FC
514 prog => 'package foo; use overload qr=>sub{}'),
515 qr/package foo;\s*use overload/,
516 'package, then use';
517
518like runperl(stderr => 1, switches => [ '-MO=-qq,Deparse', $path ],
d49c3562
FC
519 prog => 'use feature lexical_subs=>; my sub f;sub main::f{}'),
520 qr/^sub main::f \{/m,
521 'sub decl when lex sub is in scope';
522
f518ad75
FC
523like runperl(stderr => 1, switches => [ '-MO=-qq,Deparse', $path ],
524 prog => 'sub foo{foo()}'),
525 qr/^sub foo \{\s+foo\(\)/m,
526 'recursive sub';
527
2c5ddcd3
FC
528like runperl(stderr => 1, switches => [ '-MO=-qq,Deparse', $path ],
529 prog => 'use feature lexical_subs=>state=>;
530 state sub sb5; sub { sub sb5 { } }'),
401e2e00 531 qr/sub \{\s*\(\);\s*sub sb5 \{/m,
2c5ddcd3
FC
532 'state sub in anon sub but declared outside';
533
5e965771
FC
534is runperl(stderr => 1, switches => [ '-MO=-qq,Deparse', $path ],
535 prog => 'BEGIN { $::{f}=\!0 }'),
536 "sub BEGIN {\n \$main::{'f'} = \\1;\n}\n",
537 '&PL_sv_yes constant (used to croak)';
538
d02d1323
FC
539is runperl(stderr => 1, switches => [ '-MO=-qq,Deparse', $path, '-T' ],
540 prog => '$x =~ (1?/$a/:0)'),
541 '$x =~ ($_ =~ /$a/);'."\n",
542 '$foo =~ <branch-folded match> under taint mode';
543
62ae7cfb
FC
544unlike runperl(stderr => 1, switches => [ '-MO=-qq,Deparse', $path, '-w' ],
545 prog => 'BEGIN { undef &foo }'),
546 qr'Use of uninitialized value',
547 'no warnings for undefined sub';
548
7d9a919c
DM
549is runperl(stderr => 1, switches => [ '-MO=-qq,Deparse', $path ],
550 prog => 'sub f { 1; } BEGIN { *g = \&f; }'),
551 "sub f {\n 1;\n}\nsub BEGIN {\n *g = \\&f;\n}\n",
552 "sub glob alias shouldn't impede emitting original sub";
553
554is runperl(stderr => 1, switches => [ '-MO=-qq,Deparse', $path ],
555 prog => 'package Foo; sub f { 1; } BEGIN { *g = \&f; }'),
556 "package Foo;\nsub f {\n 1;\n}\nsub BEGIN {\n *g = \\&f;\n}\n",
557 "sub glob alias outside main shouldn't impede emitting original sub";
558
559is runperl(stderr => 1, switches => [ '-MO=-qq,Deparse', $path ],
560 prog => 'package Foo; sub f { 1; } BEGIN { *Bar::f = \&f; }'),
561 "package Foo;\nsub f {\n 1;\n}\nsub BEGIN {\n *Bar::f = \\&f;\n}\n",
562 "sub glob alias in separate package shouldn't impede emitting original sub";
563
564
d8cf01c3 565done_testing($tests);
507a68aa 566
ad46c0be 567__DATA__
b8346d05
KW
568# TODO [perl #120950] This succeeds when run a 2nd time
569# y/uni/code/
570tr/\x{345}/\x{370}/;
571####
572# y/uni/code/ [perl #120950] This 2nd instance succeeds
573tr/\x{345}/\x{370}/;
574####
507a68aa 575# A constant
ad46c0be
RH
5761;
577####
507a68aa 578# Constants in a block
f5b5c2a3 579# CONTEXT no warnings;
ad46c0be 580{
ad46c0be
RH
581 '???';
582 2;
583}
584####
40ced2f4
FC
585# List of constants in void context
586# CONTEXT no warnings;
587(1,2,3);
5880;
589>>>>
590'???', '???', '???';
5910;
592####
507a68aa 593# Lexical and simple arithmetic
ad46c0be
RH
594my $test;
595++$test and $test /= 2;
596>>>>
597my $test;
598$test /= 2 if ++$test;
599####
507a68aa 600# list x
ad46c0be
RH
601-((1, 2) x 2);
602####
6a861075
FC
603# Assignment to list x
604((undef) x 3) = undef;
605####
507a68aa 606# lvalue sub
ad46c0be
RH
607{
608 my $test = sub : lvalue {
609 my $x;
610 }
611 ;
612}
613####
507a68aa 614# method
ad46c0be
RH
615{
616 my $test = sub : method {
617 my $x;
618 }
619 ;
620}
621####
6b6b21da
FC
622# anonsub attrs at statement start
623my $x = do { +sub : lvalue { my $y; } };
624my $z = do { foo: +sub : method { my $a; } };
625####
507a68aa 626# block with continue
87a42246 627{
ad46c0be 628 234;
f99a63a2 629}
ad46c0be
RH
630continue {
631 123;
87a42246 632}
ce4e655d 633####
507a68aa 634# lexical and package scalars
ce4e655d
RH
635my $x;
636print $main::x;
637####
507a68aa 638# lexical and package arrays
ce4e655d
RH
639my @x;
640print $main::x[1];
0ed5b3c8 641print \my @a;
14a55f98 642####
507a68aa 643# lexical and package hashes
14a55f98
RH
644my %x;
645$x{warn()};
ad8caead 646####
66786896
FC
647# our (LIST)
648our($foo, $bar, $baz);
649####
56cd2ef8
FC
650# CONTEXT { package Dog } use feature "state";
651# variables with declared classes
652my Dog $spot;
653our Dog $spotty;
654state Dog $spotted;
655my Dog @spot;
656our Dog @spotty;
657state Dog @spotted;
658my Dog %spot;
659our Dog %spotty;
660state Dog %spotted;
661my Dog ($foo, @bar, %baz);
662our Dog ($phoo, @barr, %bazz);
663state Dog ($fough, @barre, %bazze);
664####
f3515641
FC
665# local our
666local our $rhubarb;
5f4d8496 667local our($rhu, $barb);
f3515641 668####
507a68aa 669# <>
ad8caead 670my $foo;
18371617
LM
671$_ .= <> . <ARGV> . <$foo>;
672<$foo>;
673<${foo}>;
674<$ foo>;
675>>>>
676my $foo;
677$_ .= readline(ARGV) . readline(ARGV) . readline($foo);
678readline $foo;
679glob $foo;
680glob $foo;
cef22867 681####
9cef6114
FC
682# readline
683readline 'FH';
684readline *$_;
18371617
LM
685readline *{$_};
686readline ${"a"};
687>>>>
688readline 'FH';
689readline *$_;
9cef6114 690readline *{$_;};
18371617 691readline ${'a';};
9cef6114 692####
2fcbb74a
FC
693# <<>>
694$_ = <<>>;
695####
507a68aa 696# \x{}
11454c59 697my $foo = "Ab\x{100}\200\x{200}\237Cd\000Ef\x{1000}\cA\x{2000}\cZ";
2a43599b 698my $bar = "\x{100}";
4ae52e81 699####
2d5b99ed
FC
700# Latin-1 chars
701# TODO ? ord("A") != 65 && "EBCDIC"
702my $baz = "B\366\x{100}";
703my $bba = qr/B\366\x{100}/;
704####
507a68aa 705# s///e
4ae52e81 706s/x/'y';/e;
ef90d20a
FC
707s/x/$a;/e;
708s/x/complex_expression();/e;
241416b8 709####
507a68aa 710# block
241416b8
DM
711{ my $x; }
712####
507a68aa 713# while 1
241416b8
DM
714while (1) { my $k; }
715####
507a68aa 716# trailing for
241416b8
DM
717my ($x,@a);
718$x=1 for @a;
719>>>>
720my($x, @a);
0bb5f065 721$x = 1 foreach (@a);
241416b8 722####
507a68aa 723# 2 arguments in a 3 argument for
241416b8
DM
724for (my $i = 0; $i < 2;) {
725 my $z = 1;
726}
727####
507a68aa 728# 3 argument for
241416b8
DM
729for (my $i = 0; $i < 2; ++$i) {
730 my $z = 1;
731}
732####
507a68aa 733# 3 argument for again
241416b8
DM
734for (my $i = 0; $i < 2; ++$i) {
735 my $z = 1;
736}
737####
22584011
FC
738# 3-argument for with inverted condition
739for (my $i; not $i;) {
740 die;
741}
742for (my $i; not $i; ++$i) {
743 die;
744}
88a758b5
FC
745for (my $a; not +($1 || 2) ** 2;) {
746 die;
747}
22584011
FC
748Something_to_put_the_loop_in_void_context();
749####
507a68aa 750# while/continue
241416b8
DM
751my $i;
752while ($i) { my $z = 1; } continue { $i = 99; }
753####
507a68aa 754# foreach with my
09d856fb 755foreach my $i (1, 2) {
241416b8
DM
756 my $z = 1;
757}
758####
4da9a2ca
FC
759# OPTIONS -p
760# foreach with my under -p
761foreach my $i (1) {
762 die;
763}
764####
507a68aa 765# foreach
241416b8
DM
766my $i;
767foreach $i (1, 2) {
768 my $z = 1;
769}
770####
507a68aa 771# foreach, 2 mys
241416b8
DM
772my $i;
773foreach my $i (1, 2) {
774 my $z = 1;
775}
776####
507a68aa 777# foreach with our
241416b8
DM
778foreach our $i (1, 2) {
779 my $z = 1;
780}
781####
507a68aa 782# foreach with my and our
241416b8
DM
783my $i;
784foreach our $i (1, 2) {
785 my $z = 1;
786}
3ac6e0f9 787####
bcff4148
FC
788# foreach with state
789# CONTEXT use feature "state";
790foreach state $i (1, 2) {
791 state $z = 1;
792}
793####
bba4f5ff
FC
794# foreach with sub call
795foreach $_ (hcaerof()) {
796 ();
797}
798####
507a68aa 799# reverse sort
3ac6e0f9
RGS
800my @x;
801print reverse sort(@x);
802####
507a68aa 803# sort with cmp
3ac6e0f9
RGS
804my @x;
805print((sort {$b cmp $a} @x));
806####
507a68aa 807# reverse sort with block
3ac6e0f9
RGS
808my @x;
809print((reverse sort {$b <=> $a} @x));
36d57d93 810####
507a68aa 811# foreach reverse
36d57d93
RGS
812our @a;
813print $_ foreach (reverse @a);
aae53c41 814####
507a68aa 815# foreach reverse (not inplace)
aae53c41
RGS
816our @a;
817print $_ foreach (reverse 1, 2..5);
f86ea535 818####
507a68aa 819# bug #38684
f86ea535
SM
820our @ary;
821@ary = split(' ', 'foo', 0);
31c6271a 822####
5012eebe
DM
823my @ary;
824@ary = split(' ', 'foo', 0);
825####
de183bbb
FC
826# Split to our array
827our @array = split(//, 'foo', 0);
828####
ef7999f1
FC
829# Split to my array
830my @array = split(//, 'foo', 0);
831####
5012eebe
DM
832our @array;
833my $c;
834@array = split(/x(?{ $c++; })y/, 'foo', 0);
835####
836my($x, $y, $p);
837our $c;
838($x, $y) = split(/$p(?{ $c++; })y/, 'foo', 2);
839####
840our @ary;
841my $pat;
842@ary = split(/$pat/, 'foo', 0);
843####
844my @ary;
845our $pat;
846@ary = split(/$pat/, 'foo', 0);
847####
848our @array;
849my $pat;
850local @array = split(/$pat/, 'foo', 0);
851####
852our $pat;
853my @array = split(/$pat/, 'foo', 0);
854####
507a68aa 855# bug #40055
31c6271a
RD
856do { () };
857####
507a68aa 858# bug #40055
31c6271a 859do { my $x = 1; $x };
d9002312 860####
507a68aa 861# <20061012113037.GJ25805@c4.convolution.nl>
d9002312
SM
862my $f = sub {
863 +{[]};
864} ;
8b2d6640 865####
507a68aa 866# bug #43010
8b2d6640
FC
867'!@$%'->();
868####
507a68aa 869# bug #43010
8b2d6640
FC
870::();
871####
507a68aa 872# bug #43010
8b2d6640
FC
873'::::'->();
874####
507a68aa 875# bug #43010
8b2d6640 876&::::;
09d856fb 877####
1b38d782
FC
878# [perl #77172]
879package rt77172;
880sub foo {} foo & & & foo;
881>>>>
882package rt77172;
883foo(&{&} & foo());
884####
507a68aa 885# variables as method names
09d856fb
CK
886my $bar;
887'Foo'->$bar('orz');
35a99a08 888'Foo'->$bar('orz') = 'a stranger stranger than before';
09d856fb 889####
507a68aa 890# constants as method names
09d856fb
CK
891'Foo'->bar('orz');
892####
507a68aa 893# constants as method names without ()
09d856fb 894'Foo'->bar;
0ced6c29 895####
28bfcb02 896# [perl #47359] "indirect" method call notation
1bf8bbb0
FC
897our @bar;
898foo{@bar}+1,->foo;
899(foo{@bar}+1),foo();
900foo{@bar}1 xor foo();
901>>>>
902our @bar;
903(foo { @bar } 1)->foo;
904(foo { @bar } 1), foo();
905foo { @bar } 1 xor foo();
906####
9d52f6f3
FC
907# indirops with blocks
908# CONTEXT use 5.01;
909print {*STDOUT;} 'foo';
910printf {*STDOUT;} 'foo';
911say {*STDOUT;} 'foo';
912system {'foo';} '-foo';
913exec {'foo';} '-foo';
914####
e9c69003 915# SKIP ?$] < 5.010 && "say not implemented on this Perl version"
205fef88 916# CONTEXT use feature ':5.10';
507a68aa 917# say
7ddd1a01
NC
918say 'foo';
919####
8f57bb34 920# SKIP ?$] < 5.010 && "say not implemented on this Perl version"
8f57bb34
NC
921# CONTEXT use 5.10.0;
922# say in the context of use 5.10.0
923say 'foo';
924####
925# SKIP ?$] < 5.010 && "say not implemented on this Perl version"
8f57bb34
NC
926# say with use 5.10.0
927use 5.10.0;
928say 'foo';
929>>>>
127ce1cd 930no feature ':all';
8f57bb34
NC
931use feature ':5.10';
932say 'foo';
933####
934# SKIP ?$] < 5.010 && "say not implemented on this Perl version"
935# say with use feature ':5.10';
936use feature ':5.10';
937say 'foo';
938>>>>
939use feature 'say', 'state', 'switch';
940say 'foo';
941####
942# SKIP ?$] < 5.010 && "say not implemented on this Perl version"
8f57bb34
NC
943# CONTEXT use feature ':5.10';
944# say with use 5.10.0 in the context of use feature
945use 5.10.0;
946say 'foo';
947>>>>
127ce1cd 948no feature ':all';
8f57bb34
NC
949use feature ':5.10';
950say 'foo';
951####
952# SKIP ?$] < 5.010 && "say not implemented on this Perl version"
953# CONTEXT use 5.10.0;
954# say with use feature ':5.10' in the context of use 5.10.0
955use feature ':5.10';
956say 'foo';
957>>>>
958say 'foo';
959####
960# SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
961# CONTEXT use feature ':5.15';
962# __SUB__
963__SUB__;
964####
965# SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
8f57bb34
NC
966# CONTEXT use 5.15.0;
967# __SUB__ in the context of use 5.15.0
968__SUB__;
969####
970# SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
8f57bb34
NC
971# __SUB__ with use 5.15.0
972use 5.15.0;
973__SUB__;
974>>>>
127ce1cd 975no feature ':all';
8f57bb34
NC
976use feature ':5.16';
977__SUB__;
978####
979# SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
980# __SUB__ with use feature ':5.15';
981use feature ':5.15';
982__SUB__;
983>>>>
984use feature 'current_sub', 'evalbytes', 'fc', 'say', 'state', 'switch', 'unicode_strings', 'unicode_eval';
985__SUB__;
986####
987# SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
8f57bb34
NC
988# CONTEXT use feature ':5.15';
989# __SUB__ with use 5.15.0 in the context of use feature
990use 5.15.0;
991__SUB__;
992>>>>
127ce1cd 993no feature ':all';
8f57bb34
NC
994use feature ':5.16';
995__SUB__;
996####
997# SKIP ?$] < 5.015 && "__SUB__ not implemented on this Perl version"
998# CONTEXT use 5.15.0;
999# __SUB__ with use feature ':5.15' in the context of use 5.15.0
1000use feature ':5.15';
1001__SUB__;
1002>>>>
1003__SUB__;
1004####
e9c69003 1005# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
205fef88 1006# CONTEXT use feature ':5.10';
507a68aa 1007# state vars
0ced6c29
RGS
1008state $x = 42;
1009####
e9c69003 1010# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
205fef88 1011# CONTEXT use feature ':5.10';
507a68aa 1012# state var assignment
7ddd1a01
NC
1013{
1014 my $y = (state $x = 42);
1015}
1016####
e9c69003 1017# SKIP ?$] < 5.010 && "state vars not implemented on this Perl version"
205fef88 1018# CONTEXT use feature ':5.10';
c4a6f826 1019# state vars in anonymous subroutines
7ddd1a01
NC
1020$a = sub {
1021 state $x;
1022 return $x++;
1023}
1024;
644741fd
NC
1025####
1026# SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
507a68aa 1027# each @array;
644741fd
NC
1028each @ARGV;
1029each @$a;
1030####
1031# SKIP ?$] < 5.011 && 'each @array not implemented on this Perl version'
507a68aa 1032# keys @array; values @array
644741fd
NC
1033keys @$a if keys @ARGV;
1034values @ARGV if values @$a;
35925e80 1035####
507a68aa 1036# Anonymous arrays and hashes, and references to them
35925e80
RGS
1037my $a = {};
1038my $b = \{};
1039my $c = [];
1040my $d = \[];
9210de83 1041####
7896dde7 1042# SKIP ?$] < 5.010 && "smartmatch and given/when not implemented on this Perl version"
0f539b13 1043# CONTEXT use feature ':5.10'; no warnings 'experimental::smartmatch';
7896dde7 1044# implicit smartmatch in given/when
9210de83 1045given ('foo') {
7896dde7
Z
1046 when ('bar') { continue; }
1047 when ($_ ~~ 'quux') { continue; }
1048 default { 0; }
9210de83 1049}
7ecdd211 1050####
507a68aa 1051# conditions in elsifs (regression in change #33710 which fixed bug #37302)
7ecdd211
PJ
1052if ($a) { x(); }
1053elsif ($b) { x(); }
1054elsif ($a and $b) { x(); }
1055elsif ($a or $b) { x(); }
1056else { x(); }
03b22f1b 1057####
507a68aa 1058# interpolation in regexps
03b22f1b
RGS
1059my($y, $t);
1060/x${y}z$t/;
227375e1 1061####
4a4b8592 1062# TODO new undocumented cpan-bug #33708
507a68aa 1063# cpan-bug #33708
227375e1
RU
1064%{$_ || {}}
1065####
4a4b8592 1066# TODO hash constants not yet fixed
507a68aa 1067# cpan-bug #33708
227375e1
RU
1068use constant H => { "#" => 1 }; H->{"#"}
1069####
4a4b8592 1070# TODO optimized away 0 not yet fixed
507a68aa 1071# cpan-bug #33708
227375e1 1072foreach my $i (@_) { 0 }
edbe35ea 1073####
507a68aa 1074# tests with not, not optimized
07f3cdf5 1075my $c;
edbe35ea
VP
1076x() unless $a;
1077x() if not $a and $b;
1078x() if $a and not $b;
1079x() unless not $a and $b;
1080x() unless $a and not $b;
1081x() if not $a or $b;
1082x() if $a or not $b;
1083x() unless not $a or $b;
1084x() unless $a or not $b;
07f3cdf5
VP
1085x() if $a and not $b and $c;
1086x() if not $a and $b and not $c;
1087x() unless $a and not $b and $c;
1088x() unless not $a and $b and not $c;
1089x() if $a or not $b or $c;
1090x() if not $a or $b or not $c;
1091x() unless $a or not $b or $c;
1092x() unless not $a or $b or not $c;
edbe35ea 1093####
507a68aa 1094# tests with not, optimized
07f3cdf5 1095my $c;
edbe35ea
VP
1096x() if not $a;
1097x() unless not $a;
1098x() if not $a and not $b;
1099x() unless not $a and not $b;
1100x() if not $a or not $b;
1101x() unless not $a or not $b;
07f3cdf5
VP
1102x() if not $a and not $b and $c;
1103x() unless not $a and not $b and $c;
1104x() if not $a or not $b or $c;
1105x() unless not $a or not $b or $c;
1106x() if not $a and not $b and not $c;
1107x() unless not $a and not $b and not $c;
1108x() if not $a or not $b or not $c;
1109x() unless not $a or not $b or not $c;
1110x() unless not $a or not $b or not $c;
edbe35ea 1111>>>>
07f3cdf5 1112my $c;
edbe35ea
VP
1113x() unless $a;
1114x() if $a;
1115x() unless $a or $b;
1116x() if $a or $b;
1117x() unless $a and $b;
07f3cdf5
VP
1118x() if $a and $b;
1119x() if not $a || $b and $c;
1120x() unless not $a || $b and $c;
1121x() if not $a && $b or $c;
1122x() unless not $a && $b or $c;
1123x() unless $a or $b or $c;
1124x() if $a or $b or $c;
1125x() unless $a and $b and $c;
1126x() if $a and $b and $c;
1127x() unless not $a && $b && $c;
71c4dbc3 1128####
507a68aa 1129# tests that should be constant folded
71c4dbc3
VP
1130x() if 1;
1131x() if GLIPP;
1132x() if !GLIPP;
1133x() if GLIPP && GLIPP;
1134x() if !GLIPP || GLIPP;
1135x() if do { GLIPP };
1136x() if do { no warnings 'void'; 5; GLIPP };
1137x() if do { !GLIPP };
1138if (GLIPP) { x() } else { z() }
1139if (!GLIPP) { x() } else { z() }
1140if (GLIPP) { x() } elsif (GLIPP) { z() }
1141if (!GLIPP) { x() } elsif (GLIPP) { z() }
1142if (GLIPP) { x() } elsif (!GLIPP) { z() }
1143if (!GLIPP) { x() } elsif (!GLIPP) { z() }
1144if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (GLIPP) { t() }
1145if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
1146if (!GLIPP) { x() } elsif (!GLIPP) { z() } elsif (!GLIPP) { t() }
1147>>>>
1148x();
1149x();
1150'???';
1151x();
1152x();
1153x();
1154x();
1155do {
1156 '???'
1157};
1158do {
1159 x()
1160};
1161do {
1162 z()
1163};
1164do {
1165 x()
1166};
1167do {
1168 z()
1169};
1170do {
1171 x()
1172};
1173'???';
1174do {
1175 t()
1176};
1177'???';
1178!1;
1179####
719c50dc
RGS
1180# TODO constant deparsing has been backed out for 5.12
1181# XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
507a68aa 1182# tests that shouldn't be constant folded
ac0f1413
NC
1183# It might be fundamentally impossible to make this work on ithreads, in which
1184# case the TODO should become a SKIP
71c4dbc3
VP
1185x() if $a;
1186if ($a == 1) { x() } elsif ($b == 2) { z() }
1187if (do { foo(); GLIPP }) { x() }
1188if (do { $a++; GLIPP }) { x() }
1189>>>>
1190x() if $a;
1191if ($a == 1) { x(); } elsif ($b == 2) { z(); }
2990415a
FR
1192if (do { foo(); GLIPP }) { x(); }
1193if (do { ++$a; GLIPP }) { x(); }
1194####
0fa4a265 1195# TODO constant deparsing has been backed out for 5.12
507a68aa 1196# tests for deparsing constants
2990415a
FR
1197warn PI;
1198####
0fa4a265 1199# TODO constant deparsing has been backed out for 5.12
507a68aa 1200# tests for deparsing imported constants
3779476a 1201warn O_TRUNC;
2990415a 1202####
0fa4a265 1203# TODO constant deparsing has been backed out for 5.12
507a68aa 1204# tests for deparsing re-exported constants
2990415a
FR
1205warn O_CREAT;
1206####
0fa4a265 1207# TODO constant deparsing has been backed out for 5.12
507a68aa 1208# tests for deparsing imported constants that got deleted from the original namespace
aaf9c2b2 1209warn O_APPEND;
2990415a 1210####
0fa4a265
DM
1211# TODO constant deparsing has been backed out for 5.12
1212# XXXTODO ? $Config::Config{useithreads} && "doesn't work with threads"
507a68aa 1213# tests for deparsing constants which got turned into full typeglobs
ac0f1413
NC
1214# It might be fundamentally impossible to make this work on ithreads, in which
1215# case the TODO should become a SKIP
2990415a
FR
1216warn O_EXCL;
1217eval '@Fcntl::O_EXCL = qw/affe tiger/;';
1218warn O_EXCL;
1219####
0fa4a265 1220# TODO constant deparsing has been backed out for 5.12
507a68aa 1221# tests for deparsing of blessed constant with overloaded numification
2990415a 1222warn OVERLOADED_NUMIFICATION;
79289e05 1223####
507a68aa 1224# strict
79289e05 1225no strict;
415d4c68
FC
1226print $x;
1227use strict 'vars';
1228print $main::x;
1229use strict 'subs';
1230print $main::x;
1231use strict 'refs';
1232print $main::x;
1233no strict 'vars';
79289e05
NC
1234$x;
1235####
1236# TODO Subsets of warnings could be encoded textually, rather than as bitflips.
507a68aa 1237# subsets of warnings
79289e05
NC
1238no warnings 'deprecated';
1239my $x;
1240####
1241# TODO Better test for CPAN #33708 - the deparsed code has different behaviour
507a68aa 1242# CPAN #33708
79289e05
NC
1243use strict;
1244no warnings;
1245
1246foreach (0..3) {
1247 my $x = 2;
1248 {
1249 my $x if 0;
1250 print ++$x, "\n";
1251 }
1252}
d83f38d8 1253####
507a68aa 1254# no attribute list
d83f38d8
NC
1255my $pi = 4;
1256####
2dc78664
NC
1257# SKIP ?$] > 5.013006 && ":= is now a syntax error"
1258# := treated as an empty attribute list
d83f38d8
NC
1259no warnings;
1260my $pi := 4;
1261>>>>
1262no warnings;
1263my $pi = 4;
1264####
507a68aa 1265# : = empty attribute list
d83f38d8
NC
1266my $pi : = 4;
1267>>>>
1268my $pi = 4;
689e417f 1269####
507a68aa 1270# in place sort
689e417f
VP
1271our @a;
1272my @b;
1273@a = sort @a;
1274@b = sort @b;
1275();
1276####
507a68aa 1277# in place reverse
689e417f
VP
1278our @a;
1279my @b;
1280@a = reverse @a;
1281@b = reverse @b;
1282();
06fc6867 1283####
507a68aa 1284# #71870 Use of uninitialized value in bitwise and B::Deparse
06fc6867
VP
1285my($r, $s, @a);
1286@a = split(/foo/, $s, 0);
1287$r = qr/foo/;
1288@a = split(/$r/, $s, 0);
1289();
98a1a137 1290####
507a68aa 1291# package declaration before label
98a1a137
Z
1292{
1293 package Foo;
1294 label: print 123;
1295}
538f5756 1296####
507a68aa 1297# shift optimisation
538f5756
RZ
1298shift;
1299>>>>
1300shift();
1301####
507a68aa 1302# shift optimisation
538f5756
RZ
1303shift @_;
1304####
507a68aa 1305# shift optimisation
538f5756
RZ
1306pop;
1307>>>>
1308pop();
1309####
507a68aa 1310# shift optimisation
538f5756 1311pop @_;
a539498a 1312####
507a68aa 1313#[perl #20444]
a539498a
FC
1314"foo" =~ (1 ? /foo/ : /bar/);
1315"foo" =~ (1 ? y/foo// : /bar/);
5e5a1632 1316"foo" =~ (1 ? y/foo//r : /bar/);
a539498a
FC
1317"foo" =~ (1 ? s/foo// : /bar/);
1318>>>>
1319'foo' =~ ($_ =~ /foo/);
1320'foo' =~ ($_ =~ tr/fo//);
5e5a1632 1321'foo' =~ ($_ =~ tr/fo//r);
a539498a 1322'foo' =~ ($_ =~ s/foo//);
e0ab66ad 1323####
5e5a1632
FC
1324# The fix for [perl #20444] broke this.
1325'foo' =~ do { () };
1326####
4b58603b
FC
1327# [perl #81424] match against aelemfast_lex
1328my @s;
1329print /$s[1]/;
1330####
36727b53
FC
1331# /$#a/
1332print /$#main::a/;
1333####
bae5b54e
FC
1334# /@array/
1335our @a;
1336my @b;
1337print /@a/;
1338print /@b/;
1339print qr/@a/;
1340print qr/@b/;
1341####
3b91d897
FC
1342# =~ QR_CONSTANT
1343use constant QR_CONSTANT => qr/a/soupmix;
1344'' =~ QR_CONSTANT;
1345>>>>
1346'' =~ /a/impsux;
1347####
9e32885a
FC
1348# $lexical =~ //
1349my $x;
1350$x =~ //;
1351####
b9bc576f 1352# [perl #91318] /regexp/applaud
09622ee2
FC
1353print /a/a, s/b/c/a;
1354print /a/aa, s/b/c/aa;
1355print /a/p, s/b/c/p;
1356print /a/l, s/b/c/l;
1357print /a/u, s/b/c/u;
b9bc576f
FC
1358{
1359 use feature "unicode_strings";
09622ee2 1360 print /a/d, s/b/c/d;
b9bc576f
FC
1361}
1362{
1363 use re "/u";
09622ee2 1364 print /a/d, s/b/c/d;
b9bc576f 1365}
dff5ffe4
FC
1366{
1367 use 5.012;
1368 print /a/d, s/b/c/d;
1369}
b9bc576f 1370>>>>
09622ee2
FC
1371print /a/a, s/b/c/a;
1372print /a/aa, s/b/c/aa;
1373print /a/p, s/b/c/p;
1374print /a/l, s/b/c/l;
1375print /a/u, s/b/c/u;
b9bc576f 1376{
a8095af7 1377 use feature 'unicode_strings';
09622ee2 1378 print /a/d, s/b/c/d;
b9bc576f
FC
1379}
1380{
0bb01b05
FC
1381 BEGIN { $^H{'reflags'} = '0';
1382 $^H{'reflags_charset'} = '2'; }
09622ee2 1383 print /a/d, s/b/c/d;
b9bc576f 1384}
dff5ffe4 1385{
127ce1cd 1386 no feature ':all';
dff5ffe4
FC
1387 use feature ':5.12';
1388 print /a/d, s/b/c/d;
1389}
b9bc576f 1390####
dc6dfd62
LM
1391# all the flags (qr//)
1392$_ = qr/X/m;
1393$_ = qr/X/s;
1394$_ = qr/X/i;
1395$_ = qr/X/x;
1396$_ = qr/X/p;
1397$_ = qr/X/o;
1398$_ = qr/X/u;
1399$_ = qr/X/a;
1400$_ = qr/X/l;
1401$_ = qr/X/n;
1402####
1403use feature 'unicode_strings';
1404$_ = qr/X/d;
1405####
1406# all the flags (m//)
1407/X/m;
1408/X/s;
1409/X/i;
1410/X/x;
1411/X/p;
1412/X/o;
1413/X/u;
1414/X/a;
1415/X/l;
1416/X/n;
1417/X/g;
1418/X/cg;
1419####
1420use feature 'unicode_strings';
1421/X/d;
1422####
1423# all the flags (s///)
1424s/X//m;
1425s/X//s;
1426s/X//i;
1427s/X//x;
1428s/X//p;
1429s/X//o;
1430s/X//u;
1431s/X//a;
1432s/X//l;
1433s/X//n;
1434s/X//g;
1435s/X/'';/e;
1436s/X//r;
1437####
1438use feature 'unicode_strings';
1439s/X//d;
1440####
4fc9437a
DM
1441# tr/// with all the flags: empty replacement
1442tr/B-G//;
1443tr/B-G//c;
1444tr/B-G//d;
1445tr/B-G//s;
1446tr/B-G//cd;
1447tr/B-G//ds;
1448tr/B-G//cs;
1449tr/B-G//cds;
1450tr/B-G//r;
1451####
1452# tr/// with all the flags: short replacement
1453tr/B-G/b/;
1454tr/B-G/b/c;
1455tr/B-G/b/d;
1456tr/B-G/b/s;
1457tr/B-G/b/cd;
1458tr/B-G/b/ds;
1459tr/B-G/b/cs;
1460tr/B-G/b/cds;
1461tr/B-G/b/r;
1462####
1463# tr/// with all the flags: equal length replacement
1464tr/B-G/b-g/;
1465tr/B-G/b-g/c;
1466tr/B-G/b-g/s;
1467tr/B-G/b-g/cs;
1468tr/B-G/b-g/r;
dc6dfd62 1469####
c923a699
DM
1470# tr with extended table (/c)
1471tr/\000-\375/AB/c;
1472tr/\000-\375/A-C/c;
1473tr/\000-\375/A-D/c;
1848346f 1474tr/\000-\375/A-I/c;
c923a699
DM
1475tr/\000-\375/AB/cd;
1476tr/\000-\375/A-C/cd;
1477tr/\000-\375/A-D/cd;
1848346f 1478tr/\000-\375/A-I/cd;
c923a699
DM
1479tr/\000-\375/AB/cds;
1480tr/\000-\375/A-C/cds;
1481tr/\000-\375/A-D/cds;
1848346f 1482tr/\000-\375/A-I/cds;
c923a699 1483####
9f125c4a
FC
1484# [perl #119807] s//\(3)/ge should not warn when deparsed (\3 warns)
1485s/foo/\(3);/eg;
1486####
c9fa6ae9 1487# [perl #115256]
c9fa6ae9
FC
1488"" =~ /a(?{ print q|
1489|})/;
1490>>>>
f6b6ee63 1491'' =~ /a(?{ print "\n"; })/;
c9fa6ae9
FC
1492####
1493# [perl #123217]
c9fa6ae9
FC
1494$_ = qr/(??{<<END})/
1495f.o
1496b.r
1497END
1498>>>>
1499$_ = qr/(??{ "f.o\nb.r\n"; })/;
1500####
1501# More regexp code block madness
c9fa6ae9
FC
1502my($b, @a);
1503/(?{ die $b; })/;
1504/a(?{ die $b; })a/;
1505/$a(?{ die $b; })/;
1506/@a(?{ die $b; })/;
1507/(??{ die $b; })/;
1508/a(??{ die $b; })a/;
1509/$a(??{ die $b; })/;
1510/@a(??{ die $b; })/;
1511qr/(?{ die $b; })/;
1512qr/a(?{ die $b; })a/;
1513qr/$a(?{ die $b; })/;
1514qr/@a(?{ die $b; })/;
1515qr/(??{ die $b; })/;
1516qr/a(??{ die $b; })a/;
1517qr/$a(??{ die $b; })/;
1518qr/@a(??{ die $b; })/;
1519s/(?{ die $b; })//;
1520s/a(?{ die $b; })a//;
1521s/$a(?{ die $b; })//;
1522s/@a(?{ die $b; })//;
1523s/(??{ die $b; })//;
1524s/a(??{ die $b; })a//;
1525s/$a(??{ die $b; })//;
1526s/@a(??{ die $b; })//;
1527####
ba0372a0
FC
1528# /(?x)<newline><tab>/
1529/(?x)
1530 /;
1531####
e7afc405 1532# y///r
d52196e1 1533tr/a/b/r + $a =~ tr/p/q/r;
cb8157e3 1534####
12cea2fa
FC
1535# y///d in list [perl #119815]
1536() = tr/a//d;
1537####
cb8578ff 1538# [perl #90898]
f4002a4b 1539<a,>;
18371617
LM
1540glob 'a,';
1541>>>>
1542glob 'a,';
1543glob 'a,';
09dcfa7d
FC
1544####
1545# [perl #91008]
26230909 1546# SKIP ?$] >= 5.023 && "autoderef deleted in this Perl version"
d401967c 1547# CONTEXT no warnings 'experimental::autoderef';
09dcfa7d
FC
1548each $@;
1549keys $~;
1550values $!;
5d8c42c2
FC
1551####
1552# readpipe with complex expression
1553readpipe $a + $b;
93bad3fd
NC
1554####
1555# aelemfast
1556$b::a[0] = 1;
1557####
1558# aelemfast for a lexical
1559my @a;
1560$a[0] = 1;
80e3f4ad
FC
1561####
1562# feature features without feature
0f539b13 1563# CONTEXT no warnings 'experimental::smartmatch';
80e3f4ad 1564CORE::state $x;
223b1722
FC
1565CORE::say $x;
1566CORE::given ($x) {
7896dde7 1567 CORE::when (3) {
223b1722
FC
1568 continue;
1569 }
7896dde7
Z
1570 CORE::default {
1571 CORE::break;
223b1722
FC
1572 }
1573}
1574CORE::evalbytes '';
1575() = CORE::__SUB__;
838f2281 1576() = CORE::fc $x;
223b1722
FC
1577####
1578# feature features when feature has been disabled by use VERSION
0f539b13 1579# CONTEXT no warnings 'experimental::smartmatch';
223b1722
FC
1580use feature (sprintf(":%vd", $^V));
1581use 1;
412989c2 1582CORE::say $_;
223b1722 1583CORE::state $x;
223b1722 1584CORE::given ($x) {
7896dde7 1585 CORE::when (3) {
223b1722
FC
1586 continue;
1587 }
7896dde7
Z
1588 CORE::default {
1589 CORE::break;
223b1722
FC
1590 }
1591}
1592CORE::evalbytes '';
1593() = CORE::__SUB__;
1594>>>>
412989c2 1595CORE::say $_;
205fef88 1596CORE::state $x;
205fef88 1597CORE::given ($x) {
7896dde7 1598 CORE::when (3) {
205fef88
NC
1599 continue;
1600 }
7896dde7
Z
1601 CORE::default {
1602 CORE::break;
205fef88
NC
1603 }
1604}
1605CORE::evalbytes '';
1606() = CORE::__SUB__;
1607####
1608# (the above test with CONTEXT, and the output is equivalent but different)
0f539b13 1609# CONTEXT use feature ':5.10'; no warnings 'experimental::smartmatch';
205fef88
NC
1610# feature features when feature has been disabled by use VERSION
1611use feature (sprintf(":%vd", $^V));
1612use 1;
412989c2 1613CORE::say $_;
205fef88 1614CORE::state $x;
205fef88 1615CORE::given ($x) {
7896dde7 1616 CORE::when (3) {
205fef88
NC
1617 continue;
1618 }
7896dde7
Z
1619 CORE::default {
1620 CORE::break;
205fef88
NC
1621 }
1622}
1623CORE::evalbytes '';
1624() = CORE::__SUB__;
1625>>>>
127ce1cd 1626no feature ':all';
0bb01b05 1627use feature ':default';
412989c2 1628CORE::say $_;
223b1722 1629CORE::state $x;
80e3f4ad 1630CORE::given ($x) {
7896dde7 1631 CORE::when (3) {
80e3f4ad
FC
1632 continue;
1633 }
7896dde7
Z
1634 CORE::default {
1635 CORE::break;
80e3f4ad
FC
1636 }
1637}
7d789282 1638CORE::evalbytes '';
84ed0108 1639() = CORE::__SUB__;
6ec73527 1640####
7741ceed
FC
1641# SKIP ?$] < 5.017004 && "lexical subs not implemented on this Perl version"
1642# lexical subroutines and keywords of the same name
1643# CONTEXT use feature 'lexical_subs', 'switch'; no warnings 'experimental';
7896dde7 1644my sub default;
7741ceed
FC
1645my sub else;
1646my sub elsif;
1647my sub for;
1648my sub foreach;
1649my sub given;
1650my sub if;
1651my sub m;
1652my sub no;
1653my sub package;
1654my sub q;
1655my sub qq;
1656my sub qr;
1657my sub qx;
1658my sub require;
1659my sub s;
1660my sub sub;
1661my sub tr;
1662my sub unless;
1663my sub until;
1664my sub use;
7896dde7 1665my sub when;
7741ceed 1666my sub while;
7896dde7 1667CORE::default { die; }
7741ceed
FC
1668CORE::if ($1) { die; }
1669CORE::if ($1) { die; }
1670CORE::elsif ($1) { die; }
1671CORE::else { die; }
1672CORE::for (die; $1; die) { die; }
1673CORE::foreach $_ (1 .. 10) { die; }
1674die CORE::foreach (1);
1675CORE::given ($1) { die; }
1676CORE::m[/];
1677CORE::m?/?;
1678CORE::package foo;
1679CORE::no strict;
1680() = (CORE::q['], CORE::qq["$_], CORE::qr//, CORE::qx[`]);
1681CORE::require 1;
1682CORE::s///;
1683() = CORE::sub { die; } ;
1684CORE::tr///;
1685CORE::unless ($1) { die; }
1686CORE::until ($1) { die; }
1687die CORE::until $1;
1688CORE::use strict;
7896dde7 1689CORE::when ($1 ~~ $2) { die; }
7741ceed
FC
1690CORE::while ($1) { die; }
1691die CORE::while $1;
1692####
0bb01b05
FC
1693# Feature hints
1694use feature 'current_sub', 'evalbytes';
1695print;
1696use 1;
1697print;
1698use 5.014;
1699print;
1700no feature 'unicode_strings';
1701print;
1702>>>>
a8095af7 1703use feature 'current_sub', 'evalbytes';
0bb01b05 1704print $_;
127ce1cd 1705no feature ':all';
0bb01b05
FC
1706use feature ':default';
1707print $_;
127ce1cd 1708no feature ':all';
0bb01b05
FC
1709use feature ':5.12';
1710print $_;
a8095af7 1711no feature 'unicode_strings';
0bb01b05
FC
1712print $_;
1713####
6ec73527
FC
1714# $#- $#+ $#{%} etc.
1715my @x;
5b6da579 1716@x = ($#{`}, $#{~}, $#{!}, $#{@}, $#{$}, $#{%}, $#{^}, $#{&}, $#{*});
6ec73527
FC
1717@x = ($#{(}, $#{)}, $#{[}, $#{{}, $#{]}, $#{}}, $#{'}, $#{"}, $#{,});
1718@x = ($#{<}, $#{.}, $#{>}, $#{/}, $#{?}, $#{=}, $#+, $#{\}, $#{|}, $#-);
b6bba886 1719@x = ($#{;}, $#{:}, $#{1}), $#_;
61154ac0 1720####
337d7381
FC
1721# [perl #86060] $( $| $) in regexps need braces
1722/${(}/;
1723/${|}/;
1724/${)}/;
1725/${(}${|}${)}/;
3f193e55 1726/@{+}@{-}/;
337d7381 1727####
958ed56b
FC
1728# ()[...]
1729my(@a) = ()[()];
521795fe
FC
1730####
1731# sort(foo(bar))
1732# sort(foo(bar)) is interpreted as sort &foo(bar)
1733# sort foo(bar) is interpreted as sort foo bar
1734# parentheses are not optional in this case
1735print sort(foo('bar'));
1736>>>>
1737print sort(foo('bar'));
24fcb59f
FC
1738####
1739# substr assignment
1740substr(my $a, 0, 0) = (foo(), bar());
1741$a++;
04be0204 1742####
d1718a7c
FC
1743# This following line works around an unfixed bug that we are not trying to
1744# test for here:
1745# CONTEXT BEGIN { $^H{a} = "b"; delete $^H{a} } # make %^H localised
04be0204
FC
1746# hint hash
1747BEGIN { $^H{'foo'} = undef; }
1748{
1749 BEGIN { $^H{'bar'} = undef; }
1750 {
1751 BEGIN { $^H{'baz'} = undef; }
1752 {
1753 print $_;
1754 }
1755 print $_;
1756 }
1757 print $_;
1758}
035146a3
FC
1759BEGIN { $^H{q[']} = '('; }
1760print $_;
c306e834 1761####
d1718a7c
FC
1762# This following line works around an unfixed bug that we are not trying to
1763# test for here:
1764# CONTEXT BEGIN { $^H{a} = "b"; delete $^H{a} } # make %^H localised
c306e834
FC
1765# hint hash changes that serialise the same way with sort %hh
1766BEGIN { $^H{'a'} = 'b'; }
1767{
1768 BEGIN { $^H{'b'} = 'a'; delete $^H{'a'}; }
1769 print $_;
1770}
1771print $_;
94bb57f9
FC
1772####
1773# [perl #47361] do({}) and do +{} (variants of do-file)
1774do({});
1775do +{};
8b46c09b
FC
1776sub foo::do {}
1777package foo;
1778CORE::do({});
1779CORE::do +{};
94bb57f9
FC
1780>>>>
1781do({});
1782do({});
8b46c09b
FC
1783package foo;
1784CORE::do({});
1785CORE::do({});
9c56d9ea
FC
1786####
1787# [perl #77096] functions that do not follow the llafr
1788() = (return 1) + time;
1789() = (return ($1 + $2) * $3) + time;
1790() = (return ($a xor $b)) + time;
1791() = (do 'file') + time;
1792() = (do ($1 + $2) * $3) + time;
1793() = (do ($1 xor $2)) + time;
41df74e3
FC
1794() = (goto 1) + 3;
1795() = (require 'foo') + 3;
1796() = (require foo) + 3;
266da325 1797() = (CORE::dump 1) + 3;
41df74e3
FC
1798() = (last 1) + 3;
1799() = (next 1) + 3;
1800() = (redo 1) + 3;
5830412d
FC
1801() = (-R $_) + 3;
1802() = (-W $_) + 3;
1803() = (-X $_) + 3;
1804() = (-r $_) + 3;
1805() = (-w $_) + 3;
1806() = (-x $_) + 3;
2462c3cc 1807####
917a8f4f
FC
1808# require(foo()) and do(foo())
1809require (foo());
1810do (foo());
df465735
FC
1811goto (foo());
1812CORE::dump (foo());
1813last (foo());
1814next (foo());
1815redo (foo());
917a8f4f 1816####
5e7acd25
FC
1817# require vstring
1818require v5.16;
1819####
1cabb3b3
FC
1820# [perl #97476] not() *does* follow the llafr
1821$_ = ($a xor not +($1 || 2) ** 2);
1822####
4d8ac5c7
FC
1823# Precedence conundrums with argument-less function calls
1824() = (eof) + 1;
1825() = (return) + 1;
1826() = (return, 1);
7bc8c979
FC
1827() = warn;
1828() = warn() + 1;
4d8ac5c7
FC
1829() = setpgrp() + 1;
1830####
1eb0b7be
FC
1831# loopexes have assignment prec
1832() = (CORE::dump a) | 'b';
1833() = (goto a) | 'b';
1834() = (last a) | 'b';
1835() = (next a) | 'b';
1836() = (redo a) | 'b';
1837####
2462c3cc
FC
1838# [perl #63558] open local(*FH)
1839open local *FH;
564cd6cb 1840pipe local *FH, local *FH;
843b15cc 1841####
b89b7257
FC
1842# [perl #91416] open "string"
1843open 'open';
1844open '####';
1845open '^A';
1846open "\ca";
1847>>>>
1848open *open;
1849open '####';
1850open '^A';
1851open *^A;
1852####
be6cf5cf
FC
1853# "string"->[] ->{}
1854no strict 'vars';
1855() = 'open'->[0]; #aelemfast
1856() = '####'->[0];
1857() = '^A'->[0];
1858() = "\ca"->[0];
b861b87f 1859() = 'a::]b'->[0];
10e8e32b
FC
1860() = 'open'->[$_]; #aelem
1861() = '####'->[$_];
1862() = '^A'->[$_];
1863() = "\ca"->[$_];
b861b87f 1864() = 'a::]b'->[$_];
10e8e32b
FC
1865() = 'open'->{0}; #helem
1866() = '####'->{0};
1867() = '^A'->{0};
1868() = "\ca"->{0};
b861b87f 1869() = 'a::]b'->{0};
be6cf5cf 1870>>>>
415d4c68 1871no strict 'vars';
be6cf5cf
FC
1872() = $open[0];
1873() = '####'->[0];
1874() = '^A'->[0];
1875() = $^A[0];
b861b87f 1876() = 'a::]b'->[0];
10e8e32b
FC
1877() = $open[$_];
1878() = '####'->[$_];
1879() = '^A'->[$_];
1880() = $^A[$_];
b861b87f 1881() = 'a::]b'->[$_];
10e8e32b
FC
1882() = $open{'0'};
1883() = '####'->{'0'};
1884() = '^A'->{'0'};
1885() = $^A{'0'};
b861b87f 1886() = 'a::]b'->{'0'};
be6cf5cf 1887####
843b15cc
FC
1888# [perl #74740] -(f()) vs -f()
1889$_ = -(f());
c75b4828
FC
1890####
1891# require <binop>
1892require 'a' . $1;
afb60448
HY
1893####
1894#[perl #30504] foreach-my postfix/prefix difference
1895$_ = 'foo' foreach my ($foo1, $bar1, $baz1);
1896foreach (my ($foo2, $bar2, $baz2)) { $_ = 'foo' }
1897foreach my $i (my ($foo3, $bar3, $baz3)) { $i = 'foo' }
1898>>>>
1899$_ = 'foo' foreach (my($foo1, $bar1, $baz1));
1900foreach $_ (my($foo2, $bar2, $baz2)) {
1901 $_ = 'foo';
1902}
1903foreach my $i (my($foo3, $bar3, $baz3)) {
1904 $i = 'foo';
1905}
1906####
1907#[perl #108224] foreach with continue block
1908foreach (1 .. 3) { print } continue { print "\n" }
1909foreach (1 .. 3) { } continue { }
1910foreach my $i (1 .. 3) { print $i } continue { print "\n" }
1911foreach my $i (1 .. 3) { } continue { }
1912>>>>
1913foreach $_ (1 .. 3) {
1914 print $_;
1915}
1916continue {
1917 print "\n";
1918}
1919foreach $_ (1 .. 3) {
1920 ();
1921}
1922continue {
1923 ();
1924}
1925foreach my $i (1 .. 3) {
1926 print $i;
1927}
1928continue {
1929 print "\n";
1930}
1931foreach my $i (1 .. 3) {
1932 ();
1933}
1934continue {
1935 ();
1936}
bc1cc2c3
DM
1937####
1938# file handles
1939no strict;
1940my $mfh;
1941open F;
1942open *F;
1943open $fh;
1944open $mfh;
1945open 'a+b';
1946select *F;
1947select F;
1948select $f;
1949select $mfh;
1950select 'a+b';
a7fd8ef6
DM
1951####
1952# 'my' works with padrange op
1953my($z, @z);
1954my $m1;
1955$m1 = 1;
1956$z = $m1;
1957my $m2 = 2;
1958my($m3, $m4);
1959($m3, $m4) = (1, 2);
1960@z = ($m3, $m4);
1961my($m5, $m6) = (1, 2);
1962my($m7, undef, $m8) = (1, 2, 3);
1963@z = ($m7, undef, $m8);
1964($m7, undef, $m8) = (1, 2, 3);
1965####
1966# 'our/local' works with padrange op
a7fd8ef6
DM
1967our($z, @z);
1968our $o1;
0298c760 1969no strict;
a7fd8ef6
DM
1970local $o11;
1971$o1 = 1;
1972local $o1 = 1;
1973$z = $o1;
1974$z = local $o1;
1975our $o2 = 2;
1976our($o3, $o4);
1977($o3, $o4) = (1, 2);
1978local($o3, $o4) = (1, 2);
1979@z = ($o3, $o4);
1980@z = local($o3, $o4);
1981our($o5, $o6) = (1, 2);
1982our($o7, undef, $o8) = (1, 2, 3);
1983@z = ($o7, undef, $o8);
1984@z = local($o7, undef, $o8);
1985($o7, undef, $o8) = (1, 2, 3);
1986local($o7, undef, $o8) = (1, 2, 3);
1987####
1988# 'state' works with padrange op
412989c2 1989# CONTEXT no strict; use feature 'state';
a7fd8ef6
DM
1990state($z, @z);
1991state $s1;
1992$s1 = 1;
1993$z = $s1;
1994state $s2 = 2;
1995state($s3, $s4);
1996($s3, $s4) = (1, 2);
1997@z = ($s3, $s4);
1998# assignment of state lists isn't implemented yet
1999#state($s5, $s6) = (1, 2);
2000#state($s7, undef, $s8) = (1, 2, 3);
2001#@z = ($s7, undef, $s8);
2002($s7, undef, $s8) = (1, 2, 3);
2003####
df2b00e8 2004# anon arrays with padrange
a7fd8ef6
DM
2005my($a, $b);
2006my $c = [$a, $b];
2007my $d = {$a, $b};
2008####
2009# slices with padrange
2010my($a, $b);
2011my(@x, %y);
2012@x = @x[$a, $b];
2013@x = @y{$a, $b};
2014####
2015# binops with padrange
2016my($a, $b, $c);
2017$c = $a cmp $b;
2018$c = $a + $b;
2019$a += $b;
2020$c = $a - $b;
2021$a -= $b;
2022$c = my $a1 cmp $b;
2023$c = my $a2 + $b;
2024$a += my $b1;
2025$c = my $a3 - $b;
2026$a -= my $b2;
2027####
2028# 'x' with padrange
2029my($a, $b, $c, $d, @e);
2030$c = $a x $b;
2031$a x= $b;
2032@e = ($a) x $d;
2033@e = ($a, $b) x $d;
2034@e = ($a, $b, $c) x $d;
2035@e = ($a, 1) x $d;
d5524600
DM
2036####
2037# @_ with padrange
2038my($a, $b, $c) = @_;
ce4062e7
AC
2039####
2040# SKIP ?$] < 5.017004 && "lexical subs not implemented on this Perl version"
ce4062e7 2041# lexical subroutine
db629560 2042# CONTEXT use feature 'lexical_subs';
601448c3 2043no warnings "experimental::lexical_subs";
ce4062e7
AC
2044my sub f {}
2045print f();
d4f1bfe7 2046>>>>
15a9bc0d 2047BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x54\x55\x55\x55\x55\x55"}
d4f1bfe7 2048my sub f {
d4f1bfe7
FC
2049
2050}
d4f1bfe7 2051print f();
f0cf3754
AC
2052####
2053# SKIP ?$] < 5.017004 && "lexical subs not implemented on this Perl version"
f0cf3754 2054# lexical "state" subroutine
db629560 2055# CONTEXT use feature 'state', 'lexical_subs';
f0cf3754
AC
2056no warnings 'experimental::lexical_subs';
2057state sub f {}
2058print f();
d4f1bfe7 2059>>>>
15a9bc0d 2060BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x54\x55\x55\x55\x55\x55"}
db629560 2061state sub f {
d4f1bfe7
FC
2062
2063}
d4f1bfe7 2064print f();
bcbe2b27 2065####
8443930e 2066# SKIP ?$] < 5.017004 && "lexical subs not implemented on this Perl version"
8443930e
FC
2067# lexical subroutine scoping
2068# CONTEXT use feature 'lexical_subs'; no warnings 'experimental::lexical_subs';
2069{
2070 {
2071 my sub a { die; }
2072 {
2073 foo();
2074 my sub b;
bb9bfaa4 2075 b ;
8443930e 2076 main::b();
d49c3562
FC
2077 &main::b;
2078 &main::b();
2079 my $b = \&main::b;
2080 sub b { $b; }
8443930e
FC
2081 }
2082 }
2083 b();
2084}
2085####
bb9bfaa4
FC
2086# self-referential lexical subroutine
2087# CONTEXT use feature 'lexical_subs', 'state'; no warnings 'experimental::lexical_subs';
2088();
2089state sub sb2;
2090sub sb2 {
2091 sb2 ;
2092}
2093####
494a4b9c
FC
2094# lexical subroutine with outer declaration and inner definition
2095# CONTEXT use feature 'lexical_subs'; no warnings 'experimental::lexical_subs';
2096();
2097my sub f;
2098my sub g {
2099 ();
2100 sub f { }
2101}
2102####
2c5ddcd3
FC
2103# TODO only partially fixed
2104# lexical state subroutine with outer declaration and inner definition
2105# CONTEXT use feature 'lexical_subs', 'state'; no warnings 'experimental::lexical_subs';
2106();
2107state sub sb4;
2108state sub a {
2109 ();
2110 sub sb4 { }
2111}
2112state sub sb5;
2113sub {
2114 ();
2115 sub sb5 { }
2116} ;
2117####
bcbe2b27
FC
2118# Elements of %# should not be confused with $#{ array }
2119() = ${#}{'foo'};
c4cf781e 2120####
cfd916dd 2121# $; [perl #123357]
ddb55548
FC
2122$_ = $;;
2123do {
2124 $;
2125};
2126####
c65b7c4d
FC
2127# Ampersand calls and scalar context
2128# OPTIONS -P
2129package prototest;
2130sub foo($$);
2131foo(bar(),baz());
2132>>>>
2133package prototest;
2134&foo(scalar bar(), scalar baz());
2135####
de4fa237
FC
2136# coderef2text and prototyped sub calls [perl #123435]
2137is 'foo', 'oo';
2138####
fd8be4a1
FC
2139# prototypes with unary precedence
2140package prototest;
2141sub dollar($) {}
2142sub optdollar(;$) {}
2143sub optoptdollar(;;$) {}
2144sub splat(*) {}
2145sub optsplat(;*) {}
2146sub optoptsplat(;;*) {}
2147sub bar(_) {}
2148sub optbar(;_) {}
2149sub optoptbar(;;_) {}
2150sub plus(+) {}
2151sub optplus(;+) {}
2152sub optoptplus(;;+) {}
2153sub wack(\$) {}
2154sub optwack(;\$) {}
2155sub optoptwack(;;\$) {}
2156sub wackbrack(\[$]) {}
2157sub optwackbrack(;\[$]) {}
2158sub optoptwackbrack(;;\[$]) {}
2159dollar($a < $b);
2160optdollar($a < $b);
2161optoptdollar($a < $b);
2162splat($a < $b); # Some of these deparse with ‘&’; if that changes, just
2163optsplat($a < $b); # change the tests.
2164optoptsplat($a < $b);
2165bar($a < $b);
2166optbar($a < $b);
2167optoptbar($a < $b);
2168plus($a < $b);
2169optplus($a < $b);
2170optoptplus($a < $b);
2171wack($a = $b);
2172optwack($a = $b);
2173optoptwack($a = $b);
2174wackbrack($a = $b);
2175optwackbrack($a = $b);
2176optoptwackbrack($a = $b);
2177>>>>
2178package prototest;
2179dollar($a < $b);
2180optdollar($a < $b);
2181optoptdollar($a < $b);
2182&splat($a < $b);
2183&optsplat($a < $b);
2184&optoptsplat($a < $b);
2185bar($a < $b);
2186optbar($a < $b);
2187optoptbar($a < $b);
2188&plus($a < $b);
2189&optplus($a < $b);
2190&optoptplus($a < $b);
2191&wack(\($a = $b));
2192&optwack(\($a = $b));
2193&optoptwack(\($a = $b));
2194&wackbrack(\($a = $b));
2195&optwackbrack(\($a = $b));
2196&optoptwackbrack(\($a = $b));
2197####
b024352e
DM
2198# ensure aelemfast works in the range -128..127 and that there's no
2199# funky edge cases
2200my $x;
2201no strict 'vars';
2202$x = $a[-256] + $a[-255] + $a[-129] + $a[-128] + $a[-127] + $a[-1] + $a[0];
2203$x = $a[1] + $a[126] + $a[127] + $a[128] + $a[255] + $a[256];
2204my @b;
2205$x = $b[-256] + $b[-255] + $b[-129] + $b[-128] + $b[-127] + $b[-1] + $b[0];
2206$x = $b[1] + $b[126] + $b[127] + $b[128] + $b[255] + $b[256];
e09d73a6
DIM
2207####
2208# 'm' must be preserved in m??
2209m??;
c8ec376c
FC
2210####
2211# \(@array) and \(..., (@array), ...)
2212my(@array, %hash, @a, @b, %c, %d);
2213() = \(@array);
2214() = \(%hash);
2215() = \(@a, (@b), (%c), %d);
2216() = \(@Foo::array);
2217() = \(%Foo::hash);
2218() = \(@Foo::a, (@Foo::b), (%Foo::c), %Foo::d);
a958cfbb
FC
2219####
2220# subs synonymous with keywords
2221main::our();
2222main::pop();
2223state();
2224use feature 'state';
2225main::state();
9187b6e4
FC
2226####
2227# lvalue references
baabe3fb 2228# CONTEXT use feature "state", 'refaliasing', 'lexical_subs'; no warnings 'experimental';
9187b6e4
FC
2229our $x;
2230\$x = \$x;
2231my $m;
2232\$m = \$x;
2233\my $n = \$x;
2234(\$x) = @_;
2235\($x) = @_;
2236\($m) = @_;
2237(\$m) = @_;
2238\my($p) = @_;
2239(\my $r) = @_;
2240\($x, my $a) = @{[\$x, \$x]};
2241(\$x, \my $b) = @{[\$x, \$x]};
2242\local $x = \3;
2243\local($x) = \3;
2244\state $c = \3;
2245\state($d) = \3;
2246\our $e = \3;
2247\our($f) = \3;
2248\$_[0] = foo();
2249\($_[1]) = foo();
2250my @a;
2251\$a[0] = foo();
2252\($a[1]) = foo();
2253\local($a[1]) = foo();
2254\@a[0,1] = foo();
2255\(@a[2,3]) = foo();
2256\local @a[0,1] = (\$a)x2;
2257\$_{a} = foo();
2258\($_{b}) = foo();
2259my %h;
2260\$h{a} = foo();
2261\($h{b}) = foo();
2262\local $h{a} = \$x;
2263\local($h{b}) = \$x;
2264\@h{'a','b'} = foo();
2265\(@h{2,3}) = foo();
2266\local @h{'a','b'} = (\$x)x2;
2267\@_ = foo();
2268\@a = foo();
2269(\@_) = foo();
2270(\@a) = foo();
2271\my @c = foo();
2272(\my @d) = foo();
2273\(@_) = foo();
2274\(@a) = foo();
2275\my(@g) = foo();
2276\local @_ = \@_;
2277(\local @_) = \@_;
2278\state @e = [1..3];
2279\state(@f) = \3;
2280\our @i = [1..3];
2281\our(@h) = \3;
2282\%_ = foo();
2283\%h = foo();
2284(\%_) = foo();
2285(\%h) = foo();
2286\my %c = foo();
2287(\my %d) = foo();
2288\local %_ = \%h;
2289(\local %_) = \%h;
2290\state %y = {1,2};
2291\our %z = {1,2};
2292(\our %zz) = {1,2};
2293\&a = foo();
2294(\&a) = foo();
2295\(&a) = foo();
2296{
2297 my sub a;
2298 \&a = foo();
2299 (\&a) = foo();
2300 \(&a) = foo();
2301}
2302(\$_, $_) = \(1, 2);
2303$_ == 3 ? \$_ : $_ = \3;
2304$_ == 3 ? \$_ : \$x = \3;
2305\($_ == 3 ? $_ : $x) = \3;
2306for \my $topic (\$1, \$2) {
2307 die;
2308}
2309for \state $topic (\$1, \$2) {
2310 die;
2311}
2312for \our $topic (\$1, \$2) {
2313 die;
2314}
2315for \$_ (\$1, \$2) {
2316 die;
2317}
2318for \my @a ([1,2], [3,4]) {
2319 die;
2320}
2321for \state @a ([1,2], [3,4]) {
2322 die;
2323}
2324for \our @a ([1,2], [3,4]) {
2325 die;
2326}
2327for \@_ ([1,2], [3,4]) {
2328 die;
2329}
2330for \my %a ({5,6}, {7,8}) {
2331 die;
2332}
2333for \our %a ({5,6}, {7,8}) {
2334 die;
2335}
2336for \state %a ({5,6}, {7,8}) {
2337 die;
2338}
2339for \%_ ({5,6}, {7,8}) {
2340 die;
2341}
2342{
2343 my sub a;
2344 for \&a (sub { 9; }, sub { 10; }) {
2345 die;
2346 }
2347}
2348for \&a (sub { 9; }, sub { 10; }) {
2349 die;
2350}
2351>>>>
2352our $x;
2353\$x = \$x;
2354my $m;
2355\$m = \$x;
2356\my $n = \$x;
2357(\$x) = @_;
2358(\$x) = @_;
2359(\$m) = @_;
2360(\$m) = @_;
2361(\my $p) = @_;
2362(\my $r) = @_;
2363(\$x, \my $a) = @{[\$x, \$x];};
2364(\$x, \my $b) = @{[\$x, \$x];};
2365\local $x = \3;
2366(\local $x) = \3;
2367\state $c = \3;
2368(\state $d) = \3;
2369\our $e = \3;
2370(\our $f) = \3;
2371\$_[0] = foo();
2372(\$_[1]) = foo();
2373my @a;
2374\$a[0] = foo();
2375(\$a[1]) = foo();
2376(\local $a[1]) = foo();
2377(\@a[0, 1]) = foo();
2378(\@a[2, 3]) = foo();
2379(\local @a[0, 1]) = (\$a) x 2;
2380\$_{'a'} = foo();
2381(\$_{'b'}) = foo();
2382my %h;
2383\$h{'a'} = foo();
2384(\$h{'b'}) = foo();
2385\local $h{'a'} = \$x;
2386(\local $h{'b'}) = \$x;
2387(\@h{'a', 'b'}) = foo();
2388(\@h{2, 3}) = foo();
2389(\local @h{'a', 'b'}) = (\$x) x 2;
2390\@_ = foo();
2391\@a = foo();
2392(\@_) = foo();
2393(\@a) = foo();
2394\my @c = foo();
2395(\my @d) = foo();
2396(\(@_)) = foo();
2397(\(@a)) = foo();
2398(\(my @g)) = foo();
2399\local @_ = \@_;
2400(\local @_) = \@_;
2401\state @e = [1..3];
2402(\(state @f)) = \3;
2403\our @i = [1..3];
2404(\(our @h)) = \3;
2405\%_ = foo();
2406\%h = foo();
2407(\%_) = foo();
2408(\%h) = foo();
2409\my %c = foo();
2410(\my %d) = foo();
2411\local %_ = \%h;
2412(\local %_) = \%h;
2413\state %y = {1, 2};
2414\our %z = {1, 2};
2415(\our %zz) = {1, 2};
2416\&a = foo();
2417(\&a) = foo();
2418(\&a) = foo();
2419{
2420 my sub a;
2421 \&a = foo();
2422 (\&a) = foo();
2423 (\&a) = foo();
2424}
2425(\$_, $_) = \(1, 2);
2426$_ == 3 ? \$_ : $_ = \3;
2427$_ == 3 ? \$_ : \$x = \3;
2428($_ == 3 ? \$_ : \$x) = \3;
2429foreach \my $topic (\$1, \$2) {
2430 die;
2431}
2432foreach \state $topic (\$1, \$2) {
2433 die;
2434}
2435foreach \our $topic (\$1, \$2) {
2436 die;
2437}
2438foreach \$_ (\$1, \$2) {
2439 die;
2440}
2441foreach \my @a ([1, 2], [3, 4]) {
2442 die;
2443}
2444foreach \state @a ([1, 2], [3, 4]) {
2445 die;
2446}
2447foreach \our @a ([1, 2], [3, 4]) {
2448 die;
2449}
2450foreach \@_ ([1, 2], [3, 4]) {
2451 die;
2452}
2453foreach \my %a ({5, 6}, {7, 8}) {
2454 die;
2455}
2456foreach \our %a ({5, 6}, {7, 8}) {
2457 die;
2458}
2459foreach \state %a ({5, 6}, {7, 8}) {
2460 die;
2461}
2462foreach \%_ ({5, 6}, {7, 8}) {
2463 die;
2464}
2465{
2466 my sub a;
2467 foreach \&a (sub { 9; } , sub { 10; } ) {
2468 die;
2469 }
2470}
2471foreach \&a (sub { 9; } , sub { 10; } ) {
2472 die;
2473}
3b4e2a4d
FC
2474####
2475# join $foo, pos
2476my $foo;
2477$_ = join $foo, pos
2478>>>>
2479my $foo;
2480$_ = join('???', pos $_);
fedf30e1
DM
2481####
2482# exists $a[0]
2483our @a;
2484exists $a[0];
2485####
2486# my @a; exists $a[0]
2487my @a;
2488exists $a[0];
2489####
2490# delete $a[0]
2491our @a;
2492delete $a[0];
2493####
2494# my @a; delete $a[0]
2495my @a;
2496delete $a[0];
2497####
2498# $_[0][$_[1]]
2499$_[0][$_[1]];
2500####
2501# f($a[0]);
2502my @a;
2503f($a[0]);
2504####
2505#qr/\Q$h{'key'}\E/;
2506my %h;
2507qr/\Q$h{'key'}\E/;
2508####
2509# my $x = "$h{foo}";
2510my %h;
2511my $x = "$h{'foo'}";
2512####
2513# weird constant hash key
2514my %h;
2515my $x = $h{"\000\t\x{100}"};
2516####
2517# multideref and packages
2518package foo;
2519my(%bar) = ('a', 'b');
2520our(@bar) = (1, 2);
2521$bar{'k'} = $bar[200];
2522$main::bar{'k'} = $main::bar[200];
2523$foo::bar{'k'} = $foo::bar[200];
2524package foo2;
2525$bar{'k'} = $bar[200];
2526$main::bar{'k'} = $main::bar[200];
2527$foo::bar{'k'} = $foo::bar[200];
2528>>>>
2529package foo;
2530my(%bar) = ('a', 'b');
2531our(@bar) = (1, 2);
2532$bar{'k'} = $bar[200];
2533$main::bar{'k'} = $main::bar[200];
2534$foo::bar{'k'} = $bar[200];
2535package foo2;
2536$bar{'k'} = $foo::bar[200];
2537$main::bar{'k'} = $main::bar[200];
2538$foo::bar{'k'} = $foo::bar[200];
2539####
2540# multideref and local
2541my %h;
2542local $h{'foo'}[0] = 1;
2543####
2544# multideref and exists
2545my(%h, $i);
2546my $e = exists $h{'foo'}[$i];
2547####
2548# multideref and delete
2549my(%h, $i);
2550my $e = delete $h{'foo'}[$i];
2551####
2552# multideref with leading expression
2553my $r;
9527dbdd 2554my $x = +($r // [])->{'foo'}[0];
fedf30e1
DM
2555####
2556# multideref with complex middle index
2557my(%h, $i, $j, $k);
2558my $x = $h{'foo'}[$i + $j]{$k};
0175f038 2559####
9527dbdd
DM
2560# multideref with trailing non-simple index that initially looks simple
2561# (i.e. the constant "3")
2562my($r, $i, $j, $k);
2563my $x = +($r || {})->{'foo'}[$i + $j]{3 + $k};
2564####
3c4a43a5
FC
2565# chdir
2566chdir 'file';
0175f038 2567chdir FH;
3c4a43a5 2568chdir;
27f31adf
FC
2569####
2570# 5.22 bitops
2571# CONTEXT use feature "bitwise"; no warnings "experimental::bitwise";
2572$_ = $_ | $_;
2573$_ = $_ & $_;
2574$_ = $_ ^ $_;
2575$_ = ~$_;
2576$_ = $_ |. $_;
2577$_ = $_ &. $_;
2578$_ = $_ ^. $_;
2579$_ = ~.$_;
2580$_ |= $_;
2581$_ &= $_;
2582$_ ^= $_;
2583$_ |.= $_;
2584$_ &.= $_;
2585$_ ^.= $_;
60f638af
DM
2586####
2587####
2588# Should really use 'no warnings "experimental::signatures"',
2589# but it doesn't yet deparse correctly.
2590# anon subs used because this test framework doesn't deparse named subs
2591# in the DATA code snippets.
2592#
2593# general signature
2594no warnings;
2595use feature 'signatures';
2596my $x;
2597sub ($a, $, $b = $glo::bal, $c = $a, $d = 'foo', $e = -37, $f = 0, $g = 1, $h = undef, $i = $a + 1, $j = /foo/, @) {
2598 $x++;
2599}
2600;
2601$x++;
2602####
2603# Signature and prototype
2604no warnings;
2605use feature 'signatures';
2606my $x;
894f226e 2607my $f = sub : prototype($$) ($a, $b) {
60f638af
DM
2608 $x++;
2609}
2610;
2611$x++;
2612####
2613# Signature and prototype and attrs
2614no warnings;
2615use feature 'signatures';
2616my $x;
894f226e 2617my $f = sub : prototype($$) lvalue ($a, $b) {
60f638af
DM
2618 $x++;
2619}
2620;
2621$x++;
2622####
2623# Signature and attrs
2624no warnings;
2625use feature 'signatures';
2626my $x;
894f226e 2627my $f = sub : lvalue method ($a, $b) {
60f638af
DM
2628 $x++;
2629}
2630;
2631$x++;
2632####
2633# named array slurp, null body
2634no warnings;
2635use feature 'signatures';
2636sub (@a) {
2637 ;
2638}
2639;
2640####
2641# named hash slurp
2642no warnings;
2643use feature 'signatures';
2644sub ($key, %h) {
2645 $h{$key};
2646}
2647;
2648####
2649# anon hash slurp
2650no warnings;
2651use feature 'signatures';
2652sub ($a, %) {
2653 $a;
2654}
2655;
2656####
2657# parenthesised default arg
2658no warnings;
2659use feature 'signatures';
2660sub ($a, $b = (/foo/), $c = 1) {
2661 $a + $b + $c;
2662}
2663;
2664####
2665# parenthesised default arg with TARGMY
2666no warnings;
2667use feature 'signatures';
2668sub ($a, $b = ($a + 1), $c = 1) {
2669 $a + $b + $c;
2670}
2671;
2672####
2673# empty default
2674no warnings;
2675use feature 'signatures';
2676sub ($a, $=) {
2677 $a;
2678}
2679;
b814db67
DM
2680####
2681# padrange op within pattern code blocks
2682/(?{ my($x, $y) = (); })/;
2683my $a;
2684/$a(?{ my($x, $y) = (); })/;
2685my $r1 = qr/(?{ my($x, $y) = (); })/;
2686my $r2 = qr/$a(?{ my($x, $y) = (); })/;
4d7ac81a
DM
2687####
2688# don't remove pattern whitespace escapes
2689/a\ b/;
2690/a\ b/x;
2691/a\ b/;
2692/a\ b/x;
82ab48fa
DM
2693####
2694# my attributes
2695my $s1 :foo(f1, f2) bar(b1, b2);
2696my @a1 :foo(f1, f2) bar(b1, b2);
2697my %h1 :foo(f1, f2) bar(b1, b2);
2698my($s2, @a2, %h2) :foo(f1, f2) bar(b1, b2);
2699####
2700# my class attributes
2701package Foo::Bar;
2702my Foo::Bar $s1 :foo(f1, f2) bar(b1, b2);
2703my Foo::Bar @a1 :foo(f1, f2) bar(b1, b2);
2704my Foo::Bar %h1 :foo(f1, f2) bar(b1, b2);
2705my Foo::Bar ($s2, @a2, %h2) :foo(f1, f2) bar(b1, b2);
2706package main;
2707my Foo::Bar $s3 :foo(f1, f2) bar(b1, b2);
2708my Foo::Bar @a3 :foo(f1, f2) bar(b1, b2);
2709my Foo::Bar %h3 :foo(f1, f2) bar(b1, b2);
2710my Foo::Bar ($s4, @a4, %h4) :foo(f1, f2) bar(b1, b2);
2711####
2712# avoid false positives in my $x :attribute
2713'attributes'->import('main', \my $x1, 'foo(bar)'), my $y1;
2714'attributes'->import('Fooo', \my $x2, 'foo(bar)'), my $y2;
ac1e5644
DM
2715####
2716# hash slices and hash key/value slices
2717my(@a, %h);
2718our(@oa, %oh);
2719@a = @h{'foo', 'bar'};
2720@a = %h{'foo', 'bar'};
2721@a = delete @h{'foo', 'bar'};
2722@a = delete %h{'foo', 'bar'};
2723@oa = @oh{'foo', 'bar'};
2724@oa = %oh{'foo', 'bar'};
2725@oa = delete @oh{'foo', 'bar'};
2726@oa = delete %oh{'foo', 'bar'};
748f2c65
DM
2727####
2728# keys optimised away in void and scalar context
2729no warnings;
2730;
2731our %h1;
2732my($x, %h2);
2733%h1;
2734keys %h1;
2735$x = %h1;
2736$x = keys %h1;
2737%h2;
2738keys %h2;
2739$x = %h2;
2740$x = keys %h2;
7e8d786b
DM
2741####
2742# eq,const optimised away for (index() == -1)
2743my($a, $b);
2744our $c;
2745$c = index($a, $b) == 2;
2746$c = rindex($a, $b) == 2;
2747$c = index($a, $b) == -1;
2748$c = rindex($a, $b) == -1;
2749$c = index($a, $b) != -1;
2750$c = rindex($a, $b) != -1;
2751$c = (index($a, $b) == -1);
2752$c = (rindex($a, $b) == -1);
2753$c = (index($a, $b) != -1);
2754$c = (rindex($a, $b) != -1);
2755####
2756# eq,const,sassign,madmy optimised away for (index() == -1)
2757my($a, $b);
2758my $c;
2759$c = index($a, $b) == 2;
2760$c = rindex($a, $b) == 2;
2761$c = index($a, $b) == -1;
2762$c = rindex($a, $b) == -1;
2763$c = index($a, $b) != -1;
2764$c = rindex($a, $b) != -1;
2765$c = (index($a, $b) == -1);
2766$c = (rindex($a, $b) == -1);
2767$c = (index($a, $b) != -1);
2768$c = (rindex($a, $b) != -1);
e839e6ed
DM
2769####
2770# plain multiconcat
2771my($a, $b, $c, $d, @a);
2772$d = length $a . $b . $c;
2773$d = length($a) . $b . $c;
2774print '' . $a;
2775push @a, ($a . '') * $b;
2776unshift @a, "$a" * ($b . '');
2777print $a . 'x' . $b . $c;
2778print $a . 'x' . $b . $c, $d;
2779print $b . $c . ($a . $b);
2780print $b . $c . ($a . $b);
2781print $b . $c . @a;
2782print $a . "\x{100}";
2783####
2784# double-quoted multiconcat
2785my($a, $b, $c, $d, @a);
2786print "${a}x\x{100}$b$c";
2787print "$a\Q$b\E$c\Ua$a\E\Lb$b\uc$c\E$a${b}c$c";
2788print "A=$a[length 'b' . $c . 'd'] b=$b";
2789print "A=@a B=$b";
2790print "\x{101}$a\x{100}";
2791$a = qr/\Q
2792$b $c
2793\x80
2794\x{100}
2795\E$c
2796/;
2797####
2798# sprintf multiconcat
2799my($a, $b, $c, $d, @a);
2800print sprintf("%s%s%%%sx%s\x{100}%s", $a, $b, $c, scalar @a, $d);
2801####
2802# multiconcat with lexical assign
2803my($a, $b, $c, $d, $e, @a);
2804$d = 'foo' . $a;
2805$d = "foo$a";
2806$d = $a . '';
2807$d = 'foo' . $a . 'bar';
2808$d = $a . $b;
2809$d = $a . $b . $c;
2810$d = $a . $b . $c . @a;
2811$e = ($d = $a . $b . $c);
2812$d = !$a . $b . $c;
2813$a = $b . $c . ($a . $b);
2814$e = f($d = !$a . $b) . $c;
2815$d = "${a}x\x{100}$b$c";
2816f($d = !$a . $b . $c);
2817####
2818# multiconcat with lexical my
2819my($a, $b, $c, $d, $e, @a);
2820my $d1 = 'foo' . $a;
2821my $d2 = "foo$a";
2822my $d3 = $a . '';
2823my $d4 = 'foo' . $a . 'bar';
2824my $d5 = $a . $b;
2825my $d6 = $a . $b . $c;
2826my $e7 = ($d = $a . $b . $c);
2827my $d8 = !$a . $b . $c;
2828my $d9 = $b . $c . ($a . $b);
2829my $da = f($d = !$a . $b) . $c;
2830my $dc = "${a}x\x{100}$b$c";
2831f(my $db = !$a . $b . $c);
2832my $dd = $a . $b . $c . @a;
2833####
2834# multiconcat with lexical append
2835my($a, $b, $c, $d, $e, @a);
2836$d .= '';
2837$d .= $a;
2838$d .= "$a";
2839$d .= 'foo' . $a;
2840$d .= "foo$a";
2841$d .= $a . '';
2842$d .= 'foo' . $a . 'bar';
2843$d .= $a . $b;
2844$d .= $a . $b . $c;
2845$d .= $a . $b . @a;
2846$e .= ($d = $a . $b . $c);
2847$d .= !$a . $b . $c;
2848$a .= $b . $c . ($a . $b);
2849$e .= f($d .= !$a . $b) . $c;
2850f($d .= !$a . $b . $c);
2851$d .= "${a}x\x{100}$b$c";
2852####
2853# multiconcat with expression assign
2854my($a, $b, $c, @a);
2855our($d, $e);
2856$d = 'foo' . $a;
2857$d = "foo$a";
2858$d = $a . '';
2859$d = 'foo' . $a . 'bar';
2860$d = $a . $b;
2861$d = $a . $b . $c;
2862$d = $a . $b . @a;
2863$e = ($d = $a . $b . $c);
2864$a["-$b-"] = !$a . $b . $c;
2865$a[$b]{$c}{$d ? $a : $b . $c} = !$a . $b . $c;
2866$a = $b . $c . ($a . $b);
2867$e = f($d = !$a . $b) . $c;
2868$d = "${a}x\x{100}$b$c";
2869f($d = !$a . $b . $c);
2870####
2871# multiconcat with expression concat
2872my($a, $b, $c, @a);
2873our($d, $e);
2874$d .= 'foo' . $a;
2875$d .= "foo$a";
2876$d .= $a . '';
2877$d .= 'foo' . $a . 'bar';
2878$d .= $a . $b;
2879$d .= $a . $b . $c;
2880$d .= $a . $b . @a;
2881$e .= ($d .= $a . $b . $c);
2882$a["-$b-"] .= !$a . $b . $c;
2883$a[$b]{$c}{$d ? $a : $b . $c} .= !$a . $b . $c;
2884$a .= $b . $c . ($a . $b);
2885$e .= f($d .= !$a . $b) . $c;
2886$d .= "${a}x\x{100}$b$c";
2887f($d .= !$a . $b . $c);
2888####
2889# multiconcat with CORE::sprintf
2890# CONTEXT sub sprintf {}
2891my($a, $b);
2892my $x = CORE::sprintf('%s%s', $a, $b);
2893####
2894# multiconcat with backticks
2895my($a, $b);
2896our $x;
2897$x = `$a-$b`;
2898####
2899# multiconcat within qr//
2900my($r, $a, $b);
2901$r = qr/abc\Q$a-$b\Exyz/;
b4fb15a6
JL
2902####
2903# tr with unprintable characters
2904my $str;
2905$str = 'foo';
2906$str =~ tr/\cA//;
e923f3dc
Z
2907####
2908# CORE::foo special case in bareword parsing
dd666160
Z
2909print $CORE::foo, $CORE::foo::bar;
2910print @CORE::foo, @CORE::foo::bar;
2911print %CORE::foo, %CORE::foo::bar;
2912print $CORE::foo{'a'}, $CORE::foo::bar{'a'};
2913print &CORE::foo, &CORE::foo::bar;
2914print &CORE::foo(), &CORE::foo::bar();
257296eb 2915print \&CORE::foo, \&CORE::foo::bar;
dd666160
Z
2916print *CORE::foo, *CORE::foo::bar;
2917print stat CORE::foo::, stat CORE::foo::bar;
2918print CORE::foo:: 1;
2919print CORE::foo::bar 2;
2920####
2921# trailing colons on glob names
2922no strict 'vars';
2923$Foo::::baz = 1;
2924print $foo, $foo::, $foo::::;
2925print @foo, @foo::, @foo::::;
2926print %foo, %foo::, %foo::::;
2927print $foo{'a'}, $foo::{'a'}, $foo::::{'a'};
2928print &foo, &foo::, &foo::::;
2929print &foo(), &foo::(), &foo::::();
257296eb 2930print \&foo, \&foo::, \&foo::::;
dd666160
Z
2931print *foo, *foo::, *foo::::;
2932print stat Foo, stat Foo::::;
2933print Foo 1;
2934print Foo:::: 2;
2935####
2936# trailing colons mixed with CORE
2937no strict 'vars';
2938print $CORE, $CORE::, $CORE::::;
2939print @CORE, @CORE::, @CORE::::;
2940print %CORE, %CORE::, %CORE::::;
2941print $CORE{'a'}, $CORE::{'a'}, $CORE::::{'a'};
2942print &CORE, &CORE::, &CORE::::;
2943print &CORE(), &CORE::(), &CORE::::();
257296eb 2944print \&CORE, \&CORE::, \&CORE::::;
dd666160
Z
2945print *CORE, *CORE::, *CORE::::;
2946print stat CORE, stat CORE::::;
2947print CORE 1;
2948print CORE:::: 2;
2949print $CORE::foo, $CORE::foo::, $CORE::foo::::;
2950print @CORE::foo, @CORE::foo::, @CORE::foo::::;
2951print %CORE::foo, %CORE::foo::, %CORE::foo::::;
2952print $CORE::foo{'a'}, $CORE::foo::{'a'}, $CORE::foo::::{'a'};
2953print &CORE::foo, &CORE::foo::, &CORE::foo::::;
2954print &CORE::foo(), &CORE::foo::(), &CORE::foo::::();
257296eb 2955print \&CORE::foo, \&CORE::foo::, \&CORE::foo::::;
dd666160
Z
2956print *CORE::foo, *CORE::foo::, *CORE::foo::::;
2957print stat CORE::foo::, stat CORE::foo::::;
2958print CORE::foo:: 1;
2959print CORE::foo:::: 2;
257296eb
Z
2960####
2961# \&foo
2962my sub foo {
2963 1;
2964}
2965no strict 'vars';
2966print \&main::foo;
2967print \&{foo};
2968print \&bar;
2969use strict 'vars';
2970print \&main::foo;
2971print \&{foo};
2972print \&main::bar;
2973####
2974# exists(&foo)
2975my sub foo {
2976 1;
2977}
2978no strict 'vars';
2979print exists &main::foo;
2980print exists &{foo};
2981print exists &bar;
2982use strict 'vars';
2983print exists &main::foo;
2984print exists &{foo};
2985print exists &main::bar;
ed34dca8
DM
2986# precedence of optimised-away 'keys' (OPpPADHV_ISKEYS/OPpRV2HV_ISKEYS)
2987my($r1, %h1, $res);
2988our($r2, %h2);
2989$res = keys %h1;
2990$res = keys %h2;
2991$res = keys %$r1;
2992$res = keys %$r2;
2993$res = keys(%h1) / 2 - 1;
2994$res = keys(%h2) / 2 - 1;
2995$res = keys(%$r1) / 2 - 1;
2996$res = keys(%$r2) / 2 - 1;
2997####
2998# ditto in presence of sub keys {}
2999# CONTEXT sub keys {}
3000no warnings;
3001my($r1, %h1, $res);
3002our($r2, %h2);
3003CORE::keys %h1;
3004CORE::keys(%h1) / 2;
3005$res = CORE::keys %h1;
3006$res = CORE::keys %h2;
3007$res = CORE::keys %$r1;
3008$res = CORE::keys %$r2;
3009$res = CORE::keys(%h1) / 2 - 1;
3010$res = CORE::keys(%h2) / 2 - 1;
3011$res = CORE::keys(%$r1) / 2 - 1;
3012$res = CORE::keys(%$r2) / 2 - 1;
4b75096e
DM
3013####
3014# concat: STACKED: ambiguity between .= and optimised nested
3015my($a, $b);
3016$b = $a . $a . $a;
3017(($a .= $a) .= $a) .= $a;
b2362f9c
DM
3018####
3019# multiconcat: $$ within string
3020my($a, $x);
3021$x = "${$}abc";
3022$x = "\$$a";
c4874d8a
DM
3023####
3024# single state aggregate assignment
3025# CONTEXT use feature "state";
3026state @a = (1, 2, 3);
3027state %h = ('a', 1, 'b', 2);
4a4aa6e0
DM
3028####
3029# state var with attribute
3030# CONTEXT use feature "state";
3031state $x :shared;
3032state $y :shared = 1;
3033state @a :shared;
3034state @b :shared = (1, 2);
3035state %h :shared;
3036state %i :shared = ('a', 1, 'b', 2);
d2a5d473
DM
3037####
3038# \our @a shouldn't be a list
3039my $r = \our @a;
3040my(@l) = \our((@b));
3041@l = \our(@c, @d);
89a395f4
DM
3042####
3043# postfix $#
3044our(@b, $s, $l);
3045$l = (\my @a)->$#*;
3046(\@b)->$#* = 1;
3047++(\my @c)->$#*;
3048$l = $#a;
3049$#a = 1;
3050$l = $#b;
3051$#b = 1;
3052my $r;
3053$l = $r->$#*;
3054$r->$#* = 1;
3055$l = $#{@$r;};
3056$#{$r;} = 1;
3057$l = $s->$#*;
3058$s->$#* = 1;
3059$l = $#{@$s;};
3060$#{$s;} = 1;
a601b139
DM
3061####
3062# TODO doesn't preserve backslash
3063my @a;
3064my $s = "$a[0]\[1]";