This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Deparse: don't parenthesise state @a = ...
[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
FR
1041####
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';
507a68aa 1044# implicit smartmatch in given/when
9210de83
FR
1045given ('foo') {
1046 when ('bar') { continue; }
1047 when ($_ ~~ 'quux') { continue; }
1048 default { 0; }
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####
1441# all the flags (tr///)
1442tr/X/Y/c;
1443tr/X//d;
1444tr/X//s;
1445tr/X//r;
1446####
9f125c4a
FC
1447# [perl #119807] s//\(3)/ge should not warn when deparsed (\3 warns)
1448s/foo/\(3);/eg;
1449####
c9fa6ae9 1450# [perl #115256]
c9fa6ae9
FC
1451"" =~ /a(?{ print q|
1452|})/;
1453>>>>
f6b6ee63 1454'' =~ /a(?{ print "\n"; })/;
c9fa6ae9
FC
1455####
1456# [perl #123217]
c9fa6ae9
FC
1457$_ = qr/(??{<<END})/
1458f.o
1459b.r
1460END
1461>>>>
1462$_ = qr/(??{ "f.o\nb.r\n"; })/;
1463####
1464# More regexp code block madness
c9fa6ae9
FC
1465my($b, @a);
1466/(?{ die $b; })/;
1467/a(?{ die $b; })a/;
1468/$a(?{ die $b; })/;
1469/@a(?{ die $b; })/;
1470/(??{ die $b; })/;
1471/a(??{ die $b; })a/;
1472/$a(??{ die $b; })/;
1473/@a(??{ die $b; })/;
1474qr/(?{ die $b; })/;
1475qr/a(?{ die $b; })a/;
1476qr/$a(?{ die $b; })/;
1477qr/@a(?{ die $b; })/;
1478qr/(??{ die $b; })/;
1479qr/a(??{ die $b; })a/;
1480qr/$a(??{ die $b; })/;
1481qr/@a(??{ die $b; })/;
1482s/(?{ die $b; })//;
1483s/a(?{ die $b; })a//;
1484s/$a(?{ die $b; })//;
1485s/@a(?{ die $b; })//;
1486s/(??{ die $b; })//;
1487s/a(??{ die $b; })a//;
1488s/$a(??{ die $b; })//;
1489s/@a(??{ die $b; })//;
1490####
ba0372a0
FC
1491# /(?x)<newline><tab>/
1492/(?x)
1493 /;
1494####
e7afc405 1495# y///r
d52196e1 1496tr/a/b/r + $a =~ tr/p/q/r;
cb8157e3 1497####
12cea2fa
FC
1498# y///d in list [perl #119815]
1499() = tr/a//d;
1500####
cb8578ff 1501# [perl #90898]
f4002a4b 1502<a,>;
18371617
LM
1503glob 'a,';
1504>>>>
1505glob 'a,';
1506glob 'a,';
09dcfa7d
FC
1507####
1508# [perl #91008]
26230909 1509# SKIP ?$] >= 5.023 && "autoderef deleted in this Perl version"
d401967c 1510# CONTEXT no warnings 'experimental::autoderef';
09dcfa7d
FC
1511each $@;
1512keys $~;
1513values $!;
5d8c42c2
FC
1514####
1515# readpipe with complex expression
1516readpipe $a + $b;
93bad3fd
NC
1517####
1518# aelemfast
1519$b::a[0] = 1;
1520####
1521# aelemfast for a lexical
1522my @a;
1523$a[0] = 1;
80e3f4ad
FC
1524####
1525# feature features without feature
0f539b13 1526# CONTEXT no warnings 'experimental::smartmatch';
80e3f4ad 1527CORE::state $x;
223b1722
FC
1528CORE::say $x;
1529CORE::given ($x) {
1530 CORE::when (3) {
1531 continue;
1532 }
1533 CORE::default {
1534 CORE::break;
1535 }
1536}
1537CORE::evalbytes '';
1538() = CORE::__SUB__;
838f2281 1539() = CORE::fc $x;
223b1722
FC
1540####
1541# feature features when feature has been disabled by use VERSION
0f539b13 1542# CONTEXT no warnings 'experimental::smartmatch';
223b1722
FC
1543use feature (sprintf(":%vd", $^V));
1544use 1;
412989c2 1545CORE::say $_;
223b1722 1546CORE::state $x;
223b1722
FC
1547CORE::given ($x) {
1548 CORE::when (3) {
1549 continue;
1550 }
1551 CORE::default {
1552 CORE::break;
1553 }
1554}
1555CORE::evalbytes '';
1556() = CORE::__SUB__;
1557>>>>
412989c2 1558CORE::say $_;
205fef88 1559CORE::state $x;
205fef88
NC
1560CORE::given ($x) {
1561 CORE::when (3) {
1562 continue;
1563 }
1564 CORE::default {
1565 CORE::break;
1566 }
1567}
1568CORE::evalbytes '';
1569() = CORE::__SUB__;
1570####
1571# (the above test with CONTEXT, and the output is equivalent but different)
0f539b13 1572# CONTEXT use feature ':5.10'; no warnings 'experimental::smartmatch';
205fef88
NC
1573# feature features when feature has been disabled by use VERSION
1574use feature (sprintf(":%vd", $^V));
1575use 1;
412989c2 1576CORE::say $_;
205fef88 1577CORE::state $x;
205fef88
NC
1578CORE::given ($x) {
1579 CORE::when (3) {
1580 continue;
1581 }
1582 CORE::default {
1583 CORE::break;
1584 }
1585}
1586CORE::evalbytes '';
1587() = CORE::__SUB__;
1588>>>>
127ce1cd 1589no feature ':all';
0bb01b05 1590use feature ':default';
412989c2 1591CORE::say $_;
223b1722 1592CORE::state $x;
80e3f4ad
FC
1593CORE::given ($x) {
1594 CORE::when (3) {
1595 continue;
1596 }
1597 CORE::default {
e36901c8 1598 CORE::break;
80e3f4ad
FC
1599 }
1600}
7d789282 1601CORE::evalbytes '';
84ed0108 1602() = CORE::__SUB__;
6ec73527 1603####
7741ceed
FC
1604# SKIP ?$] < 5.017004 && "lexical subs not implemented on this Perl version"
1605# lexical subroutines and keywords of the same name
1606# CONTEXT use feature 'lexical_subs', 'switch'; no warnings 'experimental';
1607my sub default;
1608my sub else;
1609my sub elsif;
1610my sub for;
1611my sub foreach;
1612my sub given;
1613my sub if;
1614my sub m;
1615my sub no;
1616my sub package;
1617my sub q;
1618my sub qq;
1619my sub qr;
1620my sub qx;
1621my sub require;
1622my sub s;
1623my sub sub;
1624my sub tr;
1625my sub unless;
1626my sub until;
1627my sub use;
1628my sub when;
1629my sub while;
1630CORE::default { die; }
1631CORE::if ($1) { die; }
1632CORE::if ($1) { die; }
1633CORE::elsif ($1) { die; }
1634CORE::else { die; }
1635CORE::for (die; $1; die) { die; }
1636CORE::foreach $_ (1 .. 10) { die; }
1637die CORE::foreach (1);
1638CORE::given ($1) { die; }
1639CORE::m[/];
1640CORE::m?/?;
1641CORE::package foo;
1642CORE::no strict;
1643() = (CORE::q['], CORE::qq["$_], CORE::qr//, CORE::qx[`]);
1644CORE::require 1;
1645CORE::s///;
1646() = CORE::sub { die; } ;
1647CORE::tr///;
1648CORE::unless ($1) { die; }
1649CORE::until ($1) { die; }
1650die CORE::until $1;
1651CORE::use strict;
1652CORE::when ($1 ~~ $2) { die; }
1653CORE::while ($1) { die; }
1654die CORE::while $1;
1655####
0bb01b05
FC
1656# Feature hints
1657use feature 'current_sub', 'evalbytes';
1658print;
1659use 1;
1660print;
1661use 5.014;
1662print;
1663no feature 'unicode_strings';
1664print;
1665>>>>
a8095af7 1666use feature 'current_sub', 'evalbytes';
0bb01b05 1667print $_;
127ce1cd 1668no feature ':all';
0bb01b05
FC
1669use feature ':default';
1670print $_;
127ce1cd 1671no feature ':all';
0bb01b05
FC
1672use feature ':5.12';
1673print $_;
a8095af7 1674no feature 'unicode_strings';
0bb01b05
FC
1675print $_;
1676####
6ec73527
FC
1677# $#- $#+ $#{%} etc.
1678my @x;
5b6da579 1679@x = ($#{`}, $#{~}, $#{!}, $#{@}, $#{$}, $#{%}, $#{^}, $#{&}, $#{*});
6ec73527
FC
1680@x = ($#{(}, $#{)}, $#{[}, $#{{}, $#{]}, $#{}}, $#{'}, $#{"}, $#{,});
1681@x = ($#{<}, $#{.}, $#{>}, $#{/}, $#{?}, $#{=}, $#+, $#{\}, $#{|}, $#-);
b6bba886 1682@x = ($#{;}, $#{:}, $#{1}), $#_;
61154ac0 1683####
ff683671
NC
1684# ${#} interpolated
1685# It's a known TODO that warnings are deparsed as bits, not textually.
1686no warnings;
61154ac0 1687() = "${#}a";
958ed56b 1688####
337d7381
FC
1689# [perl #86060] $( $| $) in regexps need braces
1690/${(}/;
1691/${|}/;
1692/${)}/;
1693/${(}${|}${)}/;
3f193e55 1694/@{+}@{-}/;
337d7381 1695####
958ed56b
FC
1696# ()[...]
1697my(@a) = ()[()];
521795fe
FC
1698####
1699# sort(foo(bar))
1700# sort(foo(bar)) is interpreted as sort &foo(bar)
1701# sort foo(bar) is interpreted as sort foo bar
1702# parentheses are not optional in this case
1703print sort(foo('bar'));
1704>>>>
1705print sort(foo('bar'));
24fcb59f
FC
1706####
1707# substr assignment
1708substr(my $a, 0, 0) = (foo(), bar());
1709$a++;
04be0204 1710####
d1718a7c
FC
1711# This following line works around an unfixed bug that we are not trying to
1712# test for here:
1713# CONTEXT BEGIN { $^H{a} = "b"; delete $^H{a} } # make %^H localised
04be0204
FC
1714# hint hash
1715BEGIN { $^H{'foo'} = undef; }
1716{
1717 BEGIN { $^H{'bar'} = undef; }
1718 {
1719 BEGIN { $^H{'baz'} = undef; }
1720 {
1721 print $_;
1722 }
1723 print $_;
1724 }
1725 print $_;
1726}
035146a3
FC
1727BEGIN { $^H{q[']} = '('; }
1728print $_;
c306e834 1729####
d1718a7c
FC
1730# This following line works around an unfixed bug that we are not trying to
1731# test for here:
1732# CONTEXT BEGIN { $^H{a} = "b"; delete $^H{a} } # make %^H localised
c306e834
FC
1733# hint hash changes that serialise the same way with sort %hh
1734BEGIN { $^H{'a'} = 'b'; }
1735{
1736 BEGIN { $^H{'b'} = 'a'; delete $^H{'a'}; }
1737 print $_;
1738}
1739print $_;
94bb57f9
FC
1740####
1741# [perl #47361] do({}) and do +{} (variants of do-file)
1742do({});
1743do +{};
8b46c09b
FC
1744sub foo::do {}
1745package foo;
1746CORE::do({});
1747CORE::do +{};
94bb57f9
FC
1748>>>>
1749do({});
1750do({});
8b46c09b
FC
1751package foo;
1752CORE::do({});
1753CORE::do({});
9c56d9ea
FC
1754####
1755# [perl #77096] functions that do not follow the llafr
1756() = (return 1) + time;
1757() = (return ($1 + $2) * $3) + time;
1758() = (return ($a xor $b)) + time;
1759() = (do 'file') + time;
1760() = (do ($1 + $2) * $3) + time;
1761() = (do ($1 xor $2)) + time;
41df74e3
FC
1762() = (goto 1) + 3;
1763() = (require 'foo') + 3;
1764() = (require foo) + 3;
266da325 1765() = (CORE::dump 1) + 3;
41df74e3
FC
1766() = (last 1) + 3;
1767() = (next 1) + 3;
1768() = (redo 1) + 3;
5830412d
FC
1769() = (-R $_) + 3;
1770() = (-W $_) + 3;
1771() = (-X $_) + 3;
1772() = (-r $_) + 3;
1773() = (-w $_) + 3;
1774() = (-x $_) + 3;
2462c3cc 1775####
917a8f4f
FC
1776# require(foo()) and do(foo())
1777require (foo());
1778do (foo());
df465735
FC
1779goto (foo());
1780CORE::dump (foo());
1781last (foo());
1782next (foo());
1783redo (foo());
917a8f4f 1784####
5e7acd25
FC
1785# require vstring
1786require v5.16;
1787####
1cabb3b3
FC
1788# [perl #97476] not() *does* follow the llafr
1789$_ = ($a xor not +($1 || 2) ** 2);
1790####
4d8ac5c7
FC
1791# Precedence conundrums with argument-less function calls
1792() = (eof) + 1;
1793() = (return) + 1;
1794() = (return, 1);
7bc8c979
FC
1795() = warn;
1796() = warn() + 1;
4d8ac5c7
FC
1797() = setpgrp() + 1;
1798####
1eb0b7be
FC
1799# loopexes have assignment prec
1800() = (CORE::dump a) | 'b';
1801() = (goto a) | 'b';
1802() = (last a) | 'b';
1803() = (next a) | 'b';
1804() = (redo a) | 'b';
1805####
2462c3cc
FC
1806# [perl #63558] open local(*FH)
1807open local *FH;
564cd6cb 1808pipe local *FH, local *FH;
843b15cc 1809####
b89b7257
FC
1810# [perl #91416] open "string"
1811open 'open';
1812open '####';
1813open '^A';
1814open "\ca";
1815>>>>
1816open *open;
1817open '####';
1818open '^A';
1819open *^A;
1820####
be6cf5cf
FC
1821# "string"->[] ->{}
1822no strict 'vars';
1823() = 'open'->[0]; #aelemfast
1824() = '####'->[0];
1825() = '^A'->[0];
1826() = "\ca"->[0];
b861b87f 1827() = 'a::]b'->[0];
10e8e32b
FC
1828() = 'open'->[$_]; #aelem
1829() = '####'->[$_];
1830() = '^A'->[$_];
1831() = "\ca"->[$_];
b861b87f 1832() = 'a::]b'->[$_];
10e8e32b
FC
1833() = 'open'->{0}; #helem
1834() = '####'->{0};
1835() = '^A'->{0};
1836() = "\ca"->{0};
b861b87f 1837() = 'a::]b'->{0};
be6cf5cf 1838>>>>
415d4c68 1839no strict 'vars';
be6cf5cf
FC
1840() = $open[0];
1841() = '####'->[0];
1842() = '^A'->[0];
1843() = $^A[0];
b861b87f 1844() = 'a::]b'->[0];
10e8e32b
FC
1845() = $open[$_];
1846() = '####'->[$_];
1847() = '^A'->[$_];
1848() = $^A[$_];
b861b87f 1849() = 'a::]b'->[$_];
10e8e32b
FC
1850() = $open{'0'};
1851() = '####'->{'0'};
1852() = '^A'->{'0'};
1853() = $^A{'0'};
b861b87f 1854() = 'a::]b'->{'0'};
be6cf5cf 1855####
843b15cc
FC
1856# [perl #74740] -(f()) vs -f()
1857$_ = -(f());
c75b4828
FC
1858####
1859# require <binop>
1860require 'a' . $1;
afb60448
HY
1861####
1862#[perl #30504] foreach-my postfix/prefix difference
1863$_ = 'foo' foreach my ($foo1, $bar1, $baz1);
1864foreach (my ($foo2, $bar2, $baz2)) { $_ = 'foo' }
1865foreach my $i (my ($foo3, $bar3, $baz3)) { $i = 'foo' }
1866>>>>
1867$_ = 'foo' foreach (my($foo1, $bar1, $baz1));
1868foreach $_ (my($foo2, $bar2, $baz2)) {
1869 $_ = 'foo';
1870}
1871foreach my $i (my($foo3, $bar3, $baz3)) {
1872 $i = 'foo';
1873}
1874####
1875#[perl #108224] foreach with continue block
1876foreach (1 .. 3) { print } continue { print "\n" }
1877foreach (1 .. 3) { } continue { }
1878foreach my $i (1 .. 3) { print $i } continue { print "\n" }
1879foreach my $i (1 .. 3) { } continue { }
1880>>>>
1881foreach $_ (1 .. 3) {
1882 print $_;
1883}
1884continue {
1885 print "\n";
1886}
1887foreach $_ (1 .. 3) {
1888 ();
1889}
1890continue {
1891 ();
1892}
1893foreach my $i (1 .. 3) {
1894 print $i;
1895}
1896continue {
1897 print "\n";
1898}
1899foreach my $i (1 .. 3) {
1900 ();
1901}
1902continue {
1903 ();
1904}
bc1cc2c3
DM
1905####
1906# file handles
1907no strict;
1908my $mfh;
1909open F;
1910open *F;
1911open $fh;
1912open $mfh;
1913open 'a+b';
1914select *F;
1915select F;
1916select $f;
1917select $mfh;
1918select 'a+b';
a7fd8ef6
DM
1919####
1920# 'my' works with padrange op
1921my($z, @z);
1922my $m1;
1923$m1 = 1;
1924$z = $m1;
1925my $m2 = 2;
1926my($m3, $m4);
1927($m3, $m4) = (1, 2);
1928@z = ($m3, $m4);
1929my($m5, $m6) = (1, 2);
1930my($m7, undef, $m8) = (1, 2, 3);
1931@z = ($m7, undef, $m8);
1932($m7, undef, $m8) = (1, 2, 3);
1933####
1934# 'our/local' works with padrange op
a7fd8ef6
DM
1935our($z, @z);
1936our $o1;
0298c760 1937no strict;
a7fd8ef6
DM
1938local $o11;
1939$o1 = 1;
1940local $o1 = 1;
1941$z = $o1;
1942$z = local $o1;
1943our $o2 = 2;
1944our($o3, $o4);
1945($o3, $o4) = (1, 2);
1946local($o3, $o4) = (1, 2);
1947@z = ($o3, $o4);
1948@z = local($o3, $o4);
1949our($o5, $o6) = (1, 2);
1950our($o7, undef, $o8) = (1, 2, 3);
1951@z = ($o7, undef, $o8);
1952@z = local($o7, undef, $o8);
1953($o7, undef, $o8) = (1, 2, 3);
1954local($o7, undef, $o8) = (1, 2, 3);
1955####
1956# 'state' works with padrange op
412989c2 1957# CONTEXT no strict; use feature 'state';
a7fd8ef6
DM
1958state($z, @z);
1959state $s1;
1960$s1 = 1;
1961$z = $s1;
1962state $s2 = 2;
1963state($s3, $s4);
1964($s3, $s4) = (1, 2);
1965@z = ($s3, $s4);
1966# assignment of state lists isn't implemented yet
1967#state($s5, $s6) = (1, 2);
1968#state($s7, undef, $s8) = (1, 2, 3);
1969#@z = ($s7, undef, $s8);
1970($s7, undef, $s8) = (1, 2, 3);
1971####
df2b00e8 1972# anon arrays with padrange
a7fd8ef6
DM
1973my($a, $b);
1974my $c = [$a, $b];
1975my $d = {$a, $b};
1976####
1977# slices with padrange
1978my($a, $b);
1979my(@x, %y);
1980@x = @x[$a, $b];
1981@x = @y{$a, $b};
1982####
1983# binops with padrange
1984my($a, $b, $c);
1985$c = $a cmp $b;
1986$c = $a + $b;
1987$a += $b;
1988$c = $a - $b;
1989$a -= $b;
1990$c = my $a1 cmp $b;
1991$c = my $a2 + $b;
1992$a += my $b1;
1993$c = my $a3 - $b;
1994$a -= my $b2;
1995####
1996# 'x' with padrange
1997my($a, $b, $c, $d, @e);
1998$c = $a x $b;
1999$a x= $b;
2000@e = ($a) x $d;
2001@e = ($a, $b) x $d;
2002@e = ($a, $b, $c) x $d;
2003@e = ($a, 1) x $d;
d5524600
DM
2004####
2005# @_ with padrange
2006my($a, $b, $c) = @_;
ce4062e7
AC
2007####
2008# SKIP ?$] < 5.017004 && "lexical subs not implemented on this Perl version"
ce4062e7 2009# lexical subroutine
db629560 2010# CONTEXT use feature 'lexical_subs';
601448c3 2011no warnings "experimental::lexical_subs";
ce4062e7
AC
2012my sub f {}
2013print f();
d4f1bfe7 2014>>>>
006c1a1d 2015BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x54\x55\x55\x55"}
d4f1bfe7 2016my sub f {
d4f1bfe7
FC
2017
2018}
d4f1bfe7 2019print f();
f0cf3754
AC
2020####
2021# SKIP ?$] < 5.017004 && "lexical subs not implemented on this Perl version"
f0cf3754 2022# lexical "state" subroutine
db629560 2023# CONTEXT use feature 'state', 'lexical_subs';
f0cf3754
AC
2024no warnings 'experimental::lexical_subs';
2025state sub f {}
2026print f();
d4f1bfe7 2027>>>>
006c1a1d 2028BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x54\x55\x55\x55"}
db629560 2029state sub f {
d4f1bfe7
FC
2030
2031}
d4f1bfe7 2032print f();
bcbe2b27 2033####
8443930e 2034# SKIP ?$] < 5.017004 && "lexical subs not implemented on this Perl version"
8443930e
FC
2035# lexical subroutine scoping
2036# CONTEXT use feature 'lexical_subs'; no warnings 'experimental::lexical_subs';
2037{
2038 {
2039 my sub a { die; }
2040 {
2041 foo();
2042 my sub b;
bb9bfaa4 2043 b ;
8443930e 2044 main::b();
d49c3562
FC
2045 &main::b;
2046 &main::b();
2047 my $b = \&main::b;
2048 sub b { $b; }
8443930e
FC
2049 }
2050 }
2051 b();
2052}
2053####
bb9bfaa4
FC
2054# self-referential lexical subroutine
2055# CONTEXT use feature 'lexical_subs', 'state'; no warnings 'experimental::lexical_subs';
2056();
2057state sub sb2;
2058sub sb2 {
2059 sb2 ;
2060}
2061####
494a4b9c
FC
2062# lexical subroutine with outer declaration and inner definition
2063# CONTEXT use feature 'lexical_subs'; no warnings 'experimental::lexical_subs';
2064();
2065my sub f;
2066my sub g {
2067 ();
2068 sub f { }
2069}
2070####
2c5ddcd3
FC
2071# TODO only partially fixed
2072# lexical state subroutine with outer declaration and inner definition
2073# CONTEXT use feature 'lexical_subs', 'state'; no warnings 'experimental::lexical_subs';
2074();
2075state sub sb4;
2076state sub a {
2077 ();
2078 sub sb4 { }
2079}
2080state sub sb5;
2081sub {
2082 ();
2083 sub sb5 { }
2084} ;
2085####
bcbe2b27
FC
2086# Elements of %# should not be confused with $#{ array }
2087() = ${#}{'foo'};
c4cf781e 2088####
cfd916dd 2089# $; [perl #123357]
ddb55548
FC
2090$_ = $;;
2091do {
2092 $;
2093};
2094####
c65b7c4d
FC
2095# Ampersand calls and scalar context
2096# OPTIONS -P
2097package prototest;
2098sub foo($$);
2099foo(bar(),baz());
2100>>>>
2101package prototest;
2102&foo(scalar bar(), scalar baz());
2103####
de4fa237
FC
2104# coderef2text and prototyped sub calls [perl #123435]
2105is 'foo', 'oo';
2106####
fd8be4a1
FC
2107# prototypes with unary precedence
2108package prototest;
2109sub dollar($) {}
2110sub optdollar(;$) {}
2111sub optoptdollar(;;$) {}
2112sub splat(*) {}
2113sub optsplat(;*) {}
2114sub optoptsplat(;;*) {}
2115sub bar(_) {}
2116sub optbar(;_) {}
2117sub optoptbar(;;_) {}
2118sub plus(+) {}
2119sub optplus(;+) {}
2120sub optoptplus(;;+) {}
2121sub wack(\$) {}
2122sub optwack(;\$) {}
2123sub optoptwack(;;\$) {}
2124sub wackbrack(\[$]) {}
2125sub optwackbrack(;\[$]) {}
2126sub optoptwackbrack(;;\[$]) {}
2127dollar($a < $b);
2128optdollar($a < $b);
2129optoptdollar($a < $b);
2130splat($a < $b); # Some of these deparse with ‘&’; if that changes, just
2131optsplat($a < $b); # change the tests.
2132optoptsplat($a < $b);
2133bar($a < $b);
2134optbar($a < $b);
2135optoptbar($a < $b);
2136plus($a < $b);
2137optplus($a < $b);
2138optoptplus($a < $b);
2139wack($a = $b);
2140optwack($a = $b);
2141optoptwack($a = $b);
2142wackbrack($a = $b);
2143optwackbrack($a = $b);
2144optoptwackbrack($a = $b);
2145>>>>
2146package prototest;
2147dollar($a < $b);
2148optdollar($a < $b);
2149optoptdollar($a < $b);
2150&splat($a < $b);
2151&optsplat($a < $b);
2152&optoptsplat($a < $b);
2153bar($a < $b);
2154optbar($a < $b);
2155optoptbar($a < $b);
2156&plus($a < $b);
2157&optplus($a < $b);
2158&optoptplus($a < $b);
2159&wack(\($a = $b));
2160&optwack(\($a = $b));
2161&optoptwack(\($a = $b));
2162&wackbrack(\($a = $b));
2163&optwackbrack(\($a = $b));
2164&optoptwackbrack(\($a = $b));
2165####
b024352e
DM
2166# ensure aelemfast works in the range -128..127 and that there's no
2167# funky edge cases
2168my $x;
2169no strict 'vars';
2170$x = $a[-256] + $a[-255] + $a[-129] + $a[-128] + $a[-127] + $a[-1] + $a[0];
2171$x = $a[1] + $a[126] + $a[127] + $a[128] + $a[255] + $a[256];
2172my @b;
2173$x = $b[-256] + $b[-255] + $b[-129] + $b[-128] + $b[-127] + $b[-1] + $b[0];
2174$x = $b[1] + $b[126] + $b[127] + $b[128] + $b[255] + $b[256];
e09d73a6
DIM
2175####
2176# 'm' must be preserved in m??
2177m??;
c8ec376c
FC
2178####
2179# \(@array) and \(..., (@array), ...)
2180my(@array, %hash, @a, @b, %c, %d);
2181() = \(@array);
2182() = \(%hash);
2183() = \(@a, (@b), (%c), %d);
2184() = \(@Foo::array);
2185() = \(%Foo::hash);
2186() = \(@Foo::a, (@Foo::b), (%Foo::c), %Foo::d);
a958cfbb
FC
2187####
2188# subs synonymous with keywords
2189main::our();
2190main::pop();
2191state();
2192use feature 'state';
2193main::state();
9187b6e4
FC
2194####
2195# lvalue references
baabe3fb 2196# CONTEXT use feature "state", 'refaliasing', 'lexical_subs'; no warnings 'experimental';
9187b6e4
FC
2197our $x;
2198\$x = \$x;
2199my $m;
2200\$m = \$x;
2201\my $n = \$x;
2202(\$x) = @_;
2203\($x) = @_;
2204\($m) = @_;
2205(\$m) = @_;
2206\my($p) = @_;
2207(\my $r) = @_;
2208\($x, my $a) = @{[\$x, \$x]};
2209(\$x, \my $b) = @{[\$x, \$x]};
2210\local $x = \3;
2211\local($x) = \3;
2212\state $c = \3;
2213\state($d) = \3;
2214\our $e = \3;
2215\our($f) = \3;
2216\$_[0] = foo();
2217\($_[1]) = foo();
2218my @a;
2219\$a[0] = foo();
2220\($a[1]) = foo();
2221\local($a[1]) = foo();
2222\@a[0,1] = foo();
2223\(@a[2,3]) = foo();
2224\local @a[0,1] = (\$a)x2;
2225\$_{a} = foo();
2226\($_{b}) = foo();
2227my %h;
2228\$h{a} = foo();
2229\($h{b}) = foo();
2230\local $h{a} = \$x;
2231\local($h{b}) = \$x;
2232\@h{'a','b'} = foo();
2233\(@h{2,3}) = foo();
2234\local @h{'a','b'} = (\$x)x2;
2235\@_ = foo();
2236\@a = foo();
2237(\@_) = foo();
2238(\@a) = foo();
2239\my @c = foo();
2240(\my @d) = foo();
2241\(@_) = foo();
2242\(@a) = foo();
2243\my(@g) = foo();
2244\local @_ = \@_;
2245(\local @_) = \@_;
2246\state @e = [1..3];
2247\state(@f) = \3;
2248\our @i = [1..3];
2249\our(@h) = \3;
2250\%_ = foo();
2251\%h = foo();
2252(\%_) = foo();
2253(\%h) = foo();
2254\my %c = foo();
2255(\my %d) = foo();
2256\local %_ = \%h;
2257(\local %_) = \%h;
2258\state %y = {1,2};
2259\our %z = {1,2};
2260(\our %zz) = {1,2};
2261\&a = foo();
2262(\&a) = foo();
2263\(&a) = foo();
2264{
2265 my sub a;
2266 \&a = foo();
2267 (\&a) = foo();
2268 \(&a) = foo();
2269}
2270(\$_, $_) = \(1, 2);
2271$_ == 3 ? \$_ : $_ = \3;
2272$_ == 3 ? \$_ : \$x = \3;
2273\($_ == 3 ? $_ : $x) = \3;
2274for \my $topic (\$1, \$2) {
2275 die;
2276}
2277for \state $topic (\$1, \$2) {
2278 die;
2279}
2280for \our $topic (\$1, \$2) {
2281 die;
2282}
2283for \$_ (\$1, \$2) {
2284 die;
2285}
2286for \my @a ([1,2], [3,4]) {
2287 die;
2288}
2289for \state @a ([1,2], [3,4]) {
2290 die;
2291}
2292for \our @a ([1,2], [3,4]) {
2293 die;
2294}
2295for \@_ ([1,2], [3,4]) {
2296 die;
2297}
2298for \my %a ({5,6}, {7,8}) {
2299 die;
2300}
2301for \our %a ({5,6}, {7,8}) {
2302 die;
2303}
2304for \state %a ({5,6}, {7,8}) {
2305 die;
2306}
2307for \%_ ({5,6}, {7,8}) {
2308 die;
2309}
2310{
2311 my sub a;
2312 for \&a (sub { 9; }, sub { 10; }) {
2313 die;
2314 }
2315}
2316for \&a (sub { 9; }, sub { 10; }) {
2317 die;
2318}
2319>>>>
2320our $x;
2321\$x = \$x;
2322my $m;
2323\$m = \$x;
2324\my $n = \$x;
2325(\$x) = @_;
2326(\$x) = @_;
2327(\$m) = @_;
2328(\$m) = @_;
2329(\my $p) = @_;
2330(\my $r) = @_;
2331(\$x, \my $a) = @{[\$x, \$x];};
2332(\$x, \my $b) = @{[\$x, \$x];};
2333\local $x = \3;
2334(\local $x) = \3;
2335\state $c = \3;
2336(\state $d) = \3;
2337\our $e = \3;
2338(\our $f) = \3;
2339\$_[0] = foo();
2340(\$_[1]) = foo();
2341my @a;
2342\$a[0] = foo();
2343(\$a[1]) = foo();
2344(\local $a[1]) = foo();
2345(\@a[0, 1]) = foo();
2346(\@a[2, 3]) = foo();
2347(\local @a[0, 1]) = (\$a) x 2;
2348\$_{'a'} = foo();
2349(\$_{'b'}) = foo();
2350my %h;
2351\$h{'a'} = foo();
2352(\$h{'b'}) = foo();
2353\local $h{'a'} = \$x;
2354(\local $h{'b'}) = \$x;
2355(\@h{'a', 'b'}) = foo();
2356(\@h{2, 3}) = foo();
2357(\local @h{'a', 'b'}) = (\$x) x 2;
2358\@_ = foo();
2359\@a = foo();
2360(\@_) = foo();
2361(\@a) = foo();
2362\my @c = foo();
2363(\my @d) = foo();
2364(\(@_)) = foo();
2365(\(@a)) = foo();
2366(\(my @g)) = foo();
2367\local @_ = \@_;
2368(\local @_) = \@_;
2369\state @e = [1..3];
2370(\(state @f)) = \3;
2371\our @i = [1..3];
2372(\(our @h)) = \3;
2373\%_ = foo();
2374\%h = foo();
2375(\%_) = foo();
2376(\%h) = foo();
2377\my %c = foo();
2378(\my %d) = foo();
2379\local %_ = \%h;
2380(\local %_) = \%h;
2381\state %y = {1, 2};
2382\our %z = {1, 2};
2383(\our %zz) = {1, 2};
2384\&a = foo();
2385(\&a) = foo();
2386(\&a) = foo();
2387{
2388 my sub a;
2389 \&a = foo();
2390 (\&a) = foo();
2391 (\&a) = foo();
2392}
2393(\$_, $_) = \(1, 2);
2394$_ == 3 ? \$_ : $_ = \3;
2395$_ == 3 ? \$_ : \$x = \3;
2396($_ == 3 ? \$_ : \$x) = \3;
2397foreach \my $topic (\$1, \$2) {
2398 die;
2399}
2400foreach \state $topic (\$1, \$2) {
2401 die;
2402}
2403foreach \our $topic (\$1, \$2) {
2404 die;
2405}
2406foreach \$_ (\$1, \$2) {
2407 die;
2408}
2409foreach \my @a ([1, 2], [3, 4]) {
2410 die;
2411}
2412foreach \state @a ([1, 2], [3, 4]) {
2413 die;
2414}
2415foreach \our @a ([1, 2], [3, 4]) {
2416 die;
2417}
2418foreach \@_ ([1, 2], [3, 4]) {
2419 die;
2420}
2421foreach \my %a ({5, 6}, {7, 8}) {
2422 die;
2423}
2424foreach \our %a ({5, 6}, {7, 8}) {
2425 die;
2426}
2427foreach \state %a ({5, 6}, {7, 8}) {
2428 die;
2429}
2430foreach \%_ ({5, 6}, {7, 8}) {
2431 die;
2432}
2433{
2434 my sub a;
2435 foreach \&a (sub { 9; } , sub { 10; } ) {
2436 die;
2437 }
2438}
2439foreach \&a (sub { 9; } , sub { 10; } ) {
2440 die;
2441}
3b4e2a4d
FC
2442####
2443# join $foo, pos
2444my $foo;
2445$_ = join $foo, pos
2446>>>>
2447my $foo;
2448$_ = join('???', pos $_);
fedf30e1
DM
2449####
2450# exists $a[0]
2451our @a;
2452exists $a[0];
2453####
2454# my @a; exists $a[0]
2455my @a;
2456exists $a[0];
2457####
2458# delete $a[0]
2459our @a;
2460delete $a[0];
2461####
2462# my @a; delete $a[0]
2463my @a;
2464delete $a[0];
2465####
2466# $_[0][$_[1]]
2467$_[0][$_[1]];
2468####
2469# f($a[0]);
2470my @a;
2471f($a[0]);
2472####
2473#qr/\Q$h{'key'}\E/;
2474my %h;
2475qr/\Q$h{'key'}\E/;
2476####
2477# my $x = "$h{foo}";
2478my %h;
2479my $x = "$h{'foo'}";
2480####
2481# weird constant hash key
2482my %h;
2483my $x = $h{"\000\t\x{100}"};
2484####
2485# multideref and packages
2486package foo;
2487my(%bar) = ('a', 'b');
2488our(@bar) = (1, 2);
2489$bar{'k'} = $bar[200];
2490$main::bar{'k'} = $main::bar[200];
2491$foo::bar{'k'} = $foo::bar[200];
2492package foo2;
2493$bar{'k'} = $bar[200];
2494$main::bar{'k'} = $main::bar[200];
2495$foo::bar{'k'} = $foo::bar[200];
2496>>>>
2497package foo;
2498my(%bar) = ('a', 'b');
2499our(@bar) = (1, 2);
2500$bar{'k'} = $bar[200];
2501$main::bar{'k'} = $main::bar[200];
2502$foo::bar{'k'} = $bar[200];
2503package foo2;
2504$bar{'k'} = $foo::bar[200];
2505$main::bar{'k'} = $main::bar[200];
2506$foo::bar{'k'} = $foo::bar[200];
2507####
2508# multideref and local
2509my %h;
2510local $h{'foo'}[0] = 1;
2511####
2512# multideref and exists
2513my(%h, $i);
2514my $e = exists $h{'foo'}[$i];
2515####
2516# multideref and delete
2517my(%h, $i);
2518my $e = delete $h{'foo'}[$i];
2519####
2520# multideref with leading expression
2521my $r;
9527dbdd 2522my $x = +($r // [])->{'foo'}[0];
fedf30e1
DM
2523####
2524# multideref with complex middle index
2525my(%h, $i, $j, $k);
2526my $x = $h{'foo'}[$i + $j]{$k};
0175f038 2527####
9527dbdd
DM
2528# multideref with trailing non-simple index that initially looks simple
2529# (i.e. the constant "3")
2530my($r, $i, $j, $k);
2531my $x = +($r || {})->{'foo'}[$i + $j]{3 + $k};
2532####
3c4a43a5
FC
2533# chdir
2534chdir 'file';
0175f038 2535chdir FH;
3c4a43a5 2536chdir;
27f31adf
FC
2537####
2538# 5.22 bitops
2539# CONTEXT use feature "bitwise"; no warnings "experimental::bitwise";
2540$_ = $_ | $_;
2541$_ = $_ & $_;
2542$_ = $_ ^ $_;
2543$_ = ~$_;
2544$_ = $_ |. $_;
2545$_ = $_ &. $_;
2546$_ = $_ ^. $_;
2547$_ = ~.$_;
2548$_ |= $_;
2549$_ &= $_;
2550$_ ^= $_;
2551$_ |.= $_;
2552$_ &.= $_;
2553$_ ^.= $_;
60f638af
DM
2554####
2555####
2556# Should really use 'no warnings "experimental::signatures"',
2557# but it doesn't yet deparse correctly.
2558# anon subs used because this test framework doesn't deparse named subs
2559# in the DATA code snippets.
2560#
2561# general signature
2562no warnings;
2563use feature 'signatures';
2564my $x;
2565sub ($a, $, $b = $glo::bal, $c = $a, $d = 'foo', $e = -37, $f = 0, $g = 1, $h = undef, $i = $a + 1, $j = /foo/, @) {
2566 $x++;
2567}
2568;
2569$x++;
2570####
2571# Signature and prototype
2572no warnings;
2573use feature 'signatures';
2574my $x;
2575sub ($a, $b) : prototype($$) {
2576 $x++;
2577}
2578;
2579$x++;
2580####
2581# Signature and prototype and attrs
2582no warnings;
2583use feature 'signatures';
2584my $x;
2585sub ($a, $b) : prototype($$) lvalue {
2586 $x++;
2587}
2588;
2589$x++;
2590####
2591# Signature and attrs
2592no warnings;
2593use feature 'signatures';
2594my $x;
2595sub ($a, $b) : lvalue method {
2596 $x++;
2597}
2598;
2599$x++;
2600####
2601# named array slurp, null body
2602no warnings;
2603use feature 'signatures';
2604sub (@a) {
2605 ;
2606}
2607;
2608####
2609# named hash slurp
2610no warnings;
2611use feature 'signatures';
2612sub ($key, %h) {
2613 $h{$key};
2614}
2615;
2616####
2617# anon hash slurp
2618no warnings;
2619use feature 'signatures';
2620sub ($a, %) {
2621 $a;
2622}
2623;
2624####
2625# parenthesised default arg
2626no warnings;
2627use feature 'signatures';
2628sub ($a, $b = (/foo/), $c = 1) {
2629 $a + $b + $c;
2630}
2631;
2632####
2633# parenthesised default arg with TARGMY
2634no warnings;
2635use feature 'signatures';
2636sub ($a, $b = ($a + 1), $c = 1) {
2637 $a + $b + $c;
2638}
2639;
2640####
2641# empty default
2642no warnings;
2643use feature 'signatures';
2644sub ($a, $=) {
2645 $a;
2646}
2647;
b814db67
DM
2648####
2649# padrange op within pattern code blocks
2650/(?{ my($x, $y) = (); })/;
2651my $a;
2652/$a(?{ my($x, $y) = (); })/;
2653my $r1 = qr/(?{ my($x, $y) = (); })/;
2654my $r2 = qr/$a(?{ my($x, $y) = (); })/;
4d7ac81a
DM
2655####
2656# don't remove pattern whitespace escapes
2657/a\ b/;
2658/a\ b/x;
2659/a\ b/;
2660/a\ b/x;
82ab48fa
DM
2661####
2662# my attributes
2663my $s1 :foo(f1, f2) bar(b1, b2);
2664my @a1 :foo(f1, f2) bar(b1, b2);
2665my %h1 :foo(f1, f2) bar(b1, b2);
2666my($s2, @a2, %h2) :foo(f1, f2) bar(b1, b2);
2667####
2668# my class attributes
2669package Foo::Bar;
2670my Foo::Bar $s1 :foo(f1, f2) bar(b1, b2);
2671my Foo::Bar @a1 :foo(f1, f2) bar(b1, b2);
2672my Foo::Bar %h1 :foo(f1, f2) bar(b1, b2);
2673my Foo::Bar ($s2, @a2, %h2) :foo(f1, f2) bar(b1, b2);
2674package main;
2675my Foo::Bar $s3 :foo(f1, f2) bar(b1, b2);
2676my Foo::Bar @a3 :foo(f1, f2) bar(b1, b2);
2677my Foo::Bar %h3 :foo(f1, f2) bar(b1, b2);
2678my Foo::Bar ($s4, @a4, %h4) :foo(f1, f2) bar(b1, b2);
2679####
2680# avoid false positives in my $x :attribute
2681'attributes'->import('main', \my $x1, 'foo(bar)'), my $y1;
2682'attributes'->import('Fooo', \my $x2, 'foo(bar)'), my $y2;
ac1e5644
DM
2683####
2684# hash slices and hash key/value slices
2685my(@a, %h);
2686our(@oa, %oh);
2687@a = @h{'foo', 'bar'};
2688@a = %h{'foo', 'bar'};
2689@a = delete @h{'foo', 'bar'};
2690@a = delete %h{'foo', 'bar'};
2691@oa = @oh{'foo', 'bar'};
2692@oa = %oh{'foo', 'bar'};
2693@oa = delete @oh{'foo', 'bar'};
2694@oa = delete %oh{'foo', 'bar'};
748f2c65
DM
2695####
2696# keys optimised away in void and scalar context
2697no warnings;
2698;
2699our %h1;
2700my($x, %h2);
2701%h1;
2702keys %h1;
2703$x = %h1;
2704$x = keys %h1;
2705%h2;
2706keys %h2;
2707$x = %h2;
2708$x = keys %h2;
7e8d786b
DM
2709####
2710# eq,const optimised away for (index() == -1)
2711my($a, $b);
2712our $c;
2713$c = index($a, $b) == 2;
2714$c = rindex($a, $b) == 2;
2715$c = index($a, $b) == -1;
2716$c = rindex($a, $b) == -1;
2717$c = index($a, $b) != -1;
2718$c = rindex($a, $b) != -1;
2719$c = (index($a, $b) == -1);
2720$c = (rindex($a, $b) == -1);
2721$c = (index($a, $b) != -1);
2722$c = (rindex($a, $b) != -1);
2723####
2724# eq,const,sassign,madmy optimised away for (index() == -1)
2725my($a, $b);
2726my $c;
2727$c = index($a, $b) == 2;
2728$c = rindex($a, $b) == 2;
2729$c = index($a, $b) == -1;
2730$c = rindex($a, $b) == -1;
2731$c = index($a, $b) != -1;
2732$c = rindex($a, $b) != -1;
2733$c = (index($a, $b) == -1);
2734$c = (rindex($a, $b) == -1);
2735$c = (index($a, $b) != -1);
2736$c = (rindex($a, $b) != -1);
e839e6ed
DM
2737####
2738# plain multiconcat
2739my($a, $b, $c, $d, @a);
2740$d = length $a . $b . $c;
2741$d = length($a) . $b . $c;
2742print '' . $a;
2743push @a, ($a . '') * $b;
2744unshift @a, "$a" * ($b . '');
2745print $a . 'x' . $b . $c;
2746print $a . 'x' . $b . $c, $d;
2747print $b . $c . ($a . $b);
2748print $b . $c . ($a . $b);
2749print $b . $c . @a;
2750print $a . "\x{100}";
2751####
2752# double-quoted multiconcat
2753my($a, $b, $c, $d, @a);
2754print "${a}x\x{100}$b$c";
2755print "$a\Q$b\E$c\Ua$a\E\Lb$b\uc$c\E$a${b}c$c";
2756print "A=$a[length 'b' . $c . 'd'] b=$b";
2757print "A=@a B=$b";
2758print "\x{101}$a\x{100}";
2759$a = qr/\Q
2760$b $c
2761\x80
2762\x{100}
2763\E$c
2764/;
2765####
2766# sprintf multiconcat
2767my($a, $b, $c, $d, @a);
2768print sprintf("%s%s%%%sx%s\x{100}%s", $a, $b, $c, scalar @a, $d);
2769####
2770# multiconcat with lexical assign
2771my($a, $b, $c, $d, $e, @a);
2772$d = 'foo' . $a;
2773$d = "foo$a";
2774$d = $a . '';
2775$d = 'foo' . $a . 'bar';
2776$d = $a . $b;
2777$d = $a . $b . $c;
2778$d = $a . $b . $c . @a;
2779$e = ($d = $a . $b . $c);
2780$d = !$a . $b . $c;
2781$a = $b . $c . ($a . $b);
2782$e = f($d = !$a . $b) . $c;
2783$d = "${a}x\x{100}$b$c";
2784f($d = !$a . $b . $c);
2785####
2786# multiconcat with lexical my
2787my($a, $b, $c, $d, $e, @a);
2788my $d1 = 'foo' . $a;
2789my $d2 = "foo$a";
2790my $d3 = $a . '';
2791my $d4 = 'foo' . $a . 'bar';
2792my $d5 = $a . $b;
2793my $d6 = $a . $b . $c;
2794my $e7 = ($d = $a . $b . $c);
2795my $d8 = !$a . $b . $c;
2796my $d9 = $b . $c . ($a . $b);
2797my $da = f($d = !$a . $b) . $c;
2798my $dc = "${a}x\x{100}$b$c";
2799f(my $db = !$a . $b . $c);
2800my $dd = $a . $b . $c . @a;
2801####
2802# multiconcat with lexical append
2803my($a, $b, $c, $d, $e, @a);
2804$d .= '';
2805$d .= $a;
2806$d .= "$a";
2807$d .= 'foo' . $a;
2808$d .= "foo$a";
2809$d .= $a . '';
2810$d .= 'foo' . $a . 'bar';
2811$d .= $a . $b;
2812$d .= $a . $b . $c;
2813$d .= $a . $b . @a;
2814$e .= ($d = $a . $b . $c);
2815$d .= !$a . $b . $c;
2816$a .= $b . $c . ($a . $b);
2817$e .= f($d .= !$a . $b) . $c;
2818f($d .= !$a . $b . $c);
2819$d .= "${a}x\x{100}$b$c";
2820####
2821# multiconcat with expression assign
2822my($a, $b, $c, @a);
2823our($d, $e);
2824$d = 'foo' . $a;
2825$d = "foo$a";
2826$d = $a . '';
2827$d = 'foo' . $a . 'bar';
2828$d = $a . $b;
2829$d = $a . $b . $c;
2830$d = $a . $b . @a;
2831$e = ($d = $a . $b . $c);
2832$a["-$b-"] = !$a . $b . $c;
2833$a[$b]{$c}{$d ? $a : $b . $c} = !$a . $b . $c;
2834$a = $b . $c . ($a . $b);
2835$e = f($d = !$a . $b) . $c;
2836$d = "${a}x\x{100}$b$c";
2837f($d = !$a . $b . $c);
2838####
2839# multiconcat with expression concat
2840my($a, $b, $c, @a);
2841our($d, $e);
2842$d .= 'foo' . $a;
2843$d .= "foo$a";
2844$d .= $a . '';
2845$d .= 'foo' . $a . 'bar';
2846$d .= $a . $b;
2847$d .= $a . $b . $c;
2848$d .= $a . $b . @a;
2849$e .= ($d .= $a . $b . $c);
2850$a["-$b-"] .= !$a . $b . $c;
2851$a[$b]{$c}{$d ? $a : $b . $c} .= !$a . $b . $c;
2852$a .= $b . $c . ($a . $b);
2853$e .= f($d .= !$a . $b) . $c;
2854$d .= "${a}x\x{100}$b$c";
2855f($d .= !$a . $b . $c);
2856####
2857# multiconcat with CORE::sprintf
2858# CONTEXT sub sprintf {}
2859my($a, $b);
2860my $x = CORE::sprintf('%s%s', $a, $b);
2861####
2862# multiconcat with backticks
2863my($a, $b);
2864our $x;
2865$x = `$a-$b`;
2866####
2867# multiconcat within qr//
2868my($r, $a, $b);
2869$r = qr/abc\Q$a-$b\Exyz/;
b4fb15a6
JL
2870####
2871# tr with unprintable characters
2872my $str;
2873$str = 'foo';
2874$str =~ tr/\cA//;
e923f3dc
Z
2875####
2876# CORE::foo special case in bareword parsing
dd666160
Z
2877print $CORE::foo, $CORE::foo::bar;
2878print @CORE::foo, @CORE::foo::bar;
2879print %CORE::foo, %CORE::foo::bar;
2880print $CORE::foo{'a'}, $CORE::foo::bar{'a'};
2881print &CORE::foo, &CORE::foo::bar;
2882print &CORE::foo(), &CORE::foo::bar();
257296eb 2883print \&CORE::foo, \&CORE::foo::bar;
dd666160
Z
2884print *CORE::foo, *CORE::foo::bar;
2885print stat CORE::foo::, stat CORE::foo::bar;
2886print CORE::foo:: 1;
2887print CORE::foo::bar 2;
2888####
2889# trailing colons on glob names
2890no strict 'vars';
2891$Foo::::baz = 1;
2892print $foo, $foo::, $foo::::;
2893print @foo, @foo::, @foo::::;
2894print %foo, %foo::, %foo::::;
2895print $foo{'a'}, $foo::{'a'}, $foo::::{'a'};
2896print &foo, &foo::, &foo::::;
2897print &foo(), &foo::(), &foo::::();
257296eb 2898print \&foo, \&foo::, \&foo::::;
dd666160
Z
2899print *foo, *foo::, *foo::::;
2900print stat Foo, stat Foo::::;
2901print Foo 1;
2902print Foo:::: 2;
2903####
2904# trailing colons mixed with CORE
2905no strict 'vars';
2906print $CORE, $CORE::, $CORE::::;
2907print @CORE, @CORE::, @CORE::::;
2908print %CORE, %CORE::, %CORE::::;
2909print $CORE{'a'}, $CORE::{'a'}, $CORE::::{'a'};
2910print &CORE, &CORE::, &CORE::::;
2911print &CORE(), &CORE::(), &CORE::::();
257296eb 2912print \&CORE, \&CORE::, \&CORE::::;
dd666160
Z
2913print *CORE, *CORE::, *CORE::::;
2914print stat CORE, stat CORE::::;
2915print CORE 1;
2916print CORE:::: 2;
2917print $CORE::foo, $CORE::foo::, $CORE::foo::::;
2918print @CORE::foo, @CORE::foo::, @CORE::foo::::;
2919print %CORE::foo, %CORE::foo::, %CORE::foo::::;
2920print $CORE::foo{'a'}, $CORE::foo::{'a'}, $CORE::foo::::{'a'};
2921print &CORE::foo, &CORE::foo::, &CORE::foo::::;
2922print &CORE::foo(), &CORE::foo::(), &CORE::foo::::();
257296eb 2923print \&CORE::foo, \&CORE::foo::, \&CORE::foo::::;
dd666160
Z
2924print *CORE::foo, *CORE::foo::, *CORE::foo::::;
2925print stat CORE::foo::, stat CORE::foo::::;
2926print CORE::foo:: 1;
2927print CORE::foo:::: 2;
257296eb
Z
2928####
2929# \&foo
2930my sub foo {
2931 1;
2932}
2933no strict 'vars';
2934print \&main::foo;
2935print \&{foo};
2936print \&bar;
2937use strict 'vars';
2938print \&main::foo;
2939print \&{foo};
2940print \&main::bar;
2941####
2942# exists(&foo)
2943my sub foo {
2944 1;
2945}
2946no strict 'vars';
2947print exists &main::foo;
2948print exists &{foo};
2949print exists &bar;
2950use strict 'vars';
2951print exists &main::foo;
2952print exists &{foo};
2953print exists &main::bar;
ed34dca8
DM
2954# precedence of optimised-away 'keys' (OPpPADHV_ISKEYS/OPpRV2HV_ISKEYS)
2955my($r1, %h1, $res);
2956our($r2, %h2);
2957$res = keys %h1;
2958$res = keys %h2;
2959$res = keys %$r1;
2960$res = keys %$r2;
2961$res = keys(%h1) / 2 - 1;
2962$res = keys(%h2) / 2 - 1;
2963$res = keys(%$r1) / 2 - 1;
2964$res = keys(%$r2) / 2 - 1;
2965####
2966# ditto in presence of sub keys {}
2967# CONTEXT sub keys {}
2968no warnings;
2969my($r1, %h1, $res);
2970our($r2, %h2);
2971CORE::keys %h1;
2972CORE::keys(%h1) / 2;
2973$res = CORE::keys %h1;
2974$res = CORE::keys %h2;
2975$res = CORE::keys %$r1;
2976$res = CORE::keys %$r2;
2977$res = CORE::keys(%h1) / 2 - 1;
2978$res = CORE::keys(%h2) / 2 - 1;
2979$res = CORE::keys(%$r1) / 2 - 1;
2980$res = CORE::keys(%$r2) / 2 - 1;
4b75096e
DM
2981####
2982# concat: STACKED: ambiguity between .= and optimised nested
2983my($a, $b);
2984$b = $a . $a . $a;
2985(($a .= $a) .= $a) .= $a;
b2362f9c
DM
2986####
2987# multiconcat: $$ within string
2988my($a, $x);
2989$x = "${$}abc";
2990$x = "\$$a";
c4874d8a
DM
2991####
2992# single state aggregate assignment
2993# CONTEXT use feature "state";
2994state @a = (1, 2, 3);
2995state %h = ('a', 1, 'b', 2);