This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Revert "perlapi: Document sv_dup(_inc)?"
[perl5.git] / t / op / lex.t
... / ...
CommitLineData
1#!perl
2
3# Tests too complex for t/base/lex.t
4
5use strict;
6use warnings;
7
8BEGIN { chdir 't' if -d 't'; require './test.pl'; }
9
10plan(tests => 53);
11
12{
13 print <<''; # Yow!
14ok 1
15
16 # previous line intentionally left blank.
17
18 my $yow = "ok 2";
19 print <<""; # Yow!
20$yow
21
22 # previous line intentionally left blank.
23}
24
25curr_test(3);
26
27
28{
29 my %foo = (aap => "monkey");
30 my $foo = '';
31 is("@{[$foo{'aap'}]}", 'monkey', 'interpolation of hash lookup with space between lexical variable and subscript');
32 is("@{[$foo {'aap'}]}", 'monkey', 'interpolation of hash lookup with space between lexical variable and subscript - test for [perl #70091]');
33
34# Original bug report [perl #70091]
35# #!perl
36# use warnings;
37# my %foo;
38# my $foo = '';
39# (my $tmp = $foo) =~ s/^/$foo {$0}/e;
40# __END__
41#
42# This program causes a segfault with 5.10.0 and 5.10.1.
43#
44# The space between '$foo' and '{' is essential, which is why piping
45# it through perl -MO=Deparse "fixes" it.
46#
47
48}
49
50{
51 delete local $ENV{PERL_UNICODE};
52 fresh_perl_is(
53 'BEGIN{ ++$_ for @INC{"charnames.pm","_charnames.pm"} } "\N{a}"',
54 'Constant(\N{a}) unknown at - line 1, within string' . "\n"
55 ."Execution of - aborted due to compilation errors.\n",
56 { stderr => 1 },
57 'correct output (and no crash) when charnames cannot load for \N{...}'
58 );
59}
60fresh_perl_is(
61 'BEGIN{ ++$_ for @INC{"charnames.pm","_charnames.pm"};
62 $^H{charnames} = "foo" } "\N{a}"',
63 "Undefined subroutine &main::foo called at - line 2.\n"
64 ."Propagated at - line 2, within string\n"
65 ."Execution of - aborted due to compilation errors.\n",
66 { stderr => 1 },
67 'no crash when charnames cannot load and %^H holds string'
68);
69fresh_perl_is(
70 'BEGIN{ ++$_ for @INC{"charnames.pm","_charnames.pm"};
71 $^H{charnames} = \"foo" } "\N{a}"',
72 "Not a CODE reference at - line 2.\n"
73 ."Propagated at - line 2, within string\n"
74 ."Execution of - aborted due to compilation errors.\n",
75 { stderr => 1 },
76 'no crash when charnames cannot load and %^H holds string reference'
77);
78
79# not fresh_perl_is, as it seems to hide the error
80is runperl(
81 nolib => 1, # -Ilib may also hide the error
82 progs => [
83 '*{',
84 ' XS::APItest::gv_fetchmeth_type()',
85 '}'
86 ],
87 stderr => 1,
88 ),
89 "Undefined subroutine &XS::APItest::gv_fetchmeth_type called at -e line "
90 ."2.\n",
91 'no buffer corruption with multiline *{...expr...}'
92;
93
94$_ = "rhubarb";
95is ${no strict; \$_}, "rhubarb", '${no strict; ...}';
96is join("", map{no strict; "rhu$_" } "barb"), 'rhubarb',
97 'map{no strict;...}';
98
99# [perl #123753]
100fresh_perl_is(
101 '$eq = "ok\n"; print $' . "\0eq\n",
102 "ok\n",
103 { stderr => 1 },
104 '$ <null> ident'
105);
106fresh_perl_is(
107 '@eq = "ok\n"; print @' . "\0eq\n",
108 "ok\n",
109 { stderr => 1 },
110 '@ <null> ident'
111);
112fresh_perl_is(
113 '%eq = ("o"=>"k\n"); print %' . "\0eq\n",
114 "ok\n",
115 { stderr => 1 },
116 '% <null> ident'
117);
118fresh_perl_is(
119 'sub eq { "ok\n" } print &' . "\0eq\n",
120 "ok\n",
121 { stderr => 1 },
122 '& <null> ident'
123);
124fresh_perl_is(
125 '$eq = "ok\n"; print ${*' . "\0eq{SCALAR}}\n",
126 "ok\n",
127 { stderr => 1 },
128 '* <null> ident'
129);
130SKIP: {
131 skip "Different output on EBCDIC (presumably)", 3 if $::IS_EBCDIC;
132 fresh_perl_is(
133 qq'"ab}"ax;&\0z\x8Ao}\x82x;', <<gibberish,
134Bareword found where operator expected at - line 1, near ""ab}"ax"
135 (Missing operator before ax?)
136syntax error at - line 1, near ""ab}"ax"
137Unrecognized character \\x8A; marked by <-- HERE after ab}"ax;&\0z<-- HERE near column 12 at - line 1.
138gibberish
139 { stderr => 1 },
140 'gibberish containing &\0z - used to crash [perl #123753]'
141 );
142 fresh_perl_is(
143 qq'"ab}"ax;&{+z}\x8Ao}\x82x;', <<gibberish,
144Bareword found where operator expected at - line 1, near ""ab}"ax"
145 (Missing operator before ax?)
146syntax error at - line 1, near ""ab}"ax"
147Unrecognized character \\x8A; marked by <-- HERE after }"ax;&{+z}<-- HERE near column 14 at - line 1.
148gibberish
149 { stderr => 1 },
150 'gibberish containing &{+z} - used to crash [perl #123753]'
151 );
152 fresh_perl_is(
153 "\@{\327\n", <<\gibberisi,
154Unrecognized character \xD7; marked by <-- HERE after @{<-- HERE near column 3 at - line 1.
155gibberisi
156 { stderr => 1 },
157 '@ { \327 \n - used to garble output (or fail asan) [perl #128951]'
158 );
159}
160
161fresh_perl_is(
162 '/$a[/<<a',
163 "Missing right curly or square bracket at - line 1, within pattern\n" .
164 "syntax error at - line 1, at EOF\n" .
165 "Execution of - aborted due to compilation errors.\n",
166 { stderr => 1 },
167 '/$a[/<<a with no newline [perl #123712]'
168);
169fresh_perl_is(
170 '/$a[m||/<<a',
171 "Missing right curly or square bracket at - line 1, within pattern\n" .
172 "syntax error at - line 1, at EOF\n" .
173 "Execution of - aborted due to compilation errors.\n",
174 { stderr => 1 },
175 '/$a[m||/<<a with no newline [perl #123712]'
176);
177
178fresh_perl_is(
179 '"@{"',
180 "Missing right curly or square bracket at - line 1, within string\n" .
181 "syntax error at - line 1, at EOF\n" .
182 "Execution of - aborted due to compilation errors.\n",
183 { stderr => 1 },
184 '"@{" [perl #123712]'
185);
186
187fresh_perl_is(
188 '/$0{}/',
189 'syntax error at - line 1, near "{}"' . "\n" .
190 "Execution of - aborted due to compilation errors.\n",
191 { stderr => 1 },
192 '/$0{}/ with no newline [perl #123802]'
193);
194fresh_perl_is(
195 '"\L\L"',
196 'syntax error at - line 1, near "\L\L"' . "\n" .
197 "Execution of - aborted due to compilation errors.\n",
198 { stderr => 1 },
199 '"\L\L" with no newline [perl #123802]'
200);
201fresh_perl_is(
202 '<\L\L>',
203 'syntax error at - line 1, near "\L\L"' . "\n" .
204 "Execution of - aborted due to compilation errors.\n",
205 { stderr => 1 },
206 '<\L\L> with no newline [perl #123802]'
207);
208
209is eval "qq'@\x{ff13}'", "\@\x{ff13}",
210 '"@<fullwidth digit>" [perl #123963]';
211
212fresh_perl_is(
213 "s;\@{<<a;\n",
214 "Can't find string terminator \"a\" anywhere before EOF at - line 1.\n",
215 { stderr => 1 },
216 's;@{<<a; [perl #123995]'
217);
218
219fresh_perl_is(
220 '$_ = q-strict.pm-; 1 ? require : die;'
221 .' print qq-ok\n- if $INC{q-strict.pm-}',
222 "ok\n",
223 {},
224 'foo ? require : bar [perl #128307]'
225);
226
227like runperl(prog => 'sub ub(){0} ub ub', stderr=>1), qr/Bareword found/,
228 '[perl #126482] Assert failure when mentioning a constant twice in a row';
229
230fresh_perl_is(
231 "do\0"."000000",
232 "",
233 {},
234 '[perl #129069] - no output and valgrind clean'
235);
236
237fresh_perl_is(
238 "00my sub\0",
239 "Missing name in \"my sub\" at - line 1.\n",
240 {},
241 '[perl #129069] - "Missing name" warning and valgrind clean'
242);
243
244fresh_perl_like(
245 "#!perl -i u\nprint 'OK'",
246 qr/OK/,
247 {},
248 '[perl #129336] - #!perl -i argument handling'
249);
250SKIP:
251{
252 ord("A") == 65
253 or skip "These tests won't work on EBCIDIC", 3;
254 fresh_perl_is(
255 "BEGIN{\$^H=hex ~0}\xF3",
256 "Integer overflow in hexadecimal number at - line 1.\n"
257 . "Malformed UTF-8 character: \\xf3 (too short; 1 byte available, need 4) at - line 1.\n"
258 . "Malformed UTF-8 character (fatal) at - line 1.",
259 {},
260 '[perl #128996] - use of PL_op after op is freed'
261 );
262 fresh_perl_like(
263 qq(BEGIN{\$0="";\$^H=-hex join""=>1}""\xFF),
264 qr/Malformed UTF-8 character: \\xff \(too short; 1 byte available, need 13\) at - line 1\./,
265 {},
266 '[perl #128997] - buffer read overflow'
267 );
268 fresh_perl_like(
269 qq(BEGIN{\$^H=0x800000}\n 0m 0\xB5\xB500\xB5\0),
270 qr/Malformed UTF-8 character: \\xb5 \(unexpected continuation byte 0xb5, with no preceding start byte\)/,
271 {},
272 '[perl #129000] read before buffer'
273 );
274}
275# probably only failed under ASAN
276fresh_perl_is(
277 "stat\tt\$#0",
278 <<'EOM',
279$# is no longer supported as of Perl 5.30 at - line 1.
280EOM
281 {},
282 "[perl #129273] heap use after free or overflow"
283);
284
285fresh_perl_like('flock _$', qr/Not enough arguments for flock/, {stderr => 1},
286 "[perl #129190] intuit_method() invalidates PL_bufptr");
287
288# Below are tests for the single set of Latin1 range paired string delimiters
289# enabled by a feature, when the string isn't UTF-8. It is more convenient to
290# do all the UTF-8 testing for this feature in t/lib/croak/toke and
291# t/lib/warnings/toke.
292use feature 'evalbytes';
293my $lhs = "\N{U+AB}";
294utf8::downgrade($lhs);
295my $rhs = "\N{U+BB}";
296utf8::downgrade($rhs);
297
298my @warnings;
299local $SIG{__WARN__} = sub {
300 my $warning = $_[0];
301 chomp $warning;
302 push @warnings, ($warning =~ s/\n/\n# /sgr);
303};
304
305evalbytes <<EOS;
306use feature 'extra_paired_delimiters';
307
308my \$warns = q$lhs this string uses paired latin1 delimiters $rhs;
309
310no warnings 'experimental::extra_paired_delimiters';
311
312my \$nowarn = q$lhs this string uses paired latin1 delimiters $rhs;
313no feature 'extra_paired_delimiters';
314my \$warn2= q$lhs this string uses lhs delimiter fore/aft $lhs;
315my \$warn3= q$rhs this string uses rhs delimiter fore/aft $rhs;
316EOS
317
318is($@, "", "Various tests of string delims $lhs/$rhs returned without error");
319is(@warnings, 3, "And the expected number of warnings were generated");
320like($warnings[0],
321 qr/Use of '$lhs' is experimental as a string delimiter at/,
322 'And the first warning is as expected');
323like($warnings[1],
324 qr/Use of '$lhs' is deprecated as a string delimiter at/,
325 'And the second warning is as expected');
326like($warnings[2],
327 qr/Use of '$rhs' is deprecated as a string delimiter at/,
328 'And the third warning is as expected');
329
330undef @warnings;
331evalbytes <<EOS;
332use feature 'extra_paired_delimiters';
333no warnings 'experimental::extra_paired_delimiters';
334my \$warn2= q$lhs this string uses lhs delimiter fore/aft $lhs;
335EOS
336
337like($@, qr/Can't find string terminator "$rhs" anywhere before EOF/,
338 "Using paired delimiter both fore/aft fails as expected");
339is(@warnings, 0, "With no warnings generated");
340
341undef @warnings;
342evalbytes <<EOS;
343no warnings 'deprecated';
344my \$warn2= q$lhs this string uses lhs delimiter fore/aft $rhs;
345EOS
346
347like($@, qr/Can't find string terminator "$lhs" anywhere before EOF/,
348 "Using extra paired delimiter outside scope fails as expected");
349is(@warnings, 0, "With no warnings generated");
350
351undef @warnings;
352evalbytes <<EOS;
353use feature 'extra_paired_delimiters';
354no warnings 'experimental::extra_paired_delimiters';
355my \$warn2= q$rhs this string reverses the delimiters $lhs;
356EOS
357
358is($@, "", "Reversing delimiters works, as expected"
359 . " within scope of extra delims");
360is(@warnings, 0, "With no warnings generated");
361
362undef @warnings;
363evalbytes <<EOS;
364no warnings 'deprecated';
365my \$warn2= q$rhs this string uses lhs delimiter fore/aft $lhs;
366EOS
367
368like($@, qr/Can't find string terminator "$rhs" anywhere before EOF/,
369 "Using terminating paired delimiter fore, opening aft fails as expected"
370 . " outside scope of extra delims");
371is(@warnings, 0, "With no warnings generated");
372
373undef @warnings;
374evalbytes <<EOS;
375no warnings 'experimental::extra_paired_delimiters';
376use feature 'extra_paired_delimiters';
377my \$good= q$lhs this $lhs string has $lhs $lhs nested $rhs paired $rhs $rhs delimiters $rhs;
378EOS
379
380is($@, "", "Using nested extra paired delimiters works");
381is(@warnings, 0, "With no warnings generated");
382
383undef @warnings;
384evalbytes <<EOS;
385no warnings 'experimental::extra_paired_delimiters';
386use feature 'extra_paired_delimiters';
387my \$good= q$lhs this $lhs string has $lhs too few closing $lhs nested $rhs paired rhs $rhs delimiters $rhs;
388EOS
389
390like($@, qr/Can't find string terminator "$rhs" anywhere before EOF/,
391 "Using too few closing delims in nesting fails as expected");
392is(@warnings, 0, "With no warnings generated");