This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Get pat_re_eval.t working under minitest
[perl5.git] / t / re / pat_re_eval.t
CommitLineData
0f289c68
YO
1#!./perl
2#
3# This is a home for regular expression tests that don't fit into
4# the format supported by re/regexp.t. If you want to add a test
5# that does fit that format, add it to re/re_tests, not here.
6
7use strict;
8use warnings;
04e69edb 9use Config;
0f289c68
YO
10use 5.010;
11
12
13sub run_tests;
14
15$| = 1;
16
17
18BEGIN {
19 chdir 't' if -d 't';
90b541eb 20 require './test.pl';
223a2879
FC
21 @INC = () unless is_miniperl();
22 unshift @INC, '../lib';
0f289c68
YO
23}
24
25
4b22688e 26plan tests => 527; # Update this when adding/deleting tests.
0f289c68
YO
27
28run_tests() unless caller;
29
1008bb63
DM
30# test that runtime code without 'use re eval' is trapped
31
32sub norun {
33 like($@, qr/Eval-group not allowed at runtime/, @_);
34}
35
0f289c68
YO
36#
37# Tests start here.
38#
39sub run_tests {
40 {
f245da07 41 my $message = "Call code from qr //";
0f289c68
YO
42 local $_ = 'var="foo"';
43 $a = qr/(?{++$b})/;
44 $b = 7;
f245da07 45 ok(/$a$a/ && $b eq '9', $message);
0f289c68
YO
46
47 my $c="$a";
f245da07 48 ok(/$a$a/ && $b eq '11', $message);
0f289c68
YO
49
50 undef $@;
51 eval {/$c/};
1008bb63 52 norun("$message norun 1");
0f289c68 53
1008bb63
DM
54
55 {
56 eval {/$a$c$a/};
57 norun("$message norun 2");
58 use re "eval";
59 /$a$c$a/;
60 is($b, '14', $message);
61 }
0f289c68
YO
62
63 our $lex_a = 43;
64 our $lex_b = 17;
65 our $lex_c = 27;
66 my $lex_res = ($lex_b =~ qr/$lex_b(?{ $lex_c = $lex_a++ })/);
67
f245da07
NC
68 is($lex_res, 1, $message);
69 is($lex_a, 44, $message);
70 is($lex_c, 43, $message);
0f289c68 71
0f289c68 72 undef $@;
b30fcab9
DM
73 my $d = '(?{1})';
74 my $match = eval { /$a$c$a$d/ };
f245da07
NC
75 ok($@ && $@ =~ /Eval-group not allowed/ && !$match, $message);
76 is($b, '14', $message);
0f289c68
YO
77
78 $lex_a = 2;
79 $lex_a = 43;
80 $lex_b = 17;
81 $lex_c = 27;
82 $lex_res = ($lex_b =~ qr/17(?{ $lex_c = $lex_a++ })/);
83
f245da07
NC
84 is($lex_res, 1, $message);
85 is($lex_a, 44, $message);
86 is($lex_c, 43, $message);
0f289c68
YO
87
88 }
89
90 {
91 our $a = bless qr /foo/ => 'Foo';
92 ok 'goodfood' =~ $a, "Reblessed qr // matches";
de26e0cc 93 is($a, '(?^:foo)', "Reblessed qr // stringifies");
0f289c68
YO
94 my $x = "\x{3fe}";
95 my $z = my $y = "\317\276"; # Byte representation of $x
96 $a = qr /$x/;
97 ok $x =~ $a, "UTF-8 interpolation in qr //";
98 ok "a$a" =~ $x, "Stringified qr // preserves UTF-8";
99 ok "a$x" =~ /^a$a\z/, "Interpolated qr // preserves UTF-8";
100 ok "a$x" =~ /^a(??{$a})\z/,
101 "Postponed interpolation of qr // preserves UTF-8";
bb535cf1
NC
102
103
104 is(length qr /##/x, 9, "## in qr // doesn't corrupt memory; Bug 17776");
105
0f289c68 106 {
0f289c68
YO
107 ok "$x$x" =~ /^$x(??{$x})\z/,
108 "Postponed UTF-8 string in UTF-8 re matches UTF-8";
109 ok "$y$x" =~ /^$y(??{$x})\z/,
110 "Postponed UTF-8 string in non-UTF-8 re matches UTF-8";
111 ok "$y$x" !~ /^$y(??{$y})\z/,
112 "Postponed non-UTF-8 string in non-UTF-8 re doesn't match UTF-8";
113 ok "$x$x" !~ /^$x(??{$y})\z/,
114 "Postponed non-UTF-8 string in UTF-8 re doesn't match UTF-8";
115 ok "$y$y" =~ /^$y(??{$y})\z/,
116 "Postponed non-UTF-8 string in non-UTF-8 re matches non-UTF8";
117 ok "$x$y" =~ /^$x(??{$y})\z/,
118 "Postponed non-UTF-8 string in UTF-8 re matches non-UTF8";
119
120 $y = $z; # Reset $y after upgrade.
121 ok "$x$y" !~ /^$x(??{$x})\z/,
122 "Postponed UTF-8 string in UTF-8 re doesn't match non-UTF-8";
123 ok "$y$y" !~ /^$y(??{$x})\z/,
124 "Postponed UTF-8 string in non-UTF-8 re doesn't match non-UTF-8";
125 }
126 }
127
128
129 {
1008bb63 130 # Test if $^N and $+ work in (?{})
0f289c68
YO
131 our @ctl_n = ();
132 our @plus = ();
133 our $nested_tags;
134 $nested_tags = qr{
135 <
136 ((\w)+)
137 (?{
138 push @ctl_n, (defined $^N ? $^N : "undef");
139 push @plus, (defined $+ ? $+ : "undef");
140 })
141 >
142 (??{$nested_tags})*
143 </\s* \w+ \s*>
144 }x;
145
146
147 my $c = 0;
148 for my $test (
149 # Test structure:
150 # [ Expected result, Regex, Expected value(s) of $^N, Expected value(s) of $+ ]
151 [ 1, qr#^$nested_tags$#, "bla blubb bla", "a b a" ],
152 [ 1, qr#^($nested_tags)$#, "bla blubb <bla><blubb></blubb></bla>", "a b a" ],
153 [ 1, qr#^(|)$nested_tags$#, "bla blubb bla", "a b a" ],
154 [ 1, qr#^(?:|)$nested_tags$#, "bla blubb bla", "a b a" ],
155 [ 1, qr#^<(bl|bla)>$nested_tags<(/\1)>$#, "blubb /bla", "b /bla" ],
156 [ 1, qr#(??{"(|)"})$nested_tags$#, "bla blubb bla", "a b a" ],
157 [ 1, qr#^(??{"(bla|)"})$nested_tags$#, "bla blubb bla", "a b a" ],
158 [ 1, qr#^(??{"(|)"})(??{$nested_tags})$#, "bla blubb undef", "a b undef" ],
159 [ 1, qr#^(??{"(?:|)"})$nested_tags$#, "bla blubb bla", "a b a" ],
160 [ 1, qr#^((??{"(?:bla|)"}))((??{$nested_tags}))$#, "bla blubb <bla><blubb></blubb></bla>", "a b <bla><blubb></blubb></bla>" ],
161 [ 1, qr#^((??{"(?!)?"}))((??{$nested_tags}))$#, "bla blubb <bla><blubb></blubb></bla>", "a b <bla><blubb></blubb></bla>" ],
162 [ 1, qr#^((??{"(?:|<(/?bla)>)"}))((??{$nested_tags}))\1$#, "bla blubb <bla><blubb></blubb></bla>", "a b <bla><blubb></blubb></bla>" ],
163 [ 0, qr#^((??{"(?!)"}))?((??{$nested_tags}))(?!)$#, "bla blubb undef", "a b undef" ],
164
165 ) { #"#silence vim highlighting
166 $c++;
167 @ctl_n = ();
168 @plus = ();
169 my $match = (("<bla><blubb></blubb></bla>" =~ $test->[1]) ? 1 : 0);
170 push @ctl_n, (defined $^N ? $^N : "undef");
171 push @plus, (defined $+ ? $+ : "undef");
172 ok($test->[0] == $match, "match $c");
173 if ($test->[0] != $match) {
174 # unset @ctl_n and @plus
175 @ctl_n = @plus = ();
176 }
de26e0cc
NC
177 is("@ctl_n", $test->[2], "ctl_n $c");
178 is("@plus", $test->[3], "plus $c");
0f289c68
YO
179 }
180 }
181
182 {
0f289c68
YO
183 our $f;
184 local $f;
185 $f = sub {
186 defined $_[0] ? $_[0] : "undef";
187 };
188
4b0d13b9 189 like("123", qr/^(\d)(((??{1 + $^N})))+$/, 'Bug 56194');
0f289c68
YO
190
191 our @ctl_n;
192 our @plus;
193
194 my $re = qr#(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))*(?{$^N})#;
195 my $re2 = qr#(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))*(?{$^N})(|a(b)c|def)(??{"$^R"})#;
196 my $re3 = qr#(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1})){2}(?{$^N})(|a(b)c|def)(??{"$^R"})#;
197 our $re5;
198 local $re5 = qr#(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1})){2}(?{$^N})#;
199 my $re6 = qr#(??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1})#;
200 my $re7 = qr#(??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1})#;
201 my $re8 = qr/(\d+)/;
202 my $c = 0;
203 for my $test (
204 # Test structure:
205 # [
206 # String to match
207 # Regex too match
208 # Expected values of $^N
209 # Expected values of $+
210 # Expected values of $1, $2, $3, $4 and $5
211 # ]
212 [
213 "1233",
214 qr#^(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))+(??{$^N})$#,
215 "1 2 3 3",
216 "1 2 3 3",
217 "\$1 = 1, \$2 = 3, \$3 = undef, \$4 = undef, \$5 = undef",
218 ],
219 [
220 "1233",
221 qr#^(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))+(abc|def|)?(??{$+})$#,
222 "1 2 3 3",
223 "1 2 3 3",
224 "\$1 = 1, \$2 = 3, \$3 = undef, \$4 = undef, \$5 = undef",
225 ],
226 [
227 "1233",
228 qr#^(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))+(|abc|def)?(??{$+})$#,
229 "1 2 3 3",
230 "1 2 3 3",
231 "\$1 = 1, \$2 = 3, \$3 = undef, \$4 = undef, \$5 = undef",
232 ],
233 [
234 "1233",
235 qr#^(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))+(abc|def|)?(??{$^N})$#,
236 "1 2 3 3",
237 "1 2 3 3",
238 "\$1 = 1, \$2 = 3, \$3 = undef, \$4 = undef, \$5 = undef",
239 ],
240 [
241 "1233",
242 qr#^(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))+(|abc|def)?(??{$^N})$#,
243 "1 2 3 3",
244 "1 2 3 3",
245 "\$1 = 1, \$2 = 3, \$3 = undef, \$4 = undef, \$5 = undef",
246 ],
247 [
248 "123abc3",
249 qr#^($re)(|a(b)c|def)(??{$^R})$#,
250 "1 2 3 abc",
251 "1 2 3 b",
252 "\$1 = 123, \$2 = 1, \$3 = 3, \$4 = abc, \$5 = b",
253 ],
254 [
255 "123abc3",
256 qr#^($re2)$#,
257 "1 2 3 123abc3",
258 "1 2 3 b",
259 "\$1 = 123abc3, \$2 = 1, \$3 = 3, \$4 = abc, \$5 = b",
260 ],
261 [
262 "123abc3",
263 qr#^($re3)$#,
264 "1 2 123abc3",
265 "1 2 b",
266 "\$1 = 123abc3, \$2 = 1, \$3 = 3, \$4 = abc, \$5 = b",
267 ],
268 [
269 "123abc3",
270 qr#^(??{$re5})(|abc|def)(??{"$^R"})$#,
271 "1 2 abc",
272 "1 2 abc",
273 "\$1 = abc, \$2 = undef, \$3 = undef, \$4 = undef, \$5 = undef",
274 ],
275 [
276 "123abc3",
277 qr#^(??{$re5})(|a(b)c|def)(??{"$^R"})$#,
278 "1 2 abc",
279 "1 2 b",
280 "\$1 = abc, \$2 = b, \$3 = undef, \$4 = undef, \$5 = undef",
281 ],
282 [
283 "1234",
284 qr#^((\d+)((??{push @ctl_n, $f->($^N); push @plus, $f->($+);$^N + 1}))((??{push @ctl_n, $f->($^N); push @plus, $f->($+);$^N + 1}))((??{push @ctl_n, $f->($^N); push @plus, $f->($+);$^N + 1})))$#,
285 "1234 123 12 1 2 3 1234",
286 "1234 123 12 1 2 3 4",
287 "\$1 = 1234, \$2 = 1, \$3 = 2, \$4 = 3, \$5 = 4",
288 ],
289 [
290 "1234556",
291 qr#^(\d+)($re6)($re6)($re6)$re6(($re6)$re6)$#,
292 "1234556 123455 12345 1234 123 12 1 2 3 4 4 5 56",
293 "1234556 123455 12345 1234 123 12 1 2 3 4 4 5 5",
294 "\$1 = 1, \$2 = 2, \$3 = 3, \$4 = 4, \$5 = 56",
295 ],
296 [
297 "12345562",
298 qr#^((??{$re8}))($re7)($re7)($re7)$re7($re7)($re7(\2))$#,
299 "12345562 1234556 123455 12345 1234 123 12 1 2 3 4 4 5 62",
300 "12345562 1234556 123455 12345 1234 123 12 1 2 3 4 4 5 2",
301 "\$1 = 1, \$2 = 2, \$3 = 3, \$4 = 4, \$5 = 5",
302 ],
303 ) {
304 $c++;
305 @ctl_n = ();
306 @plus = ();
307 undef $^R;
308 my $match = $test->[0] =~ $test->[1];
309 my $str = join(", ", '$1 = '.$f->($1), '$2 = '.$f->($2), '$3 = '.$f->($3), '$4 = '.$f->($4),'$5 = '.$f->($5));
310 push @ctl_n, $f->($^N);
311 push @plus, $f->($+);
bb535cf1 312 ok($match, "match $c; Bug 56194");
0f289c68
YO
313 if (not $match) {
314 # unset $str, @ctl_n and @plus
315 $str = "";
316 @ctl_n = @plus = ();
317 }
bb535cf1
NC
318 is("@ctl_n", $test->[2], "ctl_n $c; Bug 56194");
319 is("@plus", $test->[3], "plus $c; Bug 56194");
320 is($str, $test->[4], "str $c; Bug 56194");
0f289c68 321 }
0f289c68 322
1008bb63 323 {
0f289c68
YO
324 @ctl_n = ();
325 @plus = ();
326
327 our $re4;
328 local $re4 = qr#(1)((??{push @ctl_n, $f->($^N); push @plus, $f->($+);$^N + 1})){2}(?{$^N})(|abc|def)(??{"$^R"})#;
329 undef $^R;
330 my $match = "123abc3" =~ m/^(??{$re4})$/;
331 my $str = join(", ", '$1 = '.$f->($1), '$2 = '.$f->($2), '$3 = '.$f->($3), '$4 = '.$f->($4),'$5 = '.$f->($5),'$^R = '.$f->($^R));
332 push @ctl_n, $f->($^N);
333 push @plus, $f->($+);
4b0d13b9 334 ok($match, 'Bug 56194');
0f289c68
YO
335 if (not $match) {
336 # unset $str
337 @ctl_n = ();
338 @plus = ();
339 $str = "";
340 }
4b0d13b9
NC
341 is("@ctl_n", "1 2 undef", 'Bug 56194');
342 is("@plus", "1 2 undef", 'Bug 56194');
343 is($str,
4b22688e
YO
344 "\$1 = undef, \$2 = undef, \$3 = undef, \$4 = undef, \$5 = undef, \$^R = 3",
345 'Bug 56194 ($^R tweaked by 121070)');
346 }
347 {
348 undef $^R;
349 "abcd"=~/(?<Char>.)(?&Char)(?{ 42 })/;
350 is("$^R", 42, 'Bug 121070 - use of (?&Char) should not clobber $^R');
351 "abcd"=~/(?<Char>.)(?&Char)(?{ 42 })(?{ 43 })/;
352 is("$^R", 43, 'related to 121070 - use of (?&Char) should not clobber $^R');
0f289c68
YO
353 }
354 }
355
d3cec5e5
DM
356 {
357 # re evals within \U, \Q etc shouldn't be seen by the lexer
358 local our $a = "i";
359 local our $B = "J";
360 ok('(?{1})' =~ /^\Q(?{1})\E$/, '\Q(?{1})\E');
361 ok('(?{1})' =~ /^\Q(?{\E1\}\)$/, '\Q(?{\E1\}\)');
1008bb63
DM
362 eval {/^\U(??{"$a\Ea"})$/ }; norun('^\U(??{"$a\Ea"})$ norun');
363 eval {/^\L(??{"$B\Ea"})$/ }; norun('^\L(??{"$B\Ea"})$ norun');
d3cec5e5
DM
364 use re 'eval';
365 ok('Ia' =~ /^\U(??{"$a\Ea"})$/, '^\U(??{"$a\Ea"})$');
366 ok('ja' =~ /^\L(??{"$B\Ea"})$/, '^\L(??{"$B\Ea"})$');
367 }
368
a39c66b9
DM
369 {
370 # Comprehensive (hopefully) tests of closure behaviour:
371 # i.e. when do (?{}) blocks get (re)compiled, and what instances
372 # of lexical vars do they close over?
373
947535e3
DM
374 # if the pattern string gets utf8 upgraded while concatenating,
375 # make sure a literal code block is still detected (by still
376 # compiling in the absence of use re 'eval')
377
378 {
379 my $s1 = "\x{80}";
380 my $s2 = "\x{100}";
381 ok("\x{80}\x{100}" =~ /^$s1(?{1})$s2$/, "utf8 upgrade");
382 }
383
a39c66b9
DM
384 my ($cr1, $cr2, $cr3, $cr4);
385
a39c66b9
DM
386 for my $x (qw(a b c)) {
387 my $bc = ($x ne 'a');
2bd8e0da 388 my $c80 = chr(0x80);
a39c66b9
DM
389
390 # the most basic: literal code should be in same scope
391 # as the parent
392
2bd8e0da
DM
393 ok("A$x" =~ /^A(??{$x})$/, "[$x] literal code");
394 ok("\x{100}$x" =~ /^\x{100}(??{$x})$/, "[$x] literal code UTF8");
a39c66b9
DM
395
396 # the "don't recompile if pattern unchanged" mechanism
397 # shouldn't apply to code blocks - recompile every time
398 # to pick up new instances of variables
399
2bd8e0da
DM
400 my $code1 = 'B(??{$x})';
401 my $code1u = $c80 . "\x{100}" . '(??{$x})';
1008bb63
DM
402
403 eval {/^A$code1$/};
404 norun("[$x] unvarying runtime code AA norun");
405 eval {/^A$code1u$/};
406 norun("[$x] unvarying runtime code AU norun");
407 eval {/^$c80\x{100}$code1$/};
408 norun("[$x] unvarying runtime code UA norun");
409 eval {/^$c80\x{101}$code1u$/};
410 norun("[$x] unvarying runtime code UU norun");
411
412 {
413 use re 'eval';
414 ok("AB$x" =~ /^A$code1$/, "[$x] unvarying runtime code AA");
415 ok("A$c80\x{100}$x" =~ /^A$code1u$/,
416 "[$x] unvarying runtime code AU");
417 ok("$c80\x{100}B$x" =~ /^$c80\x{100}$code1$/,
418 "[$x] unvarying runtime code UA");
419 ok("$c80\x{101}$c80\x{100}$x" =~ /^$c80\x{101}$code1u$/,
420 "[$x] unvarying runtime code UU");
421 }
a39c66b9
DM
422
423 # mixed literal and run-time code blocks
424
2bd8e0da
DM
425 my $code2 = 'B(??{$x})';
426 my $code2u = $c80 . "\x{100}" . '(??{$x})';
1008bb63
DM
427
428 eval {/^A(??{$x})-$code2$/};
429 norun("[$x] literal+runtime AA norun");
430 eval {/^A(??{$x})-$code2u$/};
431 norun("[$x] literal+runtime AU norun");
432 eval {/^$c80\x{100}(??{$x})-$code2$/};
433 norun("[$x] literal+runtime UA norun");
434 eval {/^$c80\x{101}(??{$x})-$code2u$/};
435 norun("[$x] literal+runtime UU norun");
436
437 {
438 use re 'eval';
439 ok("A$x-B$x" =~ /^A(??{$x})-$code2$/,
440 "[$x] literal+runtime AA");
441 ok("A$x-$c80\x{100}$x" =~ /^A(??{$x})-$code2u$/,
442 "[$x] literal+runtime AU");
443 ok("$c80\x{100}$x-B$x" =~ /^$c80\x{100}(??{$x})-$code2$/,
444 "[$x] literal+runtime UA");
445 ok("$c80\x{101}$x-$c80\x{100}$x"
446 =~ /^$c80\x{101}(??{$x})-$code2u$/,
447 "[$x] literal+runtime UU");
448 }
fdfac248 449
a39c66b9
DM
450 # literal qr code only created once, naked
451
452 $cr1 //= qr/^A(??{$x})$/;
d63c20f2 453 ok("Aa" =~ $cr1, "[$x] literal qr once naked");
a39c66b9
DM
454
455 # literal qr code only created once, embedded with text
456
457 $cr2 //= qr/B(??{$x})$/;
346d3070 458 ok("ABa" =~ /^A$cr2/, "[$x] literal qr once embedded text");
a39c66b9
DM
459
460 # literal qr code only created once, embedded with text + lit code
461
462 $cr3 //= qr/C(??{$x})$/;
346d3070 463 ok("A$x-BCa" =~ /^A(??{$x})-B$cr3/,
a39c66b9
DM
464 "[$x] literal qr once embedded text + lit code");
465
466 # literal qr code only created once, embedded with text + run code
467
468 $cr4 //= qr/C(??{$x})$/;
469 my $code3 = 'A(??{$x})';
fbf5ab7b 470
1008bb63
DM
471 eval {/^$code3-B$cr4/};
472 norun("[$x] literal qr once embedded text + run code norun");
473 {
474 use re 'eval';
475 ok("A$x-BCa" =~ /^$code3-B$cr4/,
a39c66b9 476 "[$x] literal qr once embedded text + run code");
1008bb63 477 }
a39c66b9
DM
478
479 # literal qr code, naked
480
481 my $r1 = qr/^A(??{$x})$/;
d63c20f2 482 ok("A$x" =~ $r1, "[$x] literal qr naked");
a39c66b9
DM
483
484 # literal qr code, embedded with text
485
486 my $r2 = qr/B(??{$x})$/;
629cd4f3 487 ok("AB$x" =~ /^A$r2/, "[$x] literal qr embedded text");
a39c66b9
DM
488
489 # literal qr code, embedded with text + lit code
490
491 my $r3 = qr/C(??{$x})$/;
629cd4f3 492 ok("A$x-BC$x" =~ /^A(??{$x})-B$r3/,
a39c66b9
DM
493 "[$x] literal qr embedded text + lit code");
494
495 # literal qr code, embedded with text + run code
496
497 my $r4 = qr/C(??{$x})$/;
498 my $code4 = '(??{$x})';
fbf5ab7b 499
1008bb63
DM
500 eval {/^A$code4-B$r4/};
501 norun("[$x] literal qr embedded text + run code");
fbf5ab7b 502 {
1008bb63
DM
503 use re 'eval';
504 ok("A$x-BC$x" =~ /^A$code4-B$r4/,
505 "[$x] literal qr embedded text + run code");
fbf5ab7b
DM
506 }
507
a39c66b9
DM
508 # nested qr in different scopes
509
510 my $code5 = '(??{$x})';
629cd4f3 511 my $r5 = qr/C(??{$x})/;
fbf5ab7b 512
1008bb63
DM
513 my $r6;
514 eval {qr/$code5-C(??{$x})/}; norun("r6 norun");
515 {
516 use re 'eval';
517 $r6 = qr/$code5-C(??{$x})/;
518 }
a39c66b9
DM
519
520 my @rr5;
521 my @rr6;
522
523 for my $y (qw(d e f)) {
524
525 my $rr5 = qr/^A(??{"$x$y"})-$r5/;
526 push @rr5, $rr5;
629cd4f3 527 ok("A$x$y-C$x" =~ $rr5,
a39c66b9
DM
528 "[$x-$y] literal qr + r5");
529
530 my $rr6 = qr/^A(??{"$x$y"})-$r6/;
531 push @rr6, $rr6;
629cd4f3 532 ok("A$x$y-$x-C$x" =~ $rr6,
a39c66b9
DM
533 "[$x-$y] literal qr + r6");
534 }
535
536 for my $i (0,1,2) {
537 my $y = 'Y';
538 my $yy = (qw(d e f))[$i];
539 my $rr5 = $rr5[$i];
629cd4f3
DM
540 ok("A$x$yy-C$x" =~ $rr5, "[$x-$yy] literal qr + r5, outside");
541 ok("A$x$yy-C$x-D$x" =~ /$rr5-D(??{$x})$/,
a39c66b9
DM
542 "[$x-$yy] literal qr + r5 + lit, outside");
543
629cd4f3 544
a39c66b9
DM
545 my $rr6 = $rr6[$i];
546 push @rr6, $rr6;
629cd4f3 547 ok("A$x$yy-$x-C$x" =~ $rr6,
a39c66b9 548 "[$x-$yy] literal qr + r6, outside");
629cd4f3 549 ok("A$x$yy-$x-C$x-D$x" =~ /$rr6-D(??{$x})/,
a39c66b9
DM
550 "[$x-$yy] literal qr + r6 +lit, outside");
551 }
552 }
68e2671b
DM
553
554 # recursive subs should get lexical from the correct pad depth
555
556 sub recurse {
557 my ($n) = @_;
558 return if $n > 2;
559 ok("A$n" =~ /^A(??{$n})$/, "recurse($n)");
560 recurse($n+1);
561 }
562 recurse(0);
d63c20f2
DM
563
564 # for qr// containing run-time elements but with a compile-time
565 # code block, make sure the run-time bits are executed in the same
566 # pad they were compiled in
567 {
568 my $a = 'a'; # ensure outer and inner pads don't align
569 my $b = 'b';
570 my $c = 'c';
571 my $d = 'd';
572 my $r = qr/^$b(??{$c})$d$/;
573 ok("bcd" =~ $r, "qr with run-time elements and code block");
574 }
575
b30fcab9
DM
576 # check that cascaded embedded regexes all see their own lexical
577 # environment
578
579 {
580 my ($r1, $r2, $r3, $r4);
581 my ($x1, $x2, $x3, $x4) = (5,6,7,8);
582 { my $x1 = 1; $r1 = qr/A(??{$x1})/; }
583 { my $x2 = 2; $r2 = qr/$r1(??{$x2})/; }
584 { my $x3 = 3; $r3 = qr/$r2(??{$x3})/; }
585 { my $x4 = 4; $r4 = qr/$r3(??{$x4})/; }
586 ok("A1234" =~ /^$r4$/, "cascaded qr");
587 }
588
589 # and again, but in a loop, with no external references
590 # being maintained to the qr's
591
592 {
593 my $r = 'A';
594 for my $x (1..4) {
595 $r = qr/$r(??{$x})/;
596 }
597 my $x = 5;
598 ok("A1234" =~ /^$r$/, "cascaded qr loop");
599 }
600
601
602 # and again, but compiling the qrs in an eval so there
603 # aren't even refs to the qrs from any ops
604
605 {
606 my $r = 'A';
607 for my $x (1..4) {
608 $r = eval q[ qr/$r(??{$x})/; ];
609 }
610 my $x = 5;
611 ok("A1234" =~ /^$r$/, "cascaded qr loop");
612 }
613
87c6a48b
DM
614 # have qrs with either literal code blocks or only embedded
615 # code blocks, but not both
616
617 {
618 my ($r1, $r2, $r3, $r4);
619 my ($x1, $x3) = (7,8);
620 { my $x1 = 1; $r1 = qr/A(??{$x1})/; }
621 { $r2 = qr/${r1}2/; }
622 { my $x3 = 3; $r3 = qr/$r2(??{$x3})/; }
623 { $r4 = qr/${r3}4/; }
624 ok("A1234" =~ /^$r4$/, "cascaded qr mix 1");
625 ok("A12345" =~ /^${r4}5$/, "cascaded qr mix 2");
626 ok("A1234" =~ qr/^$r4$/ , "cascaded qr mix 3");
627 ok("A12345" =~ qr/^${r4}5$/, "cascaded qr mix 4");
628 }
629
630 # and make sure things are freed at the right time
1b54b9b1
DIM
631 {
632 sub Foo99::DESTROY { $Foo99::d++ }
633 $Foo99::d = 0;
634 my $r1;
87c6a48b 635 {
1b54b9b1
DIM
636 my $x = bless [1], 'Foo99';
637 $r1 = eval 'qr/(??{$x->[0]})/';
87c6a48b 638 }
1b54b9b1
DIM
639 my $r2 = eval 'qr/a$r1/';
640 my $x = 2;
641 ok(eval '"a1" =~ qr/^$r2$/', "match while in scope");
642 # make sure PL_reg_curpm isn't holding on to anything
643 "a" =~ /a(?{1})/;
644 is($Foo99::d, 0, "before scope exit");
87c6a48b 645 }
1b54b9b1 646 ::is($Foo99::d, 1, "after scope exit");
87c6a48b 647
d63c20f2
DM
648 # forward declared subs should Do The Right Thing with any anon CVs
649 # within them (i.e. pad_fixup_inner_anons() should work)
650
651 sub forward;
652 sub forward {
653 my $x = "a";
654 my $A = "A";
655 ok("Aa" =~ qr/^A(??{$x})$/, "forward qr compiletime");
656 ok("Aa" =~ qr/^$A(??{$x})$/, "forward qr runtime");
657 }
658 forward;
a39c66b9
DM
659 }
660
d24ca0c5
DM
661 # test that run-time embedded code, when re-fed into toker,
662 # does all the right escapes
663
664 {
d24ca0c5
DM
665 my $enc = eval 'use Encode; find_encoding("ascii")';
666
667 my $x = 0;
668 my $y = 'bad';
669
670 # note that most of the strings below are single-quoted, and the
671 # things within them, like '$y', *aren't* intended to interpolate
672
673 my $s1 =
674 'a\\$y(?# (??{BEGIN{$x=1} "X1"})b(?# \Ux2\E)c\'d\\\\e\\\\Uf\\\\E';
675
676 ok(q{a$ybc'd\e\Uf\E} =~ /^$s1$/, "reparse");
677 is($x, 0, "reparse no BEGIN");
678
679 my $s2 = 'g\\$y# (??{{BEGIN{$x=2} "X3"}) \Ux3\E' . "\nh";
680
681 ok(q{a$ybc'd\\e\\Uf\\Eg$yh} =~ /^$s1$s2$/x, "reparse /x");
682 is($x, 0, "reparse /x no BEGIN");
683
684 my $b = '\\';
685 my $q = '\'';
686
687 # non-ascii in string as "<0xNNN>"
688 sub esc_str {
689 my $s = shift;
690 $s =~ s{(.)}{
691 my $c = ord($1);
692 ($c< 32 || $c > 127) ? sprintf("<0x%x>", $c) : $1;
693 }ge;
694 $s;
695 }
696 sub fmt { sprintf "hairy backslashes %s [%s] =~ /^%s/",
697 $_[0], esc_str($_[1]), esc_str($_[2]);
698 }
699
700
701 for my $u (
702 [ '', '', 'blank ' ],
703 [ "\x{100}", '\x{100}', 'single' ],
704 [ "\x{100}", "\x{100}", 'double' ])
705 {
706 for my $pair (
707 [ "$b", "$b$b" ],
708 [ "$q", "$q" ],
709 [ "$b$q", "$b$b$b$q" ],
710 [ "$b$b$q", "$b$b$b$b$q" ],
711 [ "$b$b$b$q", "$b$b$b$b$b$b$q" ],
712 [ "$b$b$b$b$q","$b$b$b$b$b$b$b$b$q" ],
713 ) {
714 my ($s, $r) = @$pair;
715 $s = "9$s";
716 my $ss = "$u->[0]$s";
717
718 my $c = '9' . $r;
719 my $cc = "$u->[1]$c";
1008bb63
DM
720
721 ok($ss =~ /^$cc/, fmt("plain $u->[2]", $ss, $cc));
d24ca0c5
DM
722
723 no strict;
724 my $chr41 = "\x41";
725 $ss = "$u->[0]\t${q}$chr41${b}x42$s";
726 $nine = $nine = "bad";
727 for my $use_qr ('', 'qr') {
728 $c = qq[(??{my \$z='{';]
729 . qq[$use_qr"$b${b}t$b$q$b${b}x41$b$b$b${b}x42"]
730 . qq[. \$nine})];
731 # (??{ qr/str/ }) goes through one less interpolation
732 # stage than (??{ qq/str/ })
733 $c =~ s{\\\\}{\\}g if ($use_qr eq 'qr');
734 $c .= $r;
735 $cc = "$u->[1]$c";
736 my $nine = 9;
1008bb63
DM
737
738 eval {/^$cc/}; norun(fmt("code norun $u->[2]", $ss, $cc));
739 {
740 use re 'eval';
741 ok($ss =~ /^$cc/, fmt("code $u->[2]", $ss, $cc));
742 }
743
d24ca0c5
DM
744 {
745 # Poor man's "use encoding 'ascii'".
746 # This causes a different code path in S_const_str()
747 # to be used
748 local ${^ENCODING} = $enc;
1008bb63
DM
749 use re 'eval';
750 ok($ss =~ /^$cc/, fmt("encode $u->[2]", $ss, $cc));
d24ca0c5
DM
751 }
752 }
753 }
754 }
755
756 my $code1u = "(??{qw(\x{100})})";
1008bb63
DM
757 eval {/^$code1u$/}; norun("reparse embeded unicode norun");
758 {
759 use re 'eval';
760 ok("\x{100}" =~ /^$code1u$/, "reparse embeded unicode");
761 }
d24ca0c5
DM
762 }
763
764 # a non-pattern literal won't get code blocks parsed at compile time;
765 # but they must get parsed later on if 'use re eval' is in scope
766 # also check that unbalanced {}'s are parsed ok
767
768 {
1008bb63
DM
769 eval q["a{" =~ '^(??{"a{"})$'];
770 norun("non-pattern literal code norun");
771 eval {/^${\'(??{"a{"})'}$/};
772 norun("runtime code with unbalanced {} norun");
773
d24ca0c5 774 use re 'eval';
2f71aec6
DM
775 ok("a{" =~ '^a(??{"{"})$', "non-pattern literal code");
776 ok("a{" =~ /^a${\'(??{"{"})'}$/, "runtime code with unbalanced {}");
d24ca0c5
DM
777 }
778
2e2e3f36
DM
779 # make sure warnings come from the right place
780
781 {
782 use warnings;
783 my ($s, $t, $w);
784 local $SIG{__WARN__} = sub { $w .= "@_" };
785
786 $w = ''; $s = 's';
787 my $r = qr/(?{$t=$s+1})/;
788 "a" =~ /a$r/;
789 like($w, qr/pat_re_eval/, "warning main file");
790
791 # do it in an eval to get predictable line numbers
792 eval q[
793
794 $r = qr/(?{$t=$s+1})/;
795 ];
796 $w = ''; $s = 's';
797 "a" =~ /a$r/;
798 like($w, qr/ at \(eval \d+\) line 3/, "warning eval A");
799
800 $w = ''; $s = 's';
801 eval q[
802 use re 'eval';
803 my $c = '(?{$t=$s+1})';
804 "a" =~ /a$c/;
805 1;
806 ];
807 like($w, qr/ at \(eval \d+\) line 1/, "warning eval B");
808 }
809
81ed78b2
DM
810 # jumbo test for:
811 # * recursion;
812 # * mixing all the different types of blocks (literal, qr/literal/,
813 # runtime);
814 # * backtracking (the Z+ alternation ensures CURLYX and full
815 # scope popping on backtracking)
816
817 {
818 sub recurse2 {
819 my ($depth)= @_;
820 return unless $depth;
821 my $s1 = '3-LMN';
822 my $r1 = qr/(??{"$s1-$depth"})/;
823
824 my $s2 = '4-PQR';
825 my $c1 = '(??{"$s2-$depth"})';
826 use re 'eval';
827 ok( "<12345-ABC-$depth-123-LMN-$depth-1234-PQR-$depth>"
828 . "<12345-ABC-$depth-123-LMN-$depth-1234-PQR-$depth>"
829 =~
830 /^<(\d|Z+)+(??{"45-ABC-$depth-"})(\d|Z+)+$r1-\d+$c1>
831 <(\d|Z+)+(??{"45-ABC-$depth-"})(\d|Z+)+$r1-\d+$c1>$/x,
832 "recurse2($depth)");
833 recurse2($depth-1);
834 }
835 recurse2(5);
836 }
837
74088413
DM
838 # nested (??{}) called from various levels of a recursive function
839
840 {
841 sub recurse3 {
842 my ($n) = @_;
843 return if $n > 3;
844 ok("A$n" =~ m{^A(??{ "0123" =~ /((??{$n}))/; $1 })$},
845 "recurse3($n)");
846 ok("A$n" !~ m{^A(??{ "0123" =~ /((??{$n}))/; "X" })$},
847 "recurse3($n) nomatch");
848 recurse3($n+1);
849 }
850 recurse3(0);
851 }
852
853 # nested (??{}) being invoked recursively via a function
854
855 {
856 my $s = '';
857 our $recurse4;
858 my @alpha = qw(A B C D E);
859 $recurse4 = sub {
860 my ($n) = @_;
861 $s .= "(n=$n:";
862 if ($n < 4) {
863 my $m = ("$alpha[$n]" . substr("0123", 0, $n+1)) =~
864 m{^([A-Z])
865 (??{
866 $s .= "1=$1:";
867 "$n-0123" =~ m{^(\d)-(((??{$recurse4->($n+1)})))};
868 $s .= "i1=$1:<=[$2]";
869 $3; # NB - not stringified
870 })
871 $
872 }x;
873 $s .= "1a=$1:";
874 $s .= $m ? 'M' : '!M';
875 }
876 my $ret = '.*?' . ($n-1);
877 $s .= "<=[$ret])";
878 return $ret;
879 };
880 $recurse4->(0);
881 my $exp = '(n=0:1=A:(n=1:1=B:(n=2:1=C:(n=3:1=D:(n=4:<=[.*?3])'
882 . 'i1=3:<=[0123]1a=D:M<=[.*?2])i1=2:<=[012]1a=C:M<=[.*?1])'
883 . 'i1=1:<=[01]1a=B:M<=[.*?0])i1=0:<=[0]1a=A:M<=[.*?-1])';
884 is($s, $exp, 'recurse4');
885 }
886
887 # single (??{}) being invoked recursively via a function
888
889 {
890 my $s = '';
891 our $recurse5;
892 my @alpha = qw(A B C D E);
893 $recurse5 = sub {
894 my ($n) = @_;
895 $s .= "(n=$n:";
896 if ($n < 4) {
897 my $m = ("$alpha[$n]" . substr("0123", 0, $n+1)) =~
898 m{^([A-Z])
899 ((??{
900 $s .= "1=$1:";
901 $recurse5->($n+1);
902 }))
903 $
904 }x;
905 $s .= "1a=$1:2=$2:";
906 $s .= $m ? 'M' : '!M';
907 }
908 my $ret = '.*?' . ($n-1);
909 $s .= "<=[$ret])";
910 return $ret;
911 };
912 $recurse5->(0);
913 my $exp = '(n=0:1=A:(n=1:1=B:(n=2:1=C:(n=3:1=D:(n=4:<=[.*?3])'
914 . '1a=D:2=0123:M<=[.*?2])1a=C:2=012:M<=[.*?1])'
915 . '1a=B:2=01:M<=[.*?0])1a=A:2=0:M<=[.*?-1])';
916 is($s, $exp, 'recurse5');
917 }
918
919
81ed78b2
DM
920 # make sure that errors during compiling run-time code get trapped
921
922 {
923 use re 'eval';
924
925 my $code = '(?{$x=})';
926 eval { "a" =~ /^a$code/ };
927 like($@, qr/syntax error at \(eval \d+\) line \d+/, 'syntax error');
928
929 $code = '(?{BEGIN{die})';
930 eval { "a" =~ /^a$code/ };
931 like($@,
932 qr/BEGIN failed--compilation aborted at \(eval \d+\) line \d+/,
933 'syntax error');
ce4bd586
BF
934
935 use utf8;
936 $code = '(?{Foo::$bar})';
937 eval { "a" =~ /^a$code/ };
938 like($@, qr/Bad name after Foo:: at \(eval \d+\) line \d+/, 'UTF8 sytax error');
81ed78b2
DM
939 }
940
732caac7
DM
941 # make sure that 'use re eval' is propagated into compiling the
942 # pattern returned by (??{})
943
944 {
945 use re 'eval';
946 my $pat = 'B(??{1})C';
947 my $A = 'A';
948 # compile-time outer code-block
949 ok("AB1CD" =~ /^A(??{$pat})D$/, "re eval propagated compile-time");
950 # run-time outer code-block
951 ok("AB1CD" =~ /^$A(??{$pat})D$/, "re eval propagated run-time");
952 }
953
197e8e6e
DM
954 # returning a ref to something that had set magic but wasn't
955 # PERL_MAGIC_qr triggered a false positive assertion failure
956 # The test is not so much concerned with it not matching,
957 # as with not failing the assertion
958
959 {
960 ok("a" !~ /^(a)(??{ \$1 })/, '(??{ ref })');
961 }
962
e4bfbed3
DM
963 # make sure the uninit warning from returning an undef var
964 # sees the right var
965
966 {
967 my ($u1, $u2);
968 my $warn = '';
969 local $SIG{__WARN__} = sub { $warn .= $_[0] };
970 $u1 =~ /(??{$u2})/ or die;
971 like($warn, qr/value \$u1 in pattern match.*\n.*value at/, 'uninit');
972 }
973
86464c5b
DM
974 # test that code blocks are called in scalar context
975
976 {
977 my @a = (0);
978 ok("" =~ /^(?{@a})$/, '(?{}) in scalar context');
979 is($^R, 1, '(?{}) in scalar context: $^R');
980 ok("1" =~ /^(??{@a})$/, '(??{}) in scalar context');
981 ok("foo" =~ /^(?(?{@a})foo|bar)$/, '(?(?{})|) in scalar context');
982 }
983
30e92347
DM
984 # BEGIN in compiled blocks shouldn't mess with $1 et al
985
986 {
987 use re 'eval';
988 my $code1 = '(B)(??{ BEGIN { "X" =~ /X/ } $1})(C)';
989 ok("ABBCA" =~ /^(.)(??{$code1})\1$/, '(?{}) BEGIN and $1');
990 my $code2 = '(B)(??{ BEGIN { "X" =~ /X/ } $1 =~ /(.)/ ? $1 : ""})(C)';
991 ok("ABBCA" =~ /^(.)(??{$code2})\1$/, '(?{}) BEGIN and $1 mark 2');
992 }
e4bfbed3 993
41953474
DM
994 # check that the optimiser is applied to code blocks: see if aelem has
995 # been converted to aelemfast
996
997 {
998 my $out;
999 for my $prog (
1000 '/(?{$a[0]})/',
1001 'q() =~ qr/(?{$a[0]})/',
1002 'use re q(eval); q() =~ q{(?{$a[0]})}',
1003 'use re q(eval); $c = q{(?{$a[0]})}; /$c/',
1004 'use re q(eval); $c = q{(?{$a[0]})}; /(?{1;})$c/',
1005 ) {
1006 $out = runperl(switches => ["-Dt"], prog => $prog, stderr => 1);
1007 like($out, qr/aelemfast|Recompile perl with -DDEBUGGING/,
1008 "optimise: '$prog'");
1009 }
1010 }
1011
d234778b
DM
1012 # [perl #115080]
1013 # Ensure that ?pat? matches exactly once, even when the run-time
1014 # pattern changes, and even when the presence of run-time (?{}) affects
1015 # how and when patterns are recompiled
41953474 1016
d234778b
DM
1017 {
1018 my $m;
1019
1020 $m = '';
1021 for (qw(a a a)) {
1022 $m .= $_ if m?$_?;
1023 }
1024 is($m, 'a', '?pat? with a,a,a');
1025
1026 $m = '';
1027 for (qw(a b c)) {
1028 $m .= $_ if m?$_?;
1029 }
1030 is($m, 'a', '?pat? with a,b,c');
1031
1032 use re 'eval';
1033
1034 $m = '';
1035 for (qw(a a a)) {
1036 my $e = qq[(??{"$_"})];
1037 $m .= $_ if m?$e?;
1038 }
1039 is($m, 'a', '?pat? with (??{a,a,a})');
1040
1041 $m = '';
1042 for (qw(a b c)) {
1043 my $e = qq[(??{"$_"})];
1044 $m .= $_ if m?$e?;
1045 }
1046 is($m, 'a', '?pat? with (??{a,b,c})');
1047 }
41953474 1048
da1f5b59
DM
1049 {
1050 # this code won't actually fail, but it used to fail valgrind,
1051 # so its here just to make sure valgrind doesn't fail again
1052 # While examining the ops of the secret anon sub wrapped around
1053 # the qr//, the pad of the sub was in scope, so cSVOPo_sv
1054 # got the const from the wrong pad. By having lots of $s's
1055 # (aka gvsv(*s), this forces the targs of the consts which have
1056 # been moved to the pad, to have high indices.
1057
1058 sub {
1059 local our $s = "abc";
1060 my $qr = qr/^(?{1})$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s/;
1061 }->();
1062 pass("cSVOPo_sv");
1063 }
1064
491453ba
DM
1065 # [perl #115004]
1066 # code blocks in qr objects that are interpolated in arrays need
1067 # handling the same as if they were interpolated from scalar vars
1068 # (before this code would need 'use re "eval"')
1069
1070 {
1071 use Tie::Array;
1072
1073 use vars '@global';
1074 local @global;
1075 my @array;
1076 my @refs = (0, \@array, 2);
1077 my @tied;
1078 tie @tied, 'Tie::StdArray';
1079 {
1080 my $bb = 'B';
1081 my $dd = 'D';
1082 @array = ('A', qr/(??{$bb})/, 'C', qr/(??{$dd})/, 'E');
1083 @tied = @array;
1084 @global = @array;
1085 }
1086 my $bb = 'X';
1087 my $dd = 'Y';
1088 ok("A B C D E=" =~ /@array/, 'bare interpolated array match');
1089 ok("A B C D E=" =~ qr/@array/, 'qr bare interpolated array match');
1090 ok("A B C D E=" =~ /@global/, 'bare interpolated global array match');
1091 ok("A B C D E=" =~ qr/@global/,
1092 'qr bare interpolated global array match');
1093 ok("A B C D E=" =~ /@{$refs[1]}/, 'bare interpolated ref array match');
1094 ok("A B C D E=" =~ qr/@{$refs[1]}/,
1095 'qr bare interpolated ref array match');
1096 ok("A B C D E=" =~ /@tied/, 'bare interpolated tied array match');
1097 ok("A B C D E=" =~ qr/@tied/, 'qr bare interpolated tied array match');
1098 ok("aA B C D E=" =~ /^a@array=$/, 'interpolated array match');
1099 ok("aA B C D E=" =~ qr/^a@array=$/, 'qr interpolated array match');
1100 ok("aA B C D E=" =~ /^a@global=$/, 'interpolated global array match');
1101 ok("aA B C D E=" =~ qr/^a@global=$/,
1102 'qr interpolated global array match');
1103 ok("aA B C D E=" =~ /^a@{$refs[1]}=$/, 'interpolated ref array match');
1104 ok("aA B C D E=" =~ qr/^a@{$refs[1]}=$/,
1105 'qr interpolated ref array match');
1106 ok("aA B C D E=" =~ /^a@tied=$/, 'interpolated tied array match');
1107 ok("aA B C D E=" =~ qr/^a@tied=$/, 'qr interpolated tied array match');
1108
1109 {
1110 local $" = '-';
1111 ok("aA-B-C-D-E=" =~ /^a@{array}=$/,
1112 'interpolated array match with local sep');
1113 ok("aA-B-C-D-E=" =~ qr/^a@{array}=$/,
1114 'qr interpolated array match with local sep');
1115 ok("aA-B-C-D-E=" =~ /^a@{global}=$/,
1116 'interpolated global array match with local sep');
1117 ok("aA-B-C-D-E=" =~ qr/^a@{global}=$/,
1118 'qr interpolated global array match with local sep');
1119 ok("aA-B-C-D-E=" =~ /^a@{tied}=$/,
1120 'interpolated tied array match with local sep');
1121 ok("aA-B-C-D-E=" =~ qr/^a@{tied}=$/,
1122 'qr interpolated tied array match with local sep');
1123 }
1124
1125 # but don't handle the array ourselves in the presence of \Q etc
1126
1127 @array = ('A', '(?{})');
1128 @global = @array;
1129 @tied = @array;
1130 ok("aA (?{})=" =~ /^a\Q@{array}\E=$/,
1131 'interpolated array match with \Q');
1132 ok("aA (?{})=" =~ qr/^a\Q@{array}\E=$/,
1133 'qr interpolated array match with \Q');
1134 ok("aA (?{})=" =~ /^a\Q@{global}\E=$/,
1135 'interpolated global array match with \Q');
1136 ok("aA (?{})=" =~ qr/^a\Q@{global}\E=$/,
1137 'qr interpolated global array match with \Q');
1138 ok("aA (?{})=" =~ /^a\Q@{$refs[1]}\E=$/,
1139 'interpolated ref array match with \Q');
1140 ok("aA (?{})=" =~ qr/^a\Q@{$refs[1]}\E=$/,
1141 'qr interpolated ref array match with \Q');
1142 ok("aA (?{})=" =~ /^a\Q@{tied}\E=$/,
1143 'interpolated tied array match with \Q');
1144 ok("aA (?{})=" =~ qr/^a\Q@{tied}\E=$/,
1145 'qr interpolated tied array match with \Q');
1146
1147 # and check it works with an empty array
1148
1149 @array = ();
1150 @global = ();
1151 @tied = ();
1152 ok("a=" =~ /^a@array=$/, 'empty array match');
1153 ok("a=" =~ qr/^a@array=$/, 'qr empty array match');
1154 ok("a=" =~ /^a@global=$/, 'empty global array match');
1155 ok("a=" =~ qr/^a@global=$/, 'qr empty global array match');
1156 ok("a=" =~ /^a@tied=$/, 'empty tied array match');
1157 ok("a=" =~ qr/^a@tied=$/, 'qr empty tied array match');
1158 ok("a=" =~ /^a\Q@{array}\E=$/, 'empty array match with \Q');
1159 ok("a=" =~ /^a\Q@{array}\E=$/, 'empty array match with \Q');
1160 ok("a=" =~ qr/^a\Q@{global}\E=$/,
1161 'qr empty global array match with \Q');
1162 ok("a=" =~ /^a\Q@{tied}\E=$/, 'empty tied array match with \Q');
1163 ok("a=" =~ qr/^a\Q@{tied}\E=$/, 'qr empty tied array match with \Q');
1164
1165 # NB: these below are empty patterns, so they happen to use the
1166 # successful match from the line above
1167
1168 ok("a=" =~ /@array/, 'empty array pattern');
1169 ok("a=" =~ qr/@array/, 'qr empty array pattern');
1170 ok("a=" =~ /@global/, 'empty global array pattern');
1171 ok("a=" =~ qr/@global/, 'qr empty global array pattern');
1172 ok("a=" =~ /@tied/, 'empty tied pattern');
1173 ok("a=" =~ qr/@tied/, 'qr empty tied pattern');
1174 ok("a=" =~ /\Q@array\E/, 'empty array pattern with \Q');
1175 ok("a=" =~ qr/\Q@array\E/, 'qr empty array pattern with \Q');
1176 ok("a=" =~ /\Q@global\E/, 'empty global array pattern with \Q');
1177 ok("a=" =~ qr/\Q@global\E/, 'qr empty global array pattern with \Q');
1178 ok("a=" =~ /\Q@tied\E/, 'empty tied pattern with \Q');
1179 ok("a=" =~ qr/\Q@tied\E/, 'qr empty tied pattern with \Q');
1180 ok("a=" =~ //, 'completely empty pattern');
1181 ok("a=" =~ qr//, 'qr completely empty pattern');
1182 }
da1f5b59 1183
63620942
FC
1184 {
1185 { package o; use overload '""'=>sub { "abc" } }
1186 my $x = bless [],"o";
1187 my $y = \$x;
1188 (my $y_addr = "$y") =~ y/()//d; # REF(0x7fcb9c02) -> REF0x7fcb9c02
1189 # $y_addr =~ $y should be true, as should $y_addr =~ /(??{$y})/
1190 "abc$y_addr" =~ /(??{$x})(??{$y})/;
1191 is "$&", "abc$y_addr",
1192 '(??{$x}) does not leak cached qr to (??{\$x}) (match)';
1193 is scalar "abcabc" =~ /(??{$x})(??{$y})/, "",
1194 '(??{$x}) does not leak cached qr to (??{\$x}) (no match)';
1195 }
81ed78b2 1196
2685dc2d
FC
1197 {
1198 sub ReEvalTieTest::TIESCALAR {bless[], "ReEvalTieTest"}
1199 sub ReEvalTieTest::STORE{}
1200 sub ReEvalTieTest::FETCH { "$1" }
1201 tie my $t, "ReEvalTieTest";
1202 $t = bless [], "o";
1203 "aab" =~ /(a)((??{"b" =~ m|(.)|; $t}))/;
1204 is "[$1 $2]", "[a b]",
1205 '(??{$tied_former_overload}) sees the right $1 in FETCH';
1206 }
1207
e66ad350
FC
1208 {
1209 my @matchsticks;
1210 my $ref = bless \my $o, "o";
1211 my $foo = sub { push @matchsticks, scalar "abc" =~ /(??{$ref})/ };
1212 &$foo;
1213 bless \$o;
1214 () = "$ref"; # flush AMAGIC flag on main
1215 &$foo;
1216 is "@matchsticks", "1 ", 'qr magic is not cached on refs';
1217 }
1218
4cba5ac0
FC
1219 {
1220 my ($foo, $bar) = ("foo"x1000, "bar"x1000);
1221 "$foo$bar" =~ /(??{".*"})/;
1222 is "$&", "foo"x1000 . "bar"x1000,
1223 'padtmp swiping does not affect "$a$b" =~ /(??{})/'
1224 }
1225
0f289c68
YO
1226} # End of sub run_tests
1227
12281;