This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Move the loading of %Config from ReTest.pl to the only use point in pat.t
[perl5.git] / t / re / pat.t
CommitLineData
8d063cd8 1#!./perl
8d37f932
DD
2#
3# This is a home for regular expression tests that don't fit into
67a2b8c6 4# the format supported by re/regexp.t. If you want to add a test
ff3f963a
KW
5# that does fit that format, add it to re/re_tests, not here. Tests for \N
6# should be added here because they are treated as single quoted strings
7# there, which means they avoid the lexer which otherwise would look at them.
8d063cd8 8
84281c31
A
9use strict;
10use warnings;
11use 5.010;
12
84281c31
A
13sub run_tests;
14
9133bbab 15$| = 1;
3568d838 16
8d37f932 17
e4d48cc9
GS
18BEGIN {
19 chdir 't' if -d 't';
9d45b377
YO
20 @INC = ('../lib','.');
21 do "re/ReTest.pl" or die $@;
e4d48cc9 22}
84281c31 23
7bd6b0e6 24plan tests => 426; # Update this when adding/deleting tests.
b7a35066 25
9d45b377 26run_tests() unless caller;
b7a35066 27
84281c31
A
28#
29# Tests start here.
30#
31sub run_tests {
0ef3e39e 32
84281c31 33 {
84281c31 34 my $x = "abc\ndef\n";
5895685f 35 (my $x_pretty = $x) =~ s/\n/\\n/g;
fd291da9 36
5895685f
NC
37 ok $x =~ /^abc/, qq ["$x_pretty" =~ /^abc/];
38 ok $x !~ /^def/, qq ["$x_pretty" !~ /^def/];
fd291da9 39
84281c31 40 # used to be a test for $*
5895685f 41 ok $x =~ /^def/m, qq ["$x_pretty" =~ /^def/m];
fd291da9 42
b33825c4
NC
43 ok(!($x =~ /^xxx/), qq ["$x_pretty" =~ /^xxx/]);
44 ok(!($x !~ /^abc/), qq ["$x_pretty" !~ /^abc/]);
fd291da9 45
5895685f 46 ok $x =~ /def/, qq ["$x_pretty" =~ /def/];
b33825c4 47 ok(!($x !~ /def/), qq ["$x_pretty" !~ /def/]);
4765795a 48
5895685f 49 ok $x !~ /.def/, qq ["$x_pretty" !~ /.def/];
b33825c4 50 ok(!($x =~ /.def/), qq ["$x_pretty" =~ /.def/]);
4765795a 51
5895685f 52 ok $x =~ /\ndef/, qq ["$x_pretty" =~ /\\ndef/];
b33825c4 53 ok(!($x !~ /\ndef/), qq ["$x_pretty" !~ /\\ndef/]);
84281c31 54 }
4765795a 55
84281c31
A
56 {
57 $_ = '123';
58 ok /^([0-9][0-9]*)/, qq [\$_ = '$_'; /^([0-9][0-9]*)/];
59 }
f9969324 60
84281c31
A
61 {
62 $_ = 'aaabbbccc';
63 ok /(a*b*)(c*)/ && $1 eq 'aaabbb' && $2 eq 'ccc',
64 qq [\$_ = '$_'; /(a*b*)(c*)/];
65 ok /(a+b+c+)/ && $1 eq 'aaabbbccc', qq [\$_ = '$_'; /(a+b+c+)/];
b33825c4 66 unlike($_, qr/a+b?c+/, qq [\$_ = '$_'; /a+b?c+/]);
84281c31
A
67
68 $_ = 'aaabccc';
69 ok /a+b?c+/, qq [\$_ = '$_'; /a+b?c+/];
70 ok /a*b?c*/, qq [\$_ = '$_'; /a*b?c*/];
71
72 $_ = 'aaaccc';
73 ok /a*b?c*/, qq [\$_ = '$_'; /a*b?c*/];
b33825c4 74 unlike($_, qr/a*b+c*/, qq [\$_ = '$_'; /a*b+c*/]);
84281c31
A
75
76 $_ = 'abcdef';
77 ok /bcd|xyz/, qq [\$_ = '$_'; /bcd|xyz/];
78 ok /xyz|bcd/, qq [\$_ = '$_'; /xyz|bcd/];
79 ok m|bc/*d|, qq [\$_ = '$_'; m|bc/*d|];
80 ok /^$_$/, qq [\$_ = '$_'; /^\$_\$/];
4765795a 81 }
4765795a 82
84281c31
A
83 {
84 # used to be a test for $*
5895685f 85 ok "ab\ncd\n" =~ /^cd/m, q ["ab\ncd\n" =~ /^cd/m];
84281c31 86 }
4765795a 87
84281c31
A
88 {
89 our %XXX = map {($_ => $_)} 123, 234, 345;
90
91 our @XXX = ('ok 1','not ok 1', 'ok 2','not ok 2','not ok 3');
92 while ($_ = shift(@XXX)) {
b33825c4 93 my $e = index ($_, 'not') >= 0 ? '' : 1;
725a61d7 94 my $r = m?(.*)?;
b33825c4 95 is($r, $e, "?(.*)?");
84281c31
A
96 /not/ && reset;
97 if (/not ok 2/) {
98 if ($^O eq 'VMS') {
99 $_ = shift(@XXX);
100 }
101 else {
102 reset 'X';
103 }
104 }
105 }
4765795a 106
84281c31
A
107 SKIP: {
108 if ($^O eq 'VMS') {
109 skip "Reset 'X'", 1;
110 }
111 ok !keys %XXX, "%XXX is empty";
112 }
4765795a 113
84281c31 114 }
4765795a 115
84281c31 116 {
4f890a30 117 my $message = "Test empty pattern";
84281c31
A
118 my $xyz = 'xyz';
119 my $cde = 'cde';
120
121 $cde =~ /[^ab]*/;
122 $xyz =~ //;
4f890a30 123 is($&, $xyz, $message);
84281c31
A
124
125 my $foo = '[^ab]*';
126 $cde =~ /$foo/;
127 $xyz =~ //;
4f890a30 128 is($&, $xyz, $message);
84281c31
A
129
130 $cde =~ /$foo/;
131 my $null;
132 no warnings 'uninitialized';
133 $xyz =~ /$null/;
4f890a30 134 is($&, $xyz, $message);
84281c31
A
135
136 $null = "";
137 $xyz =~ /$null/;
4f890a30 138 is($&, $xyz, $message);
84281c31 139 }
4765795a 140
84281c31 141 {
4f890a30 142 my $message = q !Check $`, $&, $'!;
84281c31 143 $_ = 'abcdefghi';
0f289c68 144 /def/; # optimized up to cmd
4f890a30 145 is("$`:$&:$'", 'abc:def:ghi', $message);
4765795a 146
84281c31 147 no warnings 'void';
0f289c68 148 /cde/ + 0; # optimized only to spat
4f890a30 149 is("$`:$&:$'", 'ab:cde:fghi', $message);
4765795a 150
0f289c68 151 /[d][e][f]/; # not optimized
4f890a30 152 is("$`:$&:$'", 'abc:def:ghi', $message);
84281c31 153 }
4765795a 154
84281c31
A
155 {
156 $_ = 'now is the {time for all} good men to come to.';
157 / {([^}]*)}/;
de26e0cc 158 is($1, 'time for all', "Match braces");
84281c31 159 }
4765795a 160
84281c31 161 {
4f890a30 162 my $message = "{N,M} quantifier";
84281c31 163 $_ = 'xxx {3,4} yyy zzz';
4f890a30
NC
164 ok(/( {3,4})/, $message);
165 is($1, ' ', $message);
166 unlike($_, qr/( {4,})/, $message);
167 ok(/( {2,3}.)/, $message);
168 is($1, ' y', $message);
169 ok(/(y{2,3}.)/, $message);
170 is($1, 'yyy ', $message);
171 unlike($_, qr/x {3,4}/, $message);
172 unlike($_, qr/^xxx {3,4}/, $message);
84281c31 173 }
4765795a 174
84281c31 175 {
4f890a30 176 my $message = "Test /g";
84281c31
A
177 local $" = ":";
178 $_ = "now is the time for all good men to come to.";
179 my @words = /(\w+)/g;
180 my $exp = "now:is:the:time:for:all:good:men:to:come:to";
4765795a 181
4f890a30 182 is("@words", $exp, $message);
4765795a 183
84281c31
A
184 @words = ();
185 while (/\w+/g) {
186 push (@words, $&);
187 }
4f890a30 188 is("@words", $exp, $message);
4765795a 189
84281c31
A
190 @words = ();
191 pos = 0;
192 while (/to/g) {
193 push(@words, $&);
194 }
4f890a30 195 is("@words", "to:to", $message);
4765795a 196
84281c31
A
197 pos $_ = 0;
198 @words = /to/g;
4f890a30 199 is("@words", "to:to", $message);
84281c31 200 }
4765795a 201
84281c31
A
202 {
203 $_ = "abcdefghi";
204
205 my $pat1 = 'def';
206 my $pat2 = '^def';
207 my $pat3 = '.def.';
208 my $pat4 = 'abc';
209 my $pat5 = '^abc';
210 my $pat6 = 'abc$';
211 my $pat7 = 'ghi';
212 my $pat8 = '\w*ghi';
213 my $pat9 = 'ghi$';
214
215 my $t1 = my $t2 = my $t3 = my $t4 = my $t5 =
216 my $t6 = my $t7 = my $t8 = my $t9 = 0;
217
218 for my $iter (1 .. 5) {
219 $t1++ if /$pat1/o;
220 $t2++ if /$pat2/o;
221 $t3++ if /$pat3/o;
222 $t4++ if /$pat4/o;
223 $t5++ if /$pat5/o;
224 $t6++ if /$pat6/o;
225 $t7++ if /$pat7/o;
226 $t8++ if /$pat8/o;
227 $t9++ if /$pat9/o;
228 }
229 my $x = "$t1$t2$t3$t4$t5$t6$t7$t8$t9";
de26e0cc 230 is($x, '505550555', "Test /o");
84281c31 231 }
4765795a 232
4f890a30 233 {
84281c31
A
234 my $xyz = 'xyz';
235 ok "abc" =~ /^abc$|$xyz/, "| after \$";
4765795a 236
84281c31 237 # perl 4.009 says "unmatched ()"
4f890a30 238 my $message = '$ inside ()';
4765795a 239
84281c31
A
240 my $result;
241 eval '"abc" =~ /a(bc$)|$xyz/; $result = "$&:$1"';
4f890a30
NC
242 is($@, "", $message);
243 is($result, "abc:bc", $message);
84281c31 244 }
4765795a 245
84281c31 246 {
4f890a30 247 my $message = "Scalar /g";
84281c31
A
248 $_ = "abcfooabcbar";
249
4f890a30
NC
250 ok( /abc/g && $` eq "", $message);
251 ok( /abc/g && $` eq "abcfoo", $message);
252 ok(!/abc/g, $message);
84281c31 253
4f890a30 254 $message = "Scalar /gi";
84281c31 255 pos = 0;
4f890a30
NC
256 ok( /ABC/gi && $` eq "", $message);
257 ok( /ABC/gi && $` eq "abcfoo", $message);
258 ok(!/ABC/gi, $message);
84281c31 259
4f890a30 260 $message = "Scalar /g";
84281c31 261 pos = 0;
4f890a30
NC
262 ok( /abc/g && $' eq "fooabcbar", $message);
263 ok( /abc/g && $' eq "bar", $message);
84281c31
A
264
265 $_ .= '';
266 my @x = /abc/g;
de26e0cc 267 is(@x, 2, "/g reset after assignment");
4765795a 268 }
4765795a 269
84281c31 270 {
4f890a30 271 my $message = '/g, \G and pos';
84281c31
A
272 $_ = "abdc";
273 pos $_ = 2;
274 /\Gc/gc;
4f890a30 275 is(pos $_, 2, $message);
84281c31 276 /\Gc/g;
4f890a30 277 is(pos $_, undef, $message);
84281c31 278 }
4765795a 279
84281c31 280 {
4f890a30 281 my $message = '(?{ })';
84281c31
A
282 our $out = 1;
283 'abc' =~ m'a(?{ $out = 2 })b';
4f890a30 284 is($out, 2, $message);
84281c31
A
285
286 $out = 1;
287 'abc' =~ m'a(?{ $out = 3 })c';
4f890a30 288 is($out, 1, $message);
84281c31 289 }
4765795a 290
84281c31
A
291 {
292 $_ = 'foobar1 bar2 foobar3 barfoobar5 foobar6';
293 my @out = /(?<!foo)bar./g;
de26e0cc 294 is("@out", 'bar2 barf', "Negative lookbehind");
84281c31 295 }
4765795a 296
84281c31 297 {
4f890a30 298 my $message = "REG_INFTY tests";
84281c31 299 # Tests which depend on REG_INFTY
19d6612d
NC
300
301 # Defaults assumed if this fails
302 eval { require Config; };
303 $::reg_infty = $Config::Config{reg_infty} // 32767;
84281c31
A
304 $::reg_infty_m = $::reg_infty - 1;
305 $::reg_infty_p = $::reg_infty + 1;
93f09d7b 306 $::reg_infty_m = $::reg_infty_m; # Suppress warning.
84281c31
A
307
308 # As well as failing if the pattern matches do unexpected things, the
309 # next three tests will fail if you should have picked up a lower-than-
310 # default value for $reg_infty from Config.pm, but have not.
311
4f890a30
NC
312 eval_ok q (('aaa' =~ /(a{1,$::reg_infty_m})/)[0] eq 'aaa'), $message;
313 eval_ok q (('a' x $::reg_infty_m) =~ /a{$::reg_infty_m}/), $message;
314 eval_ok q (('a' x ($::reg_infty_m - 1)) !~ /a{$::reg_infty_m}/), $message;
84281c31 315 eval "'aaa' =~ /a{1,$::reg_infty}/";
4f890a30 316 like($@, /^\QQuantifier in {,} bigger than/, $message);
84281c31 317 eval "'aaa' =~ /a{1,$::reg_infty_p}/";
4f890a30 318 like($@, qr/^\QQuantifier in {,} bigger than/, $message);
4765795a 319 }
8269fa76 320
84281c31
A
321 {
322 # Poke a couple more parse failures
323 my $context = 'x' x 256;
324 eval qq("${context}y" =~ /(?<=$context)y/);
325 ok $@ =~ /^\QLookbehind longer than 255 not/, "Lookbehind limit";
326 }
8269fa76 327
84281c31
A
328 {
329 # Long Monsters
84281c31
A
330 for my $l (125, 140, 250, 270, 300000, 30) { # Ordered to free memory
331 my $a = 'a' x $l;
4f890a30
NC
332 my $message = "Long monster, length = $l";
333 like("ba$a=", qr/a$a=/, $message);
334 unlike("b$a=", qr/a$a=/, $message);
335 like("b$a=", qr/ba+=/, $message);
84281c31 336
4f890a30 337 like("ba$a=", /b(?:a|b)+=/, $message);
84281c31
A
338 }
339 }
8269fa76 340
84281c31
A
341 {
342 # 20000 nodes, each taking 3 words per string, and 1 per branch
343 my $long_constant_len = join '|', 12120 .. 32645;
344 my $long_var_len = join '|', 8120 .. 28645;
345 my %ans = ( 'ax13876y25677lbc' => 1,
346 'ax13876y25677mcb' => 0, # not b.
347 'ax13876y35677nbc' => 0, # Num too big
348 'ax13876y25677y21378obc' => 1,
0f289c68 349 'ax13876y25677y21378zbc' => 0, # Not followed by [k-o]
84281c31
A
350 'ax13876y25677y21378y21378kbc' => 1,
351 'ax13876y25677y21378y21378kcb' => 0, # Not b.
352 'ax13876y25677y21378y21378y21378kbc' => 0, # 5 runs
353 );
354
84281c31 355 for (keys %ans) {
4f890a30
NC
356 my $message = "20000 nodes, const-len '$_'";
357 ok !($ans{$_} xor /a(?=([yx]($long_constant_len)){2,4}[k-o]).*b./o), $message;
84281c31 358
4f890a30
NC
359 $message = "20000 nodes, var-len '$_'";
360 ok !($ans{$_} xor /a(?=([yx]($long_var_len)){2,4}[k-o]).*b./o,), $message;
84281c31 361 }
b8ef571c 362 }
209a9bc1 363
84281c31 364 {
4f890a30 365 my $message = "Complicated backtracking";
84281c31
A
366 $_ = " a (bla()) and x(y b((l)u((e))) and b(l(e)e)e";
367 my $expect = "(bla()) ((l)u((e))) (l(e)e)";
368
369 use vars '$c';
370 sub matchit {
371 m/
372 (
373 \(
0f289c68 374 (?{ $c = 1 }) # Initialize
84281c31
A
375 (?:
376 (?(?{ $c == 0 }) # PREVIOUS iteration was OK, stop the loop
377 (?!
0f289c68
YO
378 ) # Fail: will unwind one iteration back
379 )
84281c31 380 (?:
0f289c68 381 [^()]+ # Match a big chunk
84281c31
A
382 (?=
383 [()]
0f289c68 384 ) # Do not try to match subchunks
84281c31
A
385 |
386 \(
387 (?{ ++$c })
388 |
389 \)
390 (?{ --$c })
391 )
0f289c68 392 )+ # This may not match with different subblocks
84281c31
A
393 )
394 (?(?{ $c != 0 })
395 (?!
0f289c68
YO
396 ) # Fail
397 ) # Otherwise the chunk 1 may succeed with $c>0
84281c31
A
398 /xg;
399 }
3568d838 400
84281c31
A
401 my @ans = ();
402 my $res;
403 push @ans, $res while $res = matchit;
4f890a30 404 is("@ans", "1 1 1", $message);
3568d838 405
84281c31 406 @ans = matchit;
4f890a30 407 is("@ans", $expect, $message);
3568d838 408
4f890a30 409 $message = "Recursion with (??{ })";
84281c31
A
410 our $matched;
411 $matched = qr/\((?:(?>[^()]+)|(??{$matched}))*\)/;
3568d838 412
84281c31
A
413 @ans = my @ans1 = ();
414 push (@ans, $res), push (@ans1, $&) while $res = m/$matched/g;
3568d838 415
4f890a30
NC
416 is("@ans", "1 1 1", $message);
417 is("@ans1", $expect, $message);
3568d838 418
84281c31 419 @ans = m/$matched/g;
4f890a30 420 is("@ans", $expect, $message);
3568d838 421
84281c31 422 }
3568d838 423
84281c31
A
424 {
425 ok "abc" =~ /^(??{"a"})b/, '"abc" =~ /^(??{"a"})b/';
426 }
3568d838 427
84281c31 428 {
0f289c68 429 my @ans = ('a/b' =~ m%(.*/)?(.*)%); # Stack may be bad
de26e0cc 430 is("@ans", 'a/ b', "Stack may be bad");
84281c31 431 }
3568d838 432
84281c31 433 {
4f890a30 434 my $message = "Eval-group not allowed at runtime";
84281c31
A
435 my $code = '{$blah = 45}';
436 our $blah = 12;
437 eval { /(?$code)/ };
4f890a30 438 ok($@ && $@ =~ /not allowed at runtime/ && $blah == 12, $message);
84281c31 439
3044771b
NC
440 $blah = 12;
441 my $res = eval { "xx" =~ /(?$code)/o };
442 {
443 no warnings 'uninitialized';
5895685f 444 chomp $@; my $message = "$message '$@', '$res', '$blah'";
4f890a30 445 ok($@ && $@ =~ /not allowed at runtime/ && $blah == 12, $message);
3044771b
NC
446 }
447
448 $code = '=xx';
449 $blah = 12;
450 $res = eval { "xx" =~ /(?$code)/o };
451 {
452 no warnings 'uninitialized';
4f890a30
NC
453 my $message = "$message '$@', '$res', '$blah'";
454 ok(!$@ && $res, $message);
3044771b 455 }
3568d838 456
84281c31
A
457 $code = '{$blah = 45}';
458 $blah = 12;
459 eval "/(?$code)/";
4f890a30 460 is($blah, 45, $message);
3568d838 461
84281c31
A
462 $blah = 12;
463 /(?{$blah = 45})/;
4f890a30 464 is($blah, 45, $message);
84281c31 465 }
3568d838 466
84281c31 467 {
4f890a30 468 my $message = "Pos checks";
84281c31
A
469 my $x = 'banana';
470 $x =~ /.a/g;
4f890a30 471 is(pos $x, 2, $message);
3568d838 472
84281c31 473 $x =~ /.z/gc;
4f890a30 474 is(pos $x, 2, $message);
3568d838 475
84281c31
A
476 sub f {
477 my $p = $_[0];
478 return $p;
479 }
3568d838 480
84281c31 481 $x =~ /.a/g;
4f890a30 482 is(f (pos $x), 4, $message);
84281c31 483 }
3568d838 484
84281c31 485 {
4f890a30 486 my $message = 'Checking $^R';
84281c31
A
487 our $x = $^R = 67;
488 'foot' =~ /foo(?{$x = 12; 75})[t]/;
4f890a30 489 is($^R, 75, $message);
84281c31
A
490
491 $x = $^R = 67;
492 'foot' =~ /foo(?{$x = 12; 75})[xy]/;
4f890a30 493 ok($^R eq '67' && $x eq '12', $message);
84281c31
A
494
495 $x = $^R = 67;
496 'foot' =~ /foo(?{ $^R + 12 })((?{ $x = 12; $^R + 17 })[xy])?/;
4f890a30 497 ok($^R eq '79' && $x eq '12', $message);
84281c31 498 }
3568d838 499
84281c31 500 {
de26e0cc
NC
501 is(qr/\b\v$/i, '(?^i:\b\v$)', 'qr/\b\v$/i');
502 is(qr/\b\v$/s, '(?^s:\b\v$)', 'qr/\b\v$/s');
503 is(qr/\b\v$/m, '(?^m:\b\v$)', 'qr/\b\v$/m');
504 is(qr/\b\v$/x, '(?^x:\b\v$)', 'qr/\b\v$/x');
505 is(qr/\b\v$/xism, '(?^msix:\b\v$)', 'qr/\b\v$/xism');
506 is(qr/\b\v$/, '(?^:\b\v$)', 'qr/\b\v$/');
84281c31 507 }
3568d838 508
9de15fec 509 { # Test that charset modifier work, and are interpolated
de26e0cc
NC
510 is(qr/\b\v$/, '(?^:\b\v$)', 'Verify no locale, no unicode_strings gives default modifier');
511 is(qr/(?l:\b\v$)/, '(?^:(?l:\b\v$))', 'Verify infix l modifier compiles');
512 is(qr/(?u:\b\v$)/, '(?^:(?u:\b\v$))', 'Verify infix u modifier compiles');
513 is(qr/(?l)\b\v$/, '(?^:(?l)\b\v$)', 'Verify (?l) compiles');
514 is(qr/(?u)\b\v$/, '(?^:(?u)\b\v$)', 'Verify (?u) compiles');
9de15fec
KW
515
516 my $dual = qr/\b\v$/;
517 use locale;
518 my $locale = qr/\b\v$/;
de26e0cc 519 is($locale, '(?^l:\b\v$)', 'Verify has l modifier when compiled under use locale');
9de15fec
KW
520 no locale;
521
522 use feature 'unicode_strings';
523 my $unicode = qr/\b\v$/;
de26e0cc
NC
524 is($unicode, '(?^u:\b\v$)', 'Verify has u modifier when compiled under unicode_strings');
525 is(qr/abc$dual/, '(?^u:abc(?^:\b\v$))', 'Verify retains d meaning when interpolated under locale');
526 is(qr/abc$locale/, '(?^u:abc(?^l:\b\v$))', 'Verify retains l when interpolated under unicode_strings');
9de15fec
KW
527
528 no feature 'unicode_strings';
de26e0cc
NC
529 is(qr/abc$locale/, '(?^:abc(?^l:\b\v$))', 'Verify retains l when interpolated outside locale and unicode strings');
530 is(qr/def$unicode/, '(?^:def(?^u:\b\v$))', 'Verify retains u when interpolated outside locale and unicode strings');
9de15fec
KW
531
532 use locale;
de26e0cc
NC
533 is(qr/abc$dual/, '(?^l:abc(?^:\b\v$))', 'Verify retains d meaning when interpolated under locale');
534 is(qr/abc$unicode/, '(?^l:abc(?^u:\b\v$))', 'Verify retains u when interpolated under locale');
9de15fec
KW
535 }
536
84281c31 537 {
4f890a30 538 my $message = "Look around";
84281c31 539 $_ = 'xabcx';
84281c31 540 foreach my $ans ('', 'c') {
4f890a30
NC
541 ok(/(?<=(?=a)..)((?=c)|.)/g, $message);
542 is($1, $ans, $message);
84281c31
A
543 }
544 }
3568d838 545
84281c31 546 {
4f890a30 547 my $message = "Empty clause";
84281c31
A
548 $_ = 'a';
549 foreach my $ans ('', 'a', '') {
4f890a30
NC
550 ok(/^|a|$/g, $message);
551 is($&, $ans, $message);
84281c31
A
552 }
553 }
3568d838 554
84281c31 555 {
84281c31 556 sub prefixify {
4f890a30
NC
557 my $message = "Prefixify";
558 {
84281c31 559 my ($v, $a, $b, $res) = @_;
4f890a30
NC
560 ok($v =~ s/\Q$a\E/$b/, $message);
561 is($v, $res, $message);
84281c31
A
562 }
563 }
3568d838 564
84281c31
A
565 prefixify ('/a/b/lib/arch', "/a/b/lib", 'X/lib', 'X/lib/arch');
566 prefixify ('/a/b/man/arch', "/a/b/man", 'X/man', 'X/man/arch');
567 }
3568d838 568
84281c31
A
569 {
570 $_ = 'var="foo"';
571 /(\")/;
572 ok $1 && /$1/, "Capture a quote";
573 }
3568d838 574
84281c31 575 {
84281c31 576 no warnings 'closure';
4f890a30 577 my $message = '(?{ $var } refers to package vars';
84281c31
A
578 package aa;
579 our $c = 2;
580 $::c = 3;
581 '' =~ /(?{ $c = 4 })/;
4f890a30
NC
582 main::is($c, 4, $message);
583 main::is($::c, 3, $message);
84281c31 584 }
3568d838 585
84281c31
A
586 {
587 must_die 'q(a:[b]:) =~ /[x[:foo:]]/',
88743d87 588 qr/POSIX class \[:[^:]+:\] unknown in regex/,
84281c31
A
589 'POSIX class [: :] must have valid name';
590
591 for my $d (qw [= .]) {
592 must_die "/[[${d}foo${d}]]/",
88743d87 593 qr/\QPOSIX syntax [$d $d] is reserved for future extensions/,
84281c31
A
594 "POSIX syntax [[$d $d]] is an error";
595 }
596 }
3568d838 597
84281c31
A
598 {
599 # test if failure of patterns returns empty list
4f890a30 600 my $message = "Failed pattern returns empty list";
84281c31
A
601 $_ = 'aaa';
602 @_ = /bbb/;
4f890a30 603 is("@_", "", $message);
3568d838 604
84281c31 605 @_ = /bbb/g;
4f890a30 606 is("@_", "", $message);
a72deede 607
84281c31 608 @_ = /(bbb)/;
4f890a30 609 is("@_", "", $message);
a72deede 610
84281c31 611 @_ = /(bbb)/g;
4f890a30 612 is("@_", "", $message);
84281c31 613 }
a72deede 614
84281c31 615 {
4f890a30 616 my $message = '@- and @+ tests';
84281c31
A
617
618 /a(?=.$)/;
4f890a30
NC
619 is($#+, 0, $message);
620 is($#-, 0, $message);
621 is($+ [0], 2, $message);
622 is($- [0], 1, $message);
623 ok(!defined $+ [1] && !defined $- [1] &&
624 !defined $+ [2] && !defined $- [2], $message);
84281c31
A
625
626 /a(a)(a)/;
4f890a30
NC
627 is($#+, 2, $message);
628 is($#-, 2, $message);
629 is($+ [0], 3, $message);
630 is($- [0], 0, $message);
631 is($+ [1], 2, $message);
632 is($- [1], 1, $message);
633 is($+ [2], 3, $message);
634 is($- [2], 2, $message);
635 ok(!defined $+ [3] && !defined $- [3] &&
636 !defined $+ [4] && !defined $- [4], $message);
84281c31 637
54a4274e 638 # Exists has a special check for @-/@+ - bug 45147
4f890a30
NC
639 ok(exists $-[0], $message);
640 ok(exists $+[0], $message);
641 ok(exists $-[2], $message);
642 ok(exists $+[2], $message);
643 ok(!exists $-[3], $message);
644 ok(!exists $+[3], $message);
645 ok(exists $-[-1], $message);
646 ok(exists $+[-1], $message);
647 ok(exists $-[-3], $message);
648 ok(exists $+[-3], $message);
649 ok(!exists $-[-4], $message);
650 ok(!exists $+[-4], $message);
84281c31
A
651
652 /.(a)(b)?(a)/;
4f890a30
NC
653 is($#+, 3, $message);
654 is($#-, 3, $message);
655 is($+ [1], 2, $message);
656 is($- [1], 1, $message);
657 is($+ [3], 3, $message);
658 is($- [3], 2, $message);
659 ok(!defined $+ [2] && !defined $- [2] &&
660 !defined $+ [4] && !defined $- [4], $message);
84281c31 661
84281c31 662 /.(a)/;
4f890a30
NC
663 is($#+, 1, $message);
664 is($#-, 1, $message);
665 is($+ [0], 2, $message);
666 is($- [0], 0, $message);
667 is($+ [1], 2, $message);
668 is($- [1], 1, $message);
669 ok(!defined $+ [2] && !defined $- [2] &&
670 !defined $+ [3] && !defined $- [3], $message);
84281c31
A
671
672 /.(a)(ba*)?/;
4f890a30
NC
673 is($#+, 2, $message);
674 is($#-, 1, $message);
84281c31 675 }
a72deede 676
88743d87
NC
677 foreach ('$+[0] = 13', '$-[0] = 13', '@+ = (7, 6, 5)', '@- = qw (foo bar)') {
678 must_die($_, qr/^Modification of a read-only value attempted/,
679 'Elements of @- and @+ are read-only');
84281c31 680 }
a72deede 681
84281c31 682 {
4f890a30 683 my $message = '\G testing';
84281c31
A
684 $_ = 'aaa';
685 pos = 1;
686 my @a = /\Ga/g;
4f890a30 687 is("@a", "a a", $message);
84281c31
A
688
689 my $str = 'abcde';
690 pos $str = 2;
4f890a30
NC
691 unlike($str, qr/^\G/, $message);
692 unlike($str, qr/^.\G/, $message);
693 like($str, qr/^..\G/, $message);
694 unlike($str, qr/^...\G/, $message);
695 ok($str =~ /\G../ && $& eq 'cd', $message);
84281c31 696
04934b6d 697 local $::TODO = $::running_as_thread;
4f890a30 698 ok($str =~ /.\G./ && $& eq 'bc', $message);
84281c31 699 }
a72deede 700
84281c31 701 {
4f890a30 702 my $message = 'pos inside (?{ })';
84281c31
A
703 my $str = 'abcde';
704 our ($foo, $bar);
4f890a30
NC
705 like($str, qr/b(?{$foo = $_; $bar = pos})c/, $message);
706 is($foo, $str, $message);
707 is($bar, 2, $message);
708 is(pos $str, undef, $message);
84281c31
A
709
710 undef $foo;
711 undef $bar;
712 pos $str = undef;
4f890a30
NC
713 ok($str =~ /b(?{$foo = $_; $bar = pos})c/g, $message);
714 is($foo, $str, $message);
715 is($bar, 2, $message);
716 is(pos $str, 3, $message);
84281c31
A
717
718 $_ = $str;
719 undef $foo;
720 undef $bar;
4f890a30
NC
721 like($_, qr/b(?{$foo = $_; $bar = pos})c/, $message);
722 is($foo, $str, $message);
723 is($bar, 2, $message);
84281c31
A
724
725 undef $foo;
726 undef $bar;
4f890a30
NC
727 ok(/b(?{$foo = $_; $bar = pos})c/g, $message);
728 is($foo, $str, $message);
729 is($bar, 2, $message);
730 is(pos, 3, $message);
84281c31
A
731
732 undef $foo;
733 undef $bar;
734 pos = undef;
735 1 while /b(?{$foo = $_; $bar = pos})c/g;
4f890a30
NC
736 is($foo, $str, $message);
737 is($bar, 2, $message);
738 is(pos, undef, $message);
84281c31
A
739
740 undef $foo;
741 undef $bar;
742 $_ = 'abcde|abcde';
4f890a30
NC
743 ok(s/b(?{$foo = $_; $bar = pos})c/x/g, $message);
744 is($foo, 'abcde|abcde', $message);
745 is($bar, 8, $message);
746 is($_, 'axde|axde', $message);
84281c31
A
747
748 # List context:
749 $_ = 'abcde|abcde';
750 our @res;
751 () = /([ace]).(?{push @res, $1,$2})([ce])(?{push @res, $1,$2})/g;
752 @res = map {defined $_ ? "'$_'" : 'undef'} @res;
4f890a30 753 is("@res", "'a' undef 'a' 'c' 'e' undef 'a' undef 'a' 'c'", $message);
84281c31
A
754
755 @res = ();
756 () = /([ace]).(?{push @res, $`,$&,$'})([ce])(?{push @res, $`,$&,$'})/g;
757 @res = map {defined $_ ? "'$_'" : 'undef'} @res;
4f890a30 758 is("@res", "'' 'ab' 'cde|abcde' " .
84281c31
A
759 "'' 'abc' 'de|abcde' " .
760 "'abcd' 'e|' 'abcde' " .
761 "'abcde|' 'ab' 'cde' " .
4f890a30 762 "'abcde|' 'abc' 'de'", $message);
84281c31 763 }
f33976b4 764
84281c31 765 {
4f890a30 766 my $message = '\G anchor checks';
84281c31
A
767 my $foo = 'aabbccddeeffgg';
768 pos ($foo) = 1;
769 {
04934b6d 770 local $::TODO = $::running_as_thread;
84281c31 771 no warnings 'uninitialized';
4f890a30
NC
772 ok($foo =~ /.\G(..)/g, $message);
773 is($1, 'ab', $message);
cce850e4 774
84281c31 775 pos ($foo) += 1;
4f890a30
NC
776 ok($foo =~ /.\G(..)/g, $message);
777 is($1, 'cc', $message);
cce850e4 778
84281c31 779 pos ($foo) += 1;
4f890a30
NC
780 ok($foo =~ /.\G(..)/g, $message);
781 is($1, 'de', $message);
cce850e4 782
4f890a30 783 ok($foo =~ /\Gef/g, $message);
84281c31 784 }
cce850e4 785
84281c31 786 undef pos $foo;
4f890a30
NC
787 ok($foo =~ /\G(..)/g, $message);
788 is($1, 'aa', $message);
cce850e4 789
4f890a30
NC
790 ok($foo =~ /\G(..)/g, $message);
791 is($1, 'bb', $message);
cce850e4 792
84281c31 793 pos ($foo) = 5;
4f890a30
NC
794 ok($foo =~ /\G(..)/g, $message);
795 is($1, 'cd', $message);
84281c31 796 }
cce850e4 797
84281c31
A
798 {
799 $_ = '123x123';
800 my @res = /(\d*|x)/g;
801 local $" = '|';
de26e0cc 802 is("@res", "123||x|123|", "0 match in alternation");
84281c31 803 }
cce850e4 804
84281c31 805 {
4f890a30 806 my $message = "Match against temporaries (created via pp_helem())" .
84281c31 807 " is safe";
4f890a30
NC
808 ok({foo => "bar\n" . $^X} -> {foo} =~ /^(.*)\n/g, $message);
809 is($1, "bar", $message);
84281c31 810 }
75685a94 811
84281c31 812 {
4f890a30 813 my $message = 'package $i inside (?{ }), ' .
84281c31
A
814 'saved substrings and changing $_';
815 our @a = qw [foo bar];
816 our @b = ();
817 s/(\w)(?{push @b, $1})/,$1,/g for @a;
4f890a30
NC
818 is("@b", "f o o b a r", $message);
819 is("@a", ",f,,o,,o, ,b,,a,,r,", $message);
84281c31 820
4f890a30 821 $message = 'lexical $i inside (?{ }), ' .
84281c31
A
822 'saved substrings and changing $_';
823 no warnings 'closure';
824 my @c = qw [foo bar];
825 my @d = ();
826 s/(\w)(?{push @d, $1})/,$1,/g for @c;
4f890a30
NC
827 is("@d", "f o o b a r", $message);
828 is("@c", ",f,,o,,o, ,b,,a,,r,", $message);
d9f424b2
JH
829 }
830
84281c31 831 {
4f890a30 832 my $message = 'Brackets';
84281c31
A
833 our $brackets;
834 $brackets = qr {
835 { (?> [^{}]+ | (??{ $brackets }) )* }
836 }x;
837
4f890a30
NC
838 ok("{{}" =~ $brackets, $message);
839 is($&, "{}", $message);
840 ok("something { long { and } hairy" =~ $brackets, $message);
841 is($&, "{ and }", $message);
842 ok("something { long { and } hairy" =~ m/((??{ $brackets }))/, $message);
843 is($&, "{ and }", $message);
84281c31 844 }
a4c04bdc 845
84281c31
A
846 {
847 $_ = "a-a\nxbb";
848 pos = 1;
b33825c4 849 ok(!m/^-.*bb/mg, '$_ = "a-a\nxbb"; m/^-.*bb/mg');
84281c31 850 }
a4c04bdc 851
84281c31 852 {
4f890a30 853 my $message = '\G anchor checks';
84281c31
A
854 my $text = "aaXbXcc";
855 pos ($text) = 0;
4f890a30 856 ok($text !~ /\GXb*X/g, $message);
84281c31 857 }
a4c04bdc 858
84281c31
A
859 {
860 $_ = "xA\n" x 500;
b33825c4 861 unlike($_, qr/^\s*A/m, '$_ = "xA\n" x 500; /^\s*A/m"');
a4c04bdc 862
84281c31
A
863 my $text = "abc dbf";
864 my @res = ($text =~ /.*?(b).*?\b/g);
de26e0cc 865 is("@res", "b b", '\b is not special');
987aaf07 866 }
a4c04bdc 867
84281c31 868 {
4f890a30 869 my $message = '\S, [\S], \s, [\s]';
84281c31 870 my @a = map chr, 0 .. 255;
9d45b377
YO
871 my @b = grep m/\S/, @a;
872 my @c = grep m/[^\s]/, @a;
4f890a30 873 is("@b", "@c", $message);
84281c31
A
874
875 @b = grep /\S/, @a;
876 @c = grep /[\S]/, @a;
4f890a30 877 is("@b", "@c", $message);
84281c31
A
878
879 @b = grep /\s/, @a;
880 @c = grep /[^\S]/, @a;
4f890a30 881 is("@b", "@c", $message);
84281c31
A
882
883 @b = grep /\s/, @a;
884 @c = grep /[\s]/, @a;
4f890a30 885 is("@b", "@c", $message);
84281c31
A
886 }
887 {
4f890a30 888 my $message = '\D, [\D], \d, [\d]';
84281c31
A
889 my @a = map chr, 0 .. 255;
890 my @b = grep /\D/, @a;
891 my @c = grep /[^\d]/, @a;
4f890a30 892 is("@b", "@c", $message);
84281c31
A
893
894 @b = grep /\D/, @a;
895 @c = grep /[\D]/, @a;
4f890a30 896 is("@b", "@c", $message);
84281c31
A
897
898 @b = grep /\d/, @a;
899 @c = grep /[^\D]/, @a;
4f890a30 900 is("@b", "@c", $message);
84281c31
A
901
902 @b = grep /\d/, @a;
903 @c = grep /[\d]/, @a;
4f890a30 904 is("@b", "@c", $message);
84281c31
A
905 }
906 {
4f890a30 907 my $message = '\W, [\W], \w, [\w]';
84281c31
A
908 my @a = map chr, 0 .. 255;
909 my @b = grep /\W/, @a;
910 my @c = grep /[^\w]/, @a;
4f890a30 911 is("@b", "@c", $message);
84281c31
A
912
913 @b = grep /\W/, @a;
914 @c = grep /[\W]/, @a;
4f890a30 915 is("@b", "@c", $message);
84281c31
A
916
917 @b = grep /\w/, @a;
918 @c = grep /[^\W]/, @a;
4f890a30 919 is("@b", "@c", $message);
84281c31
A
920
921 @b = grep /\w/, @a;
922 @c = grep /[\w]/, @a;
4f890a30 923 is("@b", "@c", $message);
84281c31 924 }
a4c04bdc 925
84281c31
A
926 {
927 # see if backtracking optimization works correctly
4f890a30
NC
928 my $message = 'Backtrack optimization';
929 like("\n\n", qr/\n $ \n/x, $message);
930 like("\n\n", qr/\n* $ \n/x, $message);
931 like("\n\n", qr/\n+ $ \n/x, $message);
932 like("\n\n", qr/\n? $ \n/x, $message);
933 like("\n\n", qr/\n*? $ \n/x, $message);
934 like("\n\n", qr/\n+? $ \n/x, $message);
935 like("\n\n", qr/\n?? $ \n/x, $message);
936 unlike("\n\n", qr/\n*+ $ \n/x, $message);
937 unlike("\n\n", qr/\n++ $ \n/x, $message);
938 like("\n\n", qr/\n?+ $ \n/x, $message);
84281c31 939 }
a4c04bdc 940
84281c31
A
941 {
942 package S;
943 use overload '""' => sub {'Object S'};
944 sub new {bless []}
0f289c68 945
4f890a30 946 my $message = "Ref stringification";
5895685f
NC
947 ::ok(do { \my $v} =~ /^SCALAR/, "Scalar ref stringification") or diag($message);
948 ::ok(do {\\my $v} =~ /^REF/, "Ref ref stringification") or diag($message);
949 ::ok([] =~ /^ARRAY/, "Array ref stringification") or diag($message);
950 ::ok({} =~ /^HASH/, "Hash ref stringification") or diag($message);
951 ::ok('S' -> new =~ /^Object S/, "Object stringification") or diag($message);
84281c31 952 }
a4c04bdc 953
84281c31 954 {
4f890a30
NC
955 my $message = "Test result of match used as match";
956 ok('a1b' =~ ('xyz' =~ /y/), $message);
957 is($`, 'a', $message);
958 ok('a1b' =~ ('xyz' =~ /t/), $message);
959 is($`, 'a', $message);
84281c31 960 }
a4c04bdc 961
84281c31 962 {
d728c370
NC
963 my $message = '"1" is not \s';
964 may_not_warn sub {ok ("1\n" x 102 !~ /^\s*\n/m, $message)}, "$message (did not warn)";
84281c31 965 }
a4c04bdc 966
84281c31 967 {
4f890a30 968 my $message = '\s, [[:space:]] and [[:blank:]]';
84281c31
A
969 my %space = (spc => " ",
970 tab => "\t",
971 cr => "\r",
972 lf => "\n",
973 ff => "\f",
974 # There's no \v but the vertical tabulator seems miraculously
975 # be 11 both in ASCII and EBCDIC.
976 vt => chr(11),
977 false => "space");
978
979 my @space0 = sort grep {$space {$_} =~ /\s/ } keys %space;
980 my @space1 = sort grep {$space {$_} =~ /[[:space:]]/} keys %space;
981 my @space2 = sort grep {$space {$_} =~ /[[:blank:]]/} keys %space;
982
4f890a30
NC
983 is("@space0", "cr ff lf spc tab", $message);
984 is("@space1", "cr ff lf spc tab vt", $message);
985 is("@space2", "spc tab", $message);
84281c31 986 }
a4c04bdc 987
ff3f963a
KW
988 {
989 use charnames ":full";
4f890a30 990 # Delayed interpolation of \N'
ff3f963a
KW
991 my $r1 = qr/\N{THAI CHARACTER SARA I}/;
992 my $s1 = "\x{E34}\x{E34}\x{E34}\x{E34}";
993
994 # Bug #56444
995 ok $s1 =~ /$r1+/, 'my $r1 = qr/\N{THAI CHARACTER SARA I}/; my $s1 = "\x{E34}\x{E34}\x{E34}\x{E34}; $s1 =~ /$r1+/';
996
997 # Bug #62056
998 ok "${s1}A" =~ m/$s1\N{LATIN CAPITAL LETTER A}/, '"${s1}A" =~ m/$s1\N{LATIN CAPITAL LETTER A}/';
999
1000 ok "abbbbc" =~ m/\N{1}/ && $& eq "a", '"abbbbc" =~ m/\N{1}/ && $& eq "a"';
1001 ok "abbbbc" =~ m/\N{3,4}/ && $& eq "abbb", '"abbbbc" =~ m/\N{3,4}/ && $& eq "abbb"';
1002 }
1003
37820adc
KW
1004 {
1005 use charnames ":full";
4f890a30
NC
1006 my $message = '[perl #74982] Period coming after \N{}';
1007 ok("\x{ff08}." =~ m/\N{FULLWIDTH LEFT PARENTHESIS}./ && $& eq "\x{ff08}.", $message);
1008 ok("\x{ff08}." =~ m/[\N{FULLWIDTH LEFT PARENTHESIS}]./ && $& eq "\x{ff08}.", $message);
37820adc 1009 }
c9415951
YO
1010 {
1011 my $n= 50;
93f09d7b 1012 # this must be a high number and go from 0 to N, as the bug we are looking for doesn't
c9415951
YO
1013 # seem to be predictable. Slight changes to the test make it fail earlier or later.
1014 foreach my $i (0 .. $n)
1015 {
1016 my $str= "\n" x $i;
93f09d7b 1017 ok $str=~/.*\z/, "implicit MBOL check string disable does not break things length=$i";
c9415951
YO
1018 }
1019 }
92f3d482
YO
1020 {
1021 # we are actually testing that we dont die when executing these patterns
1022 use utf8;
1023 my $e = "Böck";
1024 ok(utf8::is_utf8($e),"got a unicode string - rt75680");
1025
1026 ok($e !~ m/.*?[x]$/, "unicode string against /.*?[x]\$/ - rt75680");
1027 ok($e !~ m/.*?\p{Space}$/i, "unicode string against /.*?\\p{space}\$/i - rt75680");
1028 ok($e !~ m/.*?[xyz]$/, "unicode string against /.*?[xyz]\$/ - rt75680");
1029 ok($e !~ m/(.*?)[,\p{isSpace}]+((?:\p{isAlpha}[\p{isSpace}\.]{1,2})+)\p{isSpace}*$/, "unicode string against big pattern - rt75680");
1030 }
1031 {
1032 # we are actually testing that we dont die when executing these patterns
1033 my $e = "B\x{f6}ck";
1034 ok(!utf8::is_utf8($e), "got a latin string - rt75680");
1035
1036 ok($e !~ m/.*?[x]$/, "latin string against /.*?[x]\$/ - rt75680");
1037 ok($e !~ m/.*?\p{Space}$/i, "latin string against /.*?\\p{space}\$/i - rt75680");
1038 ok($e !~ m/.*?[xyz]$/,"latin string against /.*?[xyz]\$/ - rt75680");
1039 ok($e !~ m/(.*?)[,\p{isSpace}]+((?:\p{isAlpha}[\p{isSpace}\.]{1,2})+)\p{isSpace}*$/,"latin string against big pattern - rt75680");
1040 }
c920e018
A
1041
1042 {
1043 #
1044 # Tests for bug 77414.
1045 #
1046
4f890a30 1047 my $message = '\p property after empty * match';
c920e018 1048 {
04934b6d 1049 local $::TODO = "Bug 77414";
4f890a30
NC
1050 like("1", qr/\s*\pN/, $message);
1051 like("-", qr/\s*\p{Dash}/, $message);
1052 like(" ", qr/\w*\p{Blank}/, $message);
c920e018
A
1053 }
1054
4f890a30
NC
1055 like("1", qr/\s*\pN+/, $message);
1056 like("-", qr/\s*\p{Dash}{1}/, $message);
1057 like(" ", qr/\w*\p{Blank}{1,4}/, $message);
c920e018
A
1058
1059 }
1060
7c17ea2f
KW
1061 SKIP: { # Some constructs with Latin1 characters cause a utf8 string not
1062 # to match itself in non-utf8
1063 if ($IS_EBCDIC) {
1064 skip "Needs to be customized to run on EBCDIC", 6;
1065 }
634c83a2
KW
1066 my $c = "\xc0";
1067 my $pattern = my $utf8_pattern = qr/((\xc0)+,?)/;
1068 utf8::upgrade($utf8_pattern);
1069 ok $c =~ $pattern, "\\xc0 =~ $pattern; Neither pattern nor target utf8";
d4e0b827 1070 ok $c =~ /$pattern/i, "\\xc0 =~ /$pattern/i; Neither pattern nor target utf8";
634c83a2 1071 ok $c =~ $utf8_pattern, "\\xc0 =~ $pattern; pattern utf8, target not";
d4e0b827 1072 ok $c =~ /$utf8_pattern/i, "\\xc0 =~ /$pattern/i; pattern utf8, target not";
634c83a2
KW
1073 utf8::upgrade($c);
1074 ok $c =~ $pattern, "\\xc0 =~ $pattern; target utf8, pattern not";
d4e0b827 1075 ok $c =~ /$pattern/i, "\\xc0 =~ /$pattern/i; target utf8, pattern not";
634c83a2 1076 ok $c =~ $utf8_pattern, "\\xc0 =~ $pattern; Both target and pattern utf8";
d4e0b827 1077 ok $c =~ /$utf8_pattern/i, "\\xc0 =~ /$pattern/i; Both target and pattern utf8";
634c83a2
KW
1078 }
1079
8cc86590
KW
1080 SKIP: { # Make sure can override the formatting
1081 if ($IS_EBCDIC) {
1082 skip "Needs to be customized to run on EBCDIC", 2;
1083 }
1084 use feature 'unicode_strings';
1085 ok "\xc0" =~ /\w/, 'Under unicode_strings: "\xc0" =~ /\w/';
1086 ok "\xc0" !~ /(?d:\w)/, 'Under unicode_strings: "\xc0" !~ /(?d:\w)/';
1087 }
1088
704f71be
B
1089 {
1090 # Test that a regex followed by an operator and/or a statement modifier work
1091 # These tests use string-eval so that it reports a clean error when it fails
1092 # (without the string eval the test script might be unparseable)
1093
1094 # Note: these test check the behaviour that currently is valid syntax
93f09d7b 1095 # If a new regex modifier is added and a test fails then there is a backwards-compatibility issue
704f71be
B
1096 # Note-2: a new deprecate warning was added for this with commit e6897b1a5db0410e387ccbf677e89fc4a1d8c97a
1097 # which indicate that this syntax will be removed in 5.16.
1098 # When this happens the tests can be removed
1099
1100 no warnings 'syntax';
de26e0cc
NC
1101 is(eval q#my $r = "a" =~ m/a/lt 2;"eval_ok $r"#, "eval_ok 1", "regex (m//) followed by lt");
1102 is(eval q#my $r = "a" =~ m/a/le 1;"eval_ok $r"#, "eval_ok 1", "regex (m//) followed by le");
1103 is(eval q#my $r = "a" =~ m/a/eq 1;"eval_ok $r"#, "eval_ok 1", "regex (m//) followed by eq");
1104 is(eval q#my $r = "a" =~ m/a/ne 0;"eval_ok $r"#, "eval_ok 1", "regex (m//) followed by ne");
1105 is(eval q#my $r = "a" =~ m/a/and 1;"eval_ok $r"#, "eval_ok 1", "regex (m//) followed by and");
1106 is(eval q#my $r = "a" =~ m/a/unless 0;"eval_ok $r"#, "eval_ok 1", "regex (m//) followed by unless");
1107 is(eval q#my $c = 1; my $r; $r = "a" =~ m/a/while $c--;"eval_ok $r"#, "eval_ok 1", "regex (m//) followed by while");
1108 is(eval q#my $c = 0; my $r; $r = "a" =~ m/a/until $c++;"eval_ok $r"#, "eval_ok 1", "regex (m//) followed by until");
1109 is(eval q#my $r; $r = "a" =~ m/a/for 1;"eval_ok $r"#, "eval_ok 1", "regex (m//) followed by for");
1110 is(eval q#my $r; $r = "a" =~ m/a/foreach 1;"eval_ok $r"#, "eval_ok 1", "regex (m//) followed by foreach");
1111
1112 is(eval q#my $t = "a"; my $r = $t =~ s/a//lt 2;"eval_ok $r"#, "eval_ok 1", "regex (s///) followed by lt");
1113 is(eval q#my $t = "a"; my $r = $t =~ s/a//le 1;"eval_ok $r"#, "eval_ok 1", "regex (s///) followed by le");
1114 is(eval q#my $t = "a"; my $r = $t =~ s/a//ne 0;"eval_ok $r"#, "eval_ok 1", "regex (s///) followed by ne");
1115 is(eval q#my $t = "a"; my $r = $t =~ s/a//and 1;"eval_ok $r"#, "eval_ok 1", "regex (s///) followed by and");
1116 is(eval q#my $t = "a"; my $r = $t =~ s/a//unless 0;"eval_ok $r"#, "eval_ok 1", "regex (s///) followed by unless");
1117
1118 is(eval q#my $c = 1; my $r; my $t = "a"; $r = $t =~ s/a//while $c--;"eval_ok $r"#, "eval_ok 1", "regex (s///) followed by while");
1119 is(eval q#my $c = 0; my $r; my $t = "a"; $r = $t =~ s/a//until $c++;"eval_ok $r"#, "eval_ok 1", "regex (s///) followed by until");
1120 is(eval q#my $r; my $t = "a"; $r = $t =~ s/a//for 1;"eval_ok $r"#, "eval_ok 1", "regex (s///) followed by for");
1121 is(eval q#my $r; my $t = "a"; $r = $t =~ s/a//for 1;"eval_ok $r"#, "eval_ok 1", "regex (s///) followed by foreach");
704f71be
B
1122 }
1123
5b6010b3
YO
1124 {
1125 my $str= "\x{100}";
1126 chop $str;
1127 my $qr= qr/$str/;
de26e0cc 1128 is("$qr", "(?^:)", "Empty pattern qr// stringifies to (?^:) with unicode flag enabled - Bug #80212");
5b6010b3
YO
1129 $str= "";
1130 $qr= qr/$str/;
de26e0cc 1131 is("$qr", "(?^:)", "Empty pattern qr// stringifies to (?^:) with unicode flag disabled - Bug #80212");
5b6010b3
YO
1132
1133 }
1134
72aa120d 1135 {
04934b6d 1136 local $::TODO = "[perl #38133]";
72aa120d
KW
1137
1138 "A" =~ /(((?:A))?)+/;
1139 my $first = $2;
1140
1141 "A" =~ /(((A))?)+/;
1142 my $second = $2;
1143
de26e0cc 1144 is($first, $second);
72aa120d
KW
1145 }
1146
99ca48e1
DM
1147 {
1148 # RT #3516: \G in a m//g expression causes problems
1149 my $count = 0;
1150 while ("abc" =~ m/(\G[ac])?/g) {
1151 last if $count++ > 10;
1152 }
1153 ok($count < 10, 'RT #3516 A');
1154
1155 $count = 0;
1156 while ("abc" =~ m/(\G|.)[ac]/g) {
1157 last if $count++ > 10;
1158 }
1159 ok($count < 10, 'RT #3516 B');
1160
1161 $count = 0;
1162 while ("abc" =~ m/(\G?[ac])?/g) {
1163 last if $count++ > 10;
1164 }
1165 ok($count < 10, 'RT #3516 C');
1166 }
84281c31
A
1167} # End of sub run_tests
1168
11691;