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.
20 @INC = ('../lib','.');
22 skip_all_if_miniperl("no dynamic loading on miniperl, no re");
26 plan tests => 459; # Update this when adding/deleting tests.
28 run_tests() unless caller;
30 # test that runtime code without 'use re eval' is trapped
33 like($@, qr/Eval-group not allowed at runtime/, @_);
41 my $message = "Call code from qr //";
42 local $_ = 'var="foo"';
45 ok(/$a$a/ && $b eq '9', $message);
48 ok(/$a$a/ && $b eq '11', $message);
52 norun("$message norun 1");
57 norun("$message norun 2");
60 is($b, '14', $message);
66 my $lex_res = ($lex_b =~ qr/$lex_b(?{ $lex_c = $lex_a++ })/);
68 is($lex_res, 1, $message);
69 is($lex_a, 44, $message);
70 is($lex_c, 43, $message);
74 my $match = eval { /$a$c$a$d/ };
75 ok($@ && $@ =~ /Eval-group not allowed/ && !$match, $message);
76 is($b, '14', $message);
82 $lex_res = ($lex_b =~ qr/17(?{ $lex_c = $lex_a++ })/);
84 is($lex_res, 1, $message);
85 is($lex_a, 44, $message);
86 is($lex_c, 43, $message);
91 our $a = bless qr /foo/ => 'Foo';
92 ok 'goodfood' =~ $a, "Reblessed qr // matches";
93 is($a, '(?^:foo)', "Reblessed qr // stringifies");
95 my $z = my $y = "\317\276"; # Byte representation of $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";
104 is(length qr /##/x, 9, "## in qr // doesn't corrupt memory; Bug 17776");
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";
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";
130 # Test if $^N and $+ work in (?{})
138 push @ctl_n, (defined $^N ? $^N : "undef");
139 push @plus, (defined $+ ? $+ : "undef");
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" ],
165 ) { #"#silence vim highlighting
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
177 is("@ctl_n", $test->[2], "ctl_n $c");
178 is("@plus", $test->[3], "plus $c");
186 defined $_[0] ? $_[0] : "undef";
189 like("123", qr/^(\d)(((??{1 + $^N})))+$/, 'Bug 56194');
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"})#;
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})#;
208 # Expected values of $^N
209 # Expected values of $+
210 # Expected values of $1, $2, $3, $4 and $5
214 qr#^(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))+(??{$^N})$#,
217 "\$1 = 1, \$2 = 3, \$3 = undef, \$4 = undef, \$5 = undef",
221 qr#^(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))+(abc|def|)?(??{$+})$#,
224 "\$1 = 1, \$2 = 3, \$3 = undef, \$4 = undef, \$5 = undef",
228 qr#^(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))+(|abc|def)?(??{$+})$#,
231 "\$1 = 1, \$2 = 3, \$3 = undef, \$4 = undef, \$5 = undef",
235 qr#^(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))+(abc|def|)?(??{$^N})$#,
238 "\$1 = 1, \$2 = 3, \$3 = undef, \$4 = undef, \$5 = undef",
242 qr#^(1)((??{ push @ctl_n, $f->($^N); push @plus, $f->($+); $^N + 1}))+(|abc|def)?(??{$^N})$#,
245 "\$1 = 1, \$2 = 3, \$3 = undef, \$4 = undef, \$5 = undef",
249 qr#^($re)(|a(b)c|def)(??{$^R})$#,
252 "\$1 = 123, \$2 = 1, \$3 = 3, \$4 = abc, \$5 = b",
259 "\$1 = 123abc3, \$2 = 1, \$3 = 3, \$4 = abc, \$5 = b",
266 "\$1 = 123abc3, \$2 = 1, \$3 = 3, \$4 = abc, \$5 = b",
270 qr#^(??{$re5})(|abc|def)(??{"$^R"})$#,
273 "\$1 = abc, \$2 = undef, \$3 = undef, \$4 = undef, \$5 = undef",
277 qr#^(??{$re5})(|a(b)c|def)(??{"$^R"})$#,
280 "\$1 = abc, \$2 = b, \$3 = undef, \$4 = undef, \$5 = undef",
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",
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",
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",
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->($+);
312 ok($match, "match $c; Bug 56194");
314 # unset $str, @ctl_n and @plus
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");
328 local $re4 = qr#(1)((??{push @ctl_n, $f->($^N); push @plus, $f->($+);$^N + 1})){2}(?{$^N})(|abc|def)(??{"$^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->($+);
334 ok($match, 'Bug 56194');
341 is("@ctl_n", "1 2 undef", 'Bug 56194');
342 is("@plus", "1 2 undef", 'Bug 56194');
344 "\$1 = undef, \$2 = undef, \$3 = undef, \$4 = undef, \$5 = undef, \$^R = undef",
350 # re evals within \U, \Q etc shouldn't be seen by the lexer
353 ok('(?{1})' =~ /^\Q(?{1})\E$/, '\Q(?{1})\E');
354 ok('(?{1})' =~ /^\Q(?{\E1\}\)$/, '\Q(?{\E1\}\)');
355 eval {/^\U(??{"$a\Ea"})$/ }; norun('^\U(??{"$a\Ea"})$ norun');
356 eval {/^\L(??{"$B\Ea"})$/ }; norun('^\L(??{"$B\Ea"})$ norun');
358 ok('Ia' =~ /^\U(??{"$a\Ea"})$/, '^\U(??{"$a\Ea"})$');
359 ok('ja' =~ /^\L(??{"$B\Ea"})$/, '^\L(??{"$B\Ea"})$');
363 # Comprehensive (hopefully) tests of closure behaviour:
364 # i.e. when do (?{}) blocks get (re)compiled, and what instances
365 # of lexical vars do they close over?
367 # if the pattern string gets utf8 upgraded while concatenating,
368 # make sure a literal code block is still detected (by still
369 # compiling in the absence of use re 'eval')
374 ok("\x{80}\x{100}" =~ /^$s1(?{1})$s2$/, "utf8 upgrade");
377 my ($cr1, $cr2, $cr3, $cr4);
379 for my $x (qw(a b c)) {
380 my $bc = ($x ne 'a');
383 # the most basic: literal code should be in same scope
386 ok("A$x" =~ /^A(??{$x})$/, "[$x] literal code");
387 ok("\x{100}$x" =~ /^\x{100}(??{$x})$/, "[$x] literal code UTF8");
389 # the "don't recompile if pattern unchanged" mechanism
390 # shouldn't apply to code blocks - recompile every time
391 # to pick up new instances of variables
393 my $code1 = 'B(??{$x})';
394 my $code1u = $c80 . "\x{100}" . '(??{$x})';
397 norun("[$x] unvarying runtime code AA norun");
399 norun("[$x] unvarying runtime code AU norun");
400 eval {/^$c80\x{100}$code1$/};
401 norun("[$x] unvarying runtime code UA norun");
402 eval {/^$c80\x{101}$code1u$/};
403 norun("[$x] unvarying runtime code UU norun");
407 ok("AB$x" =~ /^A$code1$/, "[$x] unvarying runtime code AA");
408 ok("A$c80\x{100}$x" =~ /^A$code1u$/,
409 "[$x] unvarying runtime code AU");
410 ok("$c80\x{100}B$x" =~ /^$c80\x{100}$code1$/,
411 "[$x] unvarying runtime code UA");
412 ok("$c80\x{101}$c80\x{100}$x" =~ /^$c80\x{101}$code1u$/,
413 "[$x] unvarying runtime code UU");
416 # mixed literal and run-time code blocks
418 my $code2 = 'B(??{$x})';
419 my $code2u = $c80 . "\x{100}" . '(??{$x})';
421 eval {/^A(??{$x})-$code2$/};
422 norun("[$x] literal+runtime AA norun");
423 eval {/^A(??{$x})-$code2u$/};
424 norun("[$x] literal+runtime AU norun");
425 eval {/^$c80\x{100}(??{$x})-$code2$/};
426 norun("[$x] literal+runtime UA norun");
427 eval {/^$c80\x{101}(??{$x})-$code2u$/};
428 norun("[$x] literal+runtime UU norun");
432 ok("A$x-B$x" =~ /^A(??{$x})-$code2$/,
433 "[$x] literal+runtime AA");
434 ok("A$x-$c80\x{100}$x" =~ /^A(??{$x})-$code2u$/,
435 "[$x] literal+runtime AU");
436 ok("$c80\x{100}$x-B$x" =~ /^$c80\x{100}(??{$x})-$code2$/,
437 "[$x] literal+runtime UA");
438 ok("$c80\x{101}$x-$c80\x{100}$x"
439 =~ /^$c80\x{101}(??{$x})-$code2u$/,
440 "[$x] literal+runtime UU");
443 # literal qr code only created once, naked
445 $cr1 //= qr/^A(??{$x})$/;
446 ok("Aa" =~ $cr1, "[$x] literal qr once naked");
448 # literal qr code only created once, embedded with text
450 $cr2 //= qr/B(??{$x})$/;
451 ok("ABa" =~ /^A$cr2/, "[$x] literal qr once embedded text");
453 # literal qr code only created once, embedded with text + lit code
455 $cr3 //= qr/C(??{$x})$/;
456 ok("A$x-BCa" =~ /^A(??{$x})-B$cr3/,
457 "[$x] literal qr once embedded text + lit code");
459 # literal qr code only created once, embedded with text + run code
461 $cr4 //= qr/C(??{$x})$/;
462 my $code3 = 'A(??{$x})';
464 eval {/^$code3-B$cr4/};
465 norun("[$x] literal qr once embedded text + run code norun");
468 ok("A$x-BCa" =~ /^$code3-B$cr4/,
469 "[$x] literal qr once embedded text + run code");
472 # literal qr code, naked
474 my $r1 = qr/^A(??{$x})$/;
475 ok("A$x" =~ $r1, "[$x] literal qr naked");
477 # literal qr code, embedded with text
479 my $r2 = qr/B(??{$x})$/;
480 ok("AB$x" =~ /^A$r2/, "[$x] literal qr embedded text");
482 # literal qr code, embedded with text + lit code
484 my $r3 = qr/C(??{$x})$/;
485 ok("A$x-BC$x" =~ /^A(??{$x})-B$r3/,
486 "[$x] literal qr embedded text + lit code");
488 # literal qr code, embedded with text + run code
490 my $r4 = qr/C(??{$x})$/;
491 my $code4 = '(??{$x})';
493 eval {/^A$code4-B$r4/};
494 norun("[$x] literal qr embedded text + run code");
497 ok("A$x-BC$x" =~ /^A$code4-B$r4/,
498 "[$x] literal qr embedded text + run code");
501 # nested qr in different scopes
503 my $code5 = '(??{$x})';
504 my $r5 = qr/C(??{$x})/;
507 eval {qr/$code5-C(??{$x})/}; norun("r6 norun");
510 $r6 = qr/$code5-C(??{$x})/;
516 for my $y (qw(d e f)) {
518 my $rr5 = qr/^A(??{"$x$y"})-$r5/;
520 ok("A$x$y-C$x" =~ $rr5,
521 "[$x-$y] literal qr + r5");
523 my $rr6 = qr/^A(??{"$x$y"})-$r6/;
525 ok("A$x$y-$x-C$x" =~ $rr6,
526 "[$x-$y] literal qr + r6");
531 my $yy = (qw(d e f))[$i];
533 ok("A$x$yy-C$x" =~ $rr5, "[$x-$yy] literal qr + r5, outside");
534 ok("A$x$yy-C$x-D$x" =~ /$rr5-D(??{$x})$/,
535 "[$x-$yy] literal qr + r5 + lit, outside");
540 ok("A$x$yy-$x-C$x" =~ $rr6,
541 "[$x-$yy] literal qr + r6, outside");
542 ok("A$x$yy-$x-C$x-D$x" =~ /$rr6-D(??{$x})/,
543 "[$x-$yy] literal qr + r6 +lit, outside");
547 # recursive subs should get lexical from the correct pad depth
552 ok("A$n" =~ /^A(??{$n})$/, "recurse($n)");
557 # for qr// containing run-time elements but with a compile-time
558 # code block, make sure the run-time bits are executed in the same
559 # pad they were compiled in
561 my $a = 'a'; # ensure outer and inner pads don't align
565 my $r = qr/^$b(??{$c})$d$/;
566 ok("bcd" =~ $r, "qr with run-time elements and code block");
569 # check that cascaded embedded regexes all see their own lexical
573 my ($r1, $r2, $r3, $r4);
574 my ($x1, $x2, $x3, $x4) = (5,6,7,8);
575 { my $x1 = 1; $r1 = qr/A(??{$x1})/; }
576 { my $x2 = 2; $r2 = qr/$r1(??{$x2})/; }
577 { my $x3 = 3; $r3 = qr/$r2(??{$x3})/; }
578 { my $x4 = 4; $r4 = qr/$r3(??{$x4})/; }
579 ok("A1234" =~ /^$r4$/, "cascaded qr");
582 # and again, but in a loop, with no external references
583 # being maintained to the qr's
591 ok("A1234" =~ /^$r$/, "cascaded qr loop");
595 # and again, but compiling the qrs in an eval so there
596 # aren't even refs to the qrs from any ops
601 $r = eval q[ qr/$r(??{$x})/; ];
604 ok("A1234" =~ /^$r$/, "cascaded qr loop");
607 # have qrs with either literal code blocks or only embedded
608 # code blocks, but not both
611 my ($r1, $r2, $r3, $r4);
612 my ($x1, $x3) = (7,8);
613 { my $x1 = 1; $r1 = qr/A(??{$x1})/; }
614 { $r2 = qr/${r1}2/; }
615 { my $x3 = 3; $r3 = qr/$r2(??{$x3})/; }
616 { $r4 = qr/${r3}4/; }
617 ok("A1234" =~ /^$r4$/, "cascaded qr mix 1");
618 ok("A12345" =~ /^${r4}5$/, "cascaded qr mix 2");
619 ok("A1234" =~ qr/^$r4$/ , "cascaded qr mix 3");
620 ok("A12345" =~ qr/^${r4}5$/, "cascaded qr mix 4");
623 # and make sure things are freed at the right time
627 skip "MAD doesn't free eval CVs", 3;
631 sub Foo99::DESTROY { $Foo99::d++ }
635 my $x = bless [1], 'Foo99';
636 $r1 = eval 'qr/(??{$x->[0]})/';
638 my $r2 = eval 'qr/a$r1/';
640 ok(eval '"a1" =~ qr/^$r2$/', "match while in scope");
641 # make sure PL_reg_curpm isn't holding on to anything
643 is($Foo99::d, 0, "before scope exit");
645 ::is($Foo99::d, 1, "after scope exit");
648 # forward declared subs should Do The Right Thing with any anon CVs
649 # within them (i.e. pad_fixup_inner_anons() should work)
655 ok("Aa" =~ qr/^A(??{$x})$/, "forward qr compiletime");
656 ok("Aa" =~ qr/^$A(??{$x})$/, "forward qr runtime");
661 # test that run-time embedded code, when re-fed into toker,
662 # does all the right escapes
665 my $enc = eval 'use Encode; find_encoding("ascii")';
670 # note that most of the strings below are single-quoted, and the
671 # things within them, like '$y', *aren't* intended to interpolate
674 'a\\$y(?# (??{BEGIN{$x=1} "X1"})b(?# \Ux2\E)c\'d\\\\e\\\\Uf\\\\E';
676 ok(q{a$ybc'd\e\Uf\E} =~ /^$s1$/, "reparse");
677 is($x, 0, "reparse no BEGIN");
679 my $s2 = 'g\\$y# (??{{BEGIN{$x=2} "X3"}) \Ux3\E' . "\nh";
681 ok(q{a$ybc'd\\e\\Uf\\Eg$yh} =~ /^$s1$s2$/x, "reparse /x");
682 is($x, 0, "reparse /x no BEGIN");
687 # non-ascii in string as "<0xNNN>"
692 ($c< 32 || $c > 127) ? sprintf("<0x%x>", $c) : $1;
696 sub fmt { sprintf "hairy backslashes %s [%s] =~ /^%s/",
697 $_[0], esc_str($_[1]), esc_str($_[2]);
702 [ '', '', 'blank ' ],
703 [ "\x{100}", '\x{100}', 'single' ],
704 [ "\x{100}", "\x{100}", 'double' ])
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" ],
714 my ($s, $r) = @$pair;
716 my $ss = "$u->[0]$s";
719 my $cc = "$u->[1]$c";
721 ok($ss =~ /^$cc/, fmt("plain $u->[2]", $ss, $cc));
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"]
731 # (??{ qr/str/ }) goes through one less interpolation
732 # stage than (??{ qq/str/ })
733 $c =~ s{\\\\}{\\}g if ($use_qr eq 'qr');
738 eval {/^$cc/}; norun(fmt("code norun $u->[2]", $ss, $cc));
741 ok($ss =~ /^$cc/, fmt("code $u->[2]", $ss, $cc));
745 # Poor man's "use encoding 'ascii'".
746 # This causes a different code path in S_const_str()
748 local ${^ENCODING} = $enc;
750 ok($ss =~ /^$cc/, fmt("encode $u->[2]", $ss, $cc));
756 my $code1u = "(??{qw(\x{100})})";
757 eval {/^$code1u$/}; norun("reparse embeded unicode norun");
760 ok("\x{100}" =~ /^$code1u$/, "reparse embeded unicode");
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
769 eval q["a{" =~ '^(??{"a{"})$'];
770 norun("non-pattern literal code norun");
771 eval {/^${\'(??{"a{"})'}$/};
772 norun("runtime code with unbalanced {} norun");
775 ok("a{" =~ '^a(??{"{"})$', "non-pattern literal code");
776 ok("a{" =~ /^a${\'(??{"{"})'}$/, "runtime code with unbalanced {}");
779 # make sure warnings come from the right place
784 local $SIG{__WARN__} = sub { $w .= "@_" };
787 my $r = qr/(?{$t=$s+1})/;
789 like($w, qr/pat_re_eval/, "warning main file");
791 # do it in an eval to get predictable line numbers
794 $r = qr/(?{$t=$s+1})/;
798 like($w, qr/ at \(eval \d+\) line 3/, "warning eval A");
803 my $c = '(?{$t=$s+1})';
807 like($w, qr/ at \(eval \d+\) line 1/, "warning eval B");
812 # * mixing all the different types of blocks (literal, qr/literal/,
814 # * backtracking (the Z+ alternation ensures CURLYX and full
815 # scope popping on backtracking)
820 return unless $depth;
822 my $r1 = qr/(??{"$s1-$depth"})/;
825 my $c1 = '(??{"$s2-$depth"})';
827 ok( "<12345-ABC-$depth-123-LMN-$depth-1234-PQR-$depth>"
828 . "<12345-ABC-$depth-123-LMN-$depth-1234-PQR-$depth>"
830 /^<(\d|Z+)+(??{"45-ABC-$depth-"})(\d|Z+)+$r1-\d+$c1>
831 <(\d|Z+)+(??{"45-ABC-$depth-"})(\d|Z+)+$r1-\d+$c1>$/x,
838 # nested (??{}) called from various levels of a recursive function
844 ok("A$n" =~ m{^A(??{ "0123" =~ /((??{$n}))/; $1 })$},
846 ok("A$n" !~ m{^A(??{ "0123" =~ /((??{$n}))/; "X" })$},
847 "recurse3($n) nomatch");
853 # nested (??{}) being invoked recursively via a function
858 my @alpha = qw(A B C D E);
863 my $m = ("$alpha[$n]" . substr("0123", 0, $n+1)) =~
867 "$n-0123" =~ m{^(\d)-(((??{$recurse4->($n+1)})))};
868 $s .= "i1=$1:<=[$2]";
869 $3; # NB - not stringified
874 $s .= $m ? 'M' : '!M';
876 my $ret = '.*?' . ($n-1);
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');
887 # single (??{}) being invoked recursively via a function
892 my @alpha = qw(A B C D E);
897 my $m = ("$alpha[$n]" . substr("0123", 0, $n+1)) =~
906 $s .= $m ? 'M' : '!M';
908 my $ret = '.*?' . ($n-1);
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');
920 # make sure that errors during compiling run-time code get trapped
925 my $code = '(?{$x=})';
926 eval { "a" =~ /^a$code/ };
927 like($@, qr/syntax error at \(eval \d+\) line \d+/, 'syntax error');
929 $code = '(?{BEGIN{die})';
930 eval { "a" =~ /^a$code/ };
932 qr/BEGIN failed--compilation aborted at \(eval \d+\) line \d+/,
936 # make sure that 'use re eval' is propagated into compiling the
937 # pattern returned by (??{})
941 my $pat = 'B(??{1})C';
943 # compile-time outer code-block
944 ok("AB1CD" =~ /^A(??{$pat})D$/, "re eval propagated compile-time");
945 # run-time outer code-block
946 ok("AB1CD" =~ /^$A(??{$pat})D$/, "re eval propagated run-time");
949 # returning a ref to something that had set magic but wasn't
950 # PERL_MAGIC_qr triggered a false positive assertion failure
951 # The test is not so much concerned with it not matching,
952 # as with not failing the assertion
955 ok("a" !~ /^(a)(??{ \$1 })/, '(??{ ref })');
958 # make sure the uninit warning from returning an undef var
964 local $SIG{__WARN__} = sub { $warn .= $_[0] };
965 $u1 =~ /(??{$u2})/ or die;
966 like($warn, qr/value \$u1 in pattern match.*\n.*value at/, 'uninit');
969 # test that code blocks are called in scalar context
973 ok("" =~ /^(?{@a})$/, '(?{}) in scalar context');
974 is($^R, 1, '(?{}) in scalar context: $^R');
975 ok("1" =~ /^(??{@a})$/, '(??{}) in scalar context');
976 ok("foo" =~ /^(?(?{@a})foo|bar)$/, '(?(?{})|) in scalar context');
979 # BEGIN in compiled blocks shouldn't mess with $1 et al
983 my $code1 = '(B)(??{ BEGIN { "X" =~ /X/ } $1})(C)';
984 ok("ABBCA" =~ /^(.)(??{$code1})\1$/, '(?{}) BEGIN and $1');
985 my $code2 = '(B)(??{ BEGIN { "X" =~ /X/ } $1 =~ /(.)/ ? $1 : ""})(C)';
986 ok("ABBCA" =~ /^(.)(??{$code2})\1$/, '(?{}) BEGIN and $1 mark 2');
989 # check that the optimiser is applied to code blocks: see if aelem has
990 # been converted to aelemfast
996 'q() =~ qr/(?{$a[0]})/',
997 'use re q(eval); q() =~ q{(?{$a[0]})}',
998 'use re q(eval); $c = q{(?{$a[0]})}; /$c/',
999 'use re q(eval); $c = q{(?{$a[0]})}; /(?{1;})$c/',
1001 $out = runperl(switches => ["-Dt"], prog => $prog, stderr => 1);
1002 like($out, qr/aelemfast|Recompile perl with -DDEBUGGING/,
1003 "optimise: '$prog'");
1010 } # End of sub run_tests