3 # Checks if the parser behaves correctly in edge case
4 # (including weird syntax errors)
9 skip_all_without_unicode_tables();
14 use open qw( :utf8 :std );
15 no warnings qw(misc reserved);
17 plan (tests => 66880);
19 # ${single:colon} should not be treated as a simple variable, but as a
20 # block with a label inside.
25 eval "\${\x{30cd}single:\x{30cd}colon} = 'label, not var'";
26 is ${"\x{30cd}colon"}, 'label, not var',
27 '${\x{30cd}single:\x{30cd}colon} should be block-label';
31 evalbytes '${single:colon} = "block/label, not var"';
33 'block/label, not var',
34 '...same with ${single:colon}'
38 # ${yadda'etc} and ${yadda::etc} should both work under strict
41 eval q<use strict; ${flark::fleem}>;
42 is($@, '', q<${package::var} works>);
45 eval q<use strict; ${fleem'flark}>;
46 is($@, '', q<...as does ${package'var}>);
49 # The first character in ${...} should respect the rules
54 like($@, qr/\QUnrecognized character/, q(the first character in ${...} isn't special))
57 # Checking that at least some of the special variables work
58 for my $v (qw( ^V ; < > ( ) {^GLOBAL_PHASE} ^W _ 1 4 0 ] ! @ / \ = )) {
62 is $@, '', "No syntax error for \$$v";
65 eval "use utf8; \$$v;";
66 is $@, '', "No syntax error for \$$v under 'use utf8'";
70 # Checking if the Latin-1 range behaves as expected, and that the behavior is the
71 # same whenever under strict or not.
74 local $SIG {__WARN__} = sub {push @warnings, @_ };
75 my $ord = utf8::unicode_to_native($_);
77 my $syntax_error = 0; # Do we expect this code point to generate a
78 # syntax error? Assume not, for now
82 # A different number of tests are run depending on the branches in this
83 # loop iteration. This allows us to add skips to make the reported total
84 # the same for each iteration.
88 if ($chr =~ /[[:graph:]]/a) {
90 $syntax_error = 1 if $chr eq '{';
92 elsif ($chr =~ /[[:space:]]/a) {
93 $name = sprintf "\\x%02x, an ASCII space character", $ord;
96 elsif ($chr =~ /[[:cntrl:]]/a) {
97 $name = sprintf "\\x%02x, an ASCII control", $ord;
100 elsif ($chr =~ /\pC/) {
101 if ($chr eq "\N{SHY}") {
102 $name = sprintf "\\x%02x, SHY", $ord;
105 $name = sprintf "\\x%02x, a C1 control", $ord;
108 $deprecated = ! $syntax_error;
110 elsif ($chr =~ /\p{XIDStart}/) {
111 $name = sprintf "\\x%02x, a non-ASCII XIDS character", $ord;
113 elsif ($chr =~ /\p{XPosixSpace}/) {
114 $name = sprintf "\\x%02x, a non-ASCII space character", $ord;
116 $deprecated = ! $syntax_error;
119 $name = sprintf "\\x%02x, a non-ASCII, non-XIDS graphic character", $ord;
121 no warnings 'closure';
122 my $esc = sprintf("%X", $ord);
123 utf8::downgrade($chr);
124 if ($chr !~ /\p{XIDS}/u) {
127 like($@, qr/ syntax\ error | Unrecognized\ character /x,
128 "$name as a length-1 variable generates a syntax error");
131 eval "no strict; \$$chr = 4;",
132 like($@, qr/ syntax\ error | Unrecognized\ character /x,
133 " ... and the same under 'use utf8'");
136 elsif ($chr =~ /[[:punct:][:digit:]]/a) {
137 next if ($chr eq '#' or $chr eq '*'); # RT 133583
139 # Unlike other variables, we dare not try setting the length-1
140 # variables that are ASCII punctuation and digits. This is
141 # because many of these variables have meaning to the system, and
142 # setting them could have side effects or not work as expected
143 # (And using fresh_perl() doesn't always help.) For all these we
144 # just verify that they don't generate a syntax error.
147 is $@, '', "$name as a length-1 variable doesn't generate a syntax error";
150 evalbytes "no strict; use utf8; \$$chr;",
151 is $@, '', " ... and the same under 'use utf8'";
155 is evalbytes "no strict; \$$chr = 10",
157 "$name is legal as a length-1 variable";
159 if ($chr =~ /[[:ascii:]]/) {
161 is evalbytes "no strict; use utf8; \$$chr = 1",
163 " ... and is legal under 'use utf8'";
169 eval "no strict; use utf8; \$$chr = 1";
171 qr/\QUnrecognized character \x{\E\L$esc/,
172 " ... but is illegal as a length-1 variable under 'use utf8'";
181 evalbytes "no strict; \$$chr = 1";
182 is($@, '', "$name under 'no utf8', 'no strict', is a valid length-1 variable");
185 if ($chr !~ /[[:ascii:]]/) {
187 evalbytes "use strict; \$$chr = 1";
190 " ... and under 'no utf8' does not have to be required under strict, even though it matches XIDS"
195 evalbytes "\$a$chr = 1";
197 qr/Unrecognized character /,
198 " ... but under 'no utf8', it's not allowed in length-2+ variables"
206 utf8::upgrade($utf8);
208 eval "no strict; \$$utf8 = 1";
209 is($@, '', " ... and under 'use utf8', 'no strict', is a valid length-1 variable");
213 eval "use strict; \$$utf8 = 1";
214 if ($chr =~ /[ab]/) { # These are special, for sort()
215 is($@, '', " ... and under 'use utf8', 'use strict',"
216 . " is a valid length-1 variable (\$a and \$b are special)");
221 qr/Global symbol "\$$utf8" requires explicit package name/,
222 " ... and under utf8 has to be required under strict"
230 if ($chr =~ /[#*]/) {
232 # Length-1 variables with these two characters used to be used by
233 # Perl, but now a warning is generated that they're gone.
234 # Ignore such warnings.
235 for (my $i = @warnings - 1; $i >= 0; $i--) {
236 splice @warnings, $i, 1 if $warnings[$i] =~ /is no longer supported/;
239 my $message = " ... and doesn't generate any warnings";
240 $message = " TODO $message" if $ord == 0
243 if (! ok(@warnings == 0, $message)) {
244 note join "\n", @warnings;
248 elsif (! @warnings) {
249 fail(" ... and generates deprecation warnings (since is deprecated)");
253 ok((scalar @warnings == grep { $_ =~ /deprecated/ } @warnings),
254 " ... and generates deprecation warnings (only)");
259 die "Wrong max count for tests" if $tests > $max_tests;
260 skip("untaken tests", $max_tests - $tests) if $max_tests > $tests;
266 my $ret = eval "my \$c\x{327} = 100; \$c\x{327}"; # c + cedilla
267 is($@, '', "ASCII character + combining character works as a variable name");
268 is($ret, 100, " ... and returns the correct value");
271 # From Tom Christiansen's 'highly illegal variable names are now accidentally legal' mail
273 "\N{EM DASH}", "\x{F8FF}", "\N{POUND SIGN}", "\N{SOFT HYPHEN}",
274 "\N{THIN SPACE}", "\x{11_1111}", "\x{DC00}", "\N{COMBINING DIAERESIS}",
275 "\N{COMBINING ENCLOSING CIRCLE BACKSLASH}",
278 no warnings 'non_unicode';
279 my $esc = sprintf("%x", ord $chr);
281 eval "\$$chr = 1; \$$chr";
283 qr/\QUnrecognized character \x{$esc};/,
284 "\\x{$esc} is illegal for a length-one identifier"
288 for my $i (0x100..0xffff) {
290 my $esc = sprintf("%x", $i);
292 eval "my \$$chr = q<test>; \$$chr;";
293 if ( $chr =~ /^\p{_Perl_IDStart}$/ ) {
294 is($@, '', sprintf("\\x{%04x} is XIDS, works as a length-1 variable", $i));
298 qr/\QUnrecognized character \x{$esc};/,
299 "\\x{$esc} isn't XIDS, illegal as a length-1 variable",
305 # Bleadperl v5.17.9-109-g3283393 breaks ZEFRAM/Module-Runtime-0.013.tar.gz
306 # https://github.com/Perl/perl5/issues/12841
312 is($$1, $$, q{$$1 parses as ${$1}});
318 is( $$$$1, $test, q{$$$$1 parses as ${${${$1}}}} );
320 is($@, '', q{$$1 parses correctly});
322 for my $chr ( q{@}, "\N{U+FF10}", "\N{U+0300}" ) {
323 my $esc = sprintf("\\x{%x}", ord $chr);
331 qr/syntax error|Unrecognized character/,
332 qq{\$\$$esc is a syntax error}
338 # bleadperl v5.17.9-109-g3283393 breaks JEREMY/File-Signature-1.009.tar.gz
339 # https://github.com/Perl/perl5/issues/12849
347 '${ var } works under strict'
353 for my $var ( '$', "^GLOBAL_PHASE", "^V" ) {
355 is($@, '', "\${ $var} works" );
357 is($@, '', "\${$var } works" );
359 is($@, '', "\${ $var } works" );
361 my $var = "\7LOBAL_PHASE";
363 like($@, qr/Unrecognized character \\x07/,
364 "\${ $var} generates 'Unrecognized character' error" );
366 like($@, qr/Unrecognized character \\x07/,
367 "\${$var } generates 'Unrecognized character' error" );
369 like($@, qr/Unrecognized character \\x07/,
370 "\${ $var } generates 'Unrecognized character' error" );
378 "Newlines at the start of an identifier should be skipped over"
383 skip('Is $^U on EBCDIC 1047, BC; nothing works on 0037', 1)
388 " ... but \$^J is still legal"
392 my $ret = eval "\${\cT\n}";
393 like($@, qr/\QUnrecognized character/, '${\n\cT\n} gives an error message');
397 # Prior to 5.19.4, the following changed behavior depending
398 # on the presence of the newline after '@{'.
401 my $ret = @{ foo { "a" } };
402 is($ret, $foo{a}, '@{ foo { "a" } } is parsed as @foo{a}');
407 is($ret, $foo{a}, '@{\nfoo { "a" } } is still parsed as @foo{a}');