This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add warnings_like() in test.pl to replace must_warn() in ReTest.pl.
[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
cb124425 24plan tests => 433; # 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 586 {
cb124425
NC
587 is(eval 'q(a:[b]:) =~ /[x[:foo:]]/', undef);
588 like ($@, qr/POSIX class \[:[^:]+:\] unknown in regex/,
589 'POSIX class [: :] must have valid name');
84281c31
A
590
591 for my $d (qw [= .]) {
cb124425
NC
592 is(eval "/[[${d}foo${d}]]/", undef);
593 like ($@, qr/\QPOSIX syntax [$d $d] is reserved for future extensions/,
594 "POSIX syntax [[$d $d]] is an error");
84281c31
A
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 677 foreach ('$+[0] = 13', '$-[0] = 13', '@+ = (7, 6, 5)', '@- = qw (foo bar)') {
cb124425
NC
678 is(eval $_, undef);
679 like($@, qr/^Modification of a read-only value attempted/,
680 'Elements of @- and @+ are read-only');
84281c31 681 }
a72deede 682
84281c31 683 {
4f890a30 684 my $message = '\G testing';
84281c31
A
685 $_ = 'aaa';
686 pos = 1;
687 my @a = /\Ga/g;
4f890a30 688 is("@a", "a a", $message);
84281c31
A
689
690 my $str = 'abcde';
691 pos $str = 2;
4f890a30
NC
692 unlike($str, qr/^\G/, $message);
693 unlike($str, qr/^.\G/, $message);
694 like($str, qr/^..\G/, $message);
695 unlike($str, qr/^...\G/, $message);
696 ok($str =~ /\G../ && $& eq 'cd', $message);
84281c31 697
04934b6d 698 local $::TODO = $::running_as_thread;
4f890a30 699 ok($str =~ /.\G./ && $& eq 'bc', $message);
84281c31 700 }
a72deede 701
84281c31 702 {
4f890a30 703 my $message = 'pos inside (?{ })';
84281c31
A
704 my $str = 'abcde';
705 our ($foo, $bar);
4f890a30
NC
706 like($str, qr/b(?{$foo = $_; $bar = pos})c/, $message);
707 is($foo, $str, $message);
708 is($bar, 2, $message);
709 is(pos $str, undef, $message);
84281c31
A
710
711 undef $foo;
712 undef $bar;
713 pos $str = undef;
4f890a30
NC
714 ok($str =~ /b(?{$foo = $_; $bar = pos})c/g, $message);
715 is($foo, $str, $message);
716 is($bar, 2, $message);
717 is(pos $str, 3, $message);
84281c31
A
718
719 $_ = $str;
720 undef $foo;
721 undef $bar;
4f890a30
NC
722 like($_, qr/b(?{$foo = $_; $bar = pos})c/, $message);
723 is($foo, $str, $message);
724 is($bar, 2, $message);
84281c31
A
725
726 undef $foo;
727 undef $bar;
4f890a30
NC
728 ok(/b(?{$foo = $_; $bar = pos})c/g, $message);
729 is($foo, $str, $message);
730 is($bar, 2, $message);
731 is(pos, 3, $message);
84281c31
A
732
733 undef $foo;
734 undef $bar;
735 pos = undef;
736 1 while /b(?{$foo = $_; $bar = pos})c/g;
4f890a30
NC
737 is($foo, $str, $message);
738 is($bar, 2, $message);
739 is(pos, undef, $message);
84281c31
A
740
741 undef $foo;
742 undef $bar;
743 $_ = 'abcde|abcde';
4f890a30
NC
744 ok(s/b(?{$foo = $_; $bar = pos})c/x/g, $message);
745 is($foo, 'abcde|abcde', $message);
746 is($bar, 8, $message);
747 is($_, 'axde|axde', $message);
84281c31
A
748
749 # List context:
750 $_ = 'abcde|abcde';
751 our @res;
752 () = /([ace]).(?{push @res, $1,$2})([ce])(?{push @res, $1,$2})/g;
753 @res = map {defined $_ ? "'$_'" : 'undef'} @res;
4f890a30 754 is("@res", "'a' undef 'a' 'c' 'e' undef 'a' undef 'a' 'c'", $message);
84281c31
A
755
756 @res = ();
757 () = /([ace]).(?{push @res, $`,$&,$'})([ce])(?{push @res, $`,$&,$'})/g;
758 @res = map {defined $_ ? "'$_'" : 'undef'} @res;
4f890a30 759 is("@res", "'' 'ab' 'cde|abcde' " .
84281c31
A
760 "'' 'abc' 'de|abcde' " .
761 "'abcd' 'e|' 'abcde' " .
762 "'abcde|' 'ab' 'cde' " .
4f890a30 763 "'abcde|' 'abc' 'de'", $message);
84281c31 764 }
f33976b4 765
84281c31 766 {
4f890a30 767 my $message = '\G anchor checks';
84281c31
A
768 my $foo = 'aabbccddeeffgg';
769 pos ($foo) = 1;
770 {
04934b6d 771 local $::TODO = $::running_as_thread;
84281c31 772 no warnings 'uninitialized';
4f890a30
NC
773 ok($foo =~ /.\G(..)/g, $message);
774 is($1, 'ab', $message);
cce850e4 775
84281c31 776 pos ($foo) += 1;
4f890a30
NC
777 ok($foo =~ /.\G(..)/g, $message);
778 is($1, 'cc', $message);
cce850e4 779
84281c31 780 pos ($foo) += 1;
4f890a30
NC
781 ok($foo =~ /.\G(..)/g, $message);
782 is($1, 'de', $message);
cce850e4 783
4f890a30 784 ok($foo =~ /\Gef/g, $message);
84281c31 785 }
cce850e4 786
84281c31 787 undef pos $foo;
4f890a30
NC
788 ok($foo =~ /\G(..)/g, $message);
789 is($1, 'aa', $message);
cce850e4 790
4f890a30
NC
791 ok($foo =~ /\G(..)/g, $message);
792 is($1, 'bb', $message);
cce850e4 793
84281c31 794 pos ($foo) = 5;
4f890a30
NC
795 ok($foo =~ /\G(..)/g, $message);
796 is($1, 'cd', $message);
84281c31 797 }
cce850e4 798
84281c31
A
799 {
800 $_ = '123x123';
801 my @res = /(\d*|x)/g;
802 local $" = '|';
de26e0cc 803 is("@res", "123||x|123|", "0 match in alternation");
84281c31 804 }
cce850e4 805
84281c31 806 {
4f890a30 807 my $message = "Match against temporaries (created via pp_helem())" .
84281c31 808 " is safe";
4f890a30
NC
809 ok({foo => "bar\n" . $^X} -> {foo} =~ /^(.*)\n/g, $message);
810 is($1, "bar", $message);
84281c31 811 }
75685a94 812
84281c31 813 {
4f890a30 814 my $message = 'package $i inside (?{ }), ' .
84281c31
A
815 'saved substrings and changing $_';
816 our @a = qw [foo bar];
817 our @b = ();
818 s/(\w)(?{push @b, $1})/,$1,/g for @a;
4f890a30
NC
819 is("@b", "f o o b a r", $message);
820 is("@a", ",f,,o,,o, ,b,,a,,r,", $message);
84281c31 821
4f890a30 822 $message = 'lexical $i inside (?{ }), ' .
84281c31
A
823 'saved substrings and changing $_';
824 no warnings 'closure';
825 my @c = qw [foo bar];
826 my @d = ();
827 s/(\w)(?{push @d, $1})/,$1,/g for @c;
4f890a30
NC
828 is("@d", "f o o b a r", $message);
829 is("@c", ",f,,o,,o, ,b,,a,,r,", $message);
d9f424b2
JH
830 }
831
84281c31 832 {
4f890a30 833 my $message = 'Brackets';
84281c31
A
834 our $brackets;
835 $brackets = qr {
836 { (?> [^{}]+ | (??{ $brackets }) )* }
837 }x;
838
4f890a30
NC
839 ok("{{}" =~ $brackets, $message);
840 is($&, "{}", $message);
841 ok("something { long { and } hairy" =~ $brackets, $message);
842 is($&, "{ and }", $message);
843 ok("something { long { and } hairy" =~ m/((??{ $brackets }))/, $message);
844 is($&, "{ and }", $message);
84281c31 845 }
a4c04bdc 846
84281c31
A
847 {
848 $_ = "a-a\nxbb";
849 pos = 1;
b33825c4 850 ok(!m/^-.*bb/mg, '$_ = "a-a\nxbb"; m/^-.*bb/mg');
84281c31 851 }
a4c04bdc 852
84281c31 853 {
4f890a30 854 my $message = '\G anchor checks';
84281c31
A
855 my $text = "aaXbXcc";
856 pos ($text) = 0;
4f890a30 857 ok($text !~ /\GXb*X/g, $message);
84281c31 858 }
a4c04bdc 859
84281c31
A
860 {
861 $_ = "xA\n" x 500;
b33825c4 862 unlike($_, qr/^\s*A/m, '$_ = "xA\n" x 500; /^\s*A/m"');
a4c04bdc 863
84281c31
A
864 my $text = "abc dbf";
865 my @res = ($text =~ /.*?(b).*?\b/g);
de26e0cc 866 is("@res", "b b", '\b is not special');
987aaf07 867 }
a4c04bdc 868
84281c31 869 {
4f890a30 870 my $message = '\S, [\S], \s, [\s]';
84281c31 871 my @a = map chr, 0 .. 255;
9d45b377
YO
872 my @b = grep m/\S/, @a;
873 my @c = grep m/[^\s]/, @a;
4f890a30 874 is("@b", "@c", $message);
84281c31
A
875
876 @b = grep /\S/, @a;
877 @c = grep /[\S]/, @a;
4f890a30 878 is("@b", "@c", $message);
84281c31
A
879
880 @b = grep /\s/, @a;
881 @c = grep /[^\S]/, @a;
4f890a30 882 is("@b", "@c", $message);
84281c31
A
883
884 @b = grep /\s/, @a;
885 @c = grep /[\s]/, @a;
4f890a30 886 is("@b", "@c", $message);
84281c31
A
887 }
888 {
4f890a30 889 my $message = '\D, [\D], \d, [\d]';
84281c31
A
890 my @a = map chr, 0 .. 255;
891 my @b = grep /\D/, @a;
892 my @c = grep /[^\d]/, @a;
4f890a30 893 is("@b", "@c", $message);
84281c31
A
894
895 @b = grep /\D/, @a;
896 @c = grep /[\D]/, @a;
4f890a30 897 is("@b", "@c", $message);
84281c31
A
898
899 @b = grep /\d/, @a;
900 @c = grep /[^\D]/, @a;
4f890a30 901 is("@b", "@c", $message);
84281c31
A
902
903 @b = grep /\d/, @a;
904 @c = grep /[\d]/, @a;
4f890a30 905 is("@b", "@c", $message);
84281c31
A
906 }
907 {
4f890a30 908 my $message = '\W, [\W], \w, [\w]';
84281c31
A
909 my @a = map chr, 0 .. 255;
910 my @b = grep /\W/, @a;
911 my @c = grep /[^\w]/, @a;
4f890a30 912 is("@b", "@c", $message);
84281c31
A
913
914 @b = grep /\W/, @a;
915 @c = grep /[\W]/, @a;
4f890a30 916 is("@b", "@c", $message);
84281c31
A
917
918 @b = grep /\w/, @a;
919 @c = grep /[^\W]/, @a;
4f890a30 920 is("@b", "@c", $message);
84281c31
A
921
922 @b = grep /\w/, @a;
923 @c = grep /[\w]/, @a;
4f890a30 924 is("@b", "@c", $message);
84281c31 925 }
a4c04bdc 926
84281c31
A
927 {
928 # see if backtracking optimization works correctly
4f890a30
NC
929 my $message = 'Backtrack optimization';
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 like("\n\n", qr/\n?? $ \n/x, $message);
937 unlike("\n\n", qr/\n*+ $ \n/x, $message);
938 unlike("\n\n", qr/\n++ $ \n/x, $message);
939 like("\n\n", qr/\n?+ $ \n/x, $message);
84281c31 940 }
a4c04bdc 941
84281c31
A
942 {
943 package S;
944 use overload '""' => sub {'Object S'};
945 sub new {bless []}
0f289c68 946
4f890a30 947 my $message = "Ref stringification";
5895685f
NC
948 ::ok(do { \my $v} =~ /^SCALAR/, "Scalar ref stringification") or diag($message);
949 ::ok(do {\\my $v} =~ /^REF/, "Ref ref stringification") or diag($message);
950 ::ok([] =~ /^ARRAY/, "Array ref stringification") or diag($message);
951 ::ok({} =~ /^HASH/, "Hash ref stringification") or diag($message);
952 ::ok('S' -> new =~ /^Object S/, "Object stringification") or diag($message);
84281c31 953 }
a4c04bdc 954
84281c31 955 {
4f890a30
NC
956 my $message = "Test result of match used as match";
957 ok('a1b' =~ ('xyz' =~ /y/), $message);
958 is($`, 'a', $message);
959 ok('a1b' =~ ('xyz' =~ /t/), $message);
960 is($`, 'a', $message);
84281c31 961 }
a4c04bdc 962
84281c31 963 {
d728c370 964 my $message = '"1" is not \s';
c11a8df3
NC
965 warning_is(sub {unlike("1\n" x 102, qr/^\s*\n/m, $message)},
966 undef, "$message (did not warn)");
84281c31 967 }
a4c04bdc 968
84281c31 969 {
4f890a30 970 my $message = '\s, [[:space:]] and [[:blank:]]';
84281c31
A
971 my %space = (spc => " ",
972 tab => "\t",
973 cr => "\r",
974 lf => "\n",
975 ff => "\f",
976 # There's no \v but the vertical tabulator seems miraculously
977 # be 11 both in ASCII and EBCDIC.
978 vt => chr(11),
979 false => "space");
980
981 my @space0 = sort grep {$space {$_} =~ /\s/ } keys %space;
982 my @space1 = sort grep {$space {$_} =~ /[[:space:]]/} keys %space;
983 my @space2 = sort grep {$space {$_} =~ /[[:blank:]]/} keys %space;
984
4f890a30
NC
985 is("@space0", "cr ff lf spc tab", $message);
986 is("@space1", "cr ff lf spc tab vt", $message);
987 is("@space2", "spc tab", $message);
84281c31 988 }
a4c04bdc 989
ff3f963a
KW
990 {
991 use charnames ":full";
4f890a30 992 # Delayed interpolation of \N'
ff3f963a
KW
993 my $r1 = qr/\N{THAI CHARACTER SARA I}/;
994 my $s1 = "\x{E34}\x{E34}\x{E34}\x{E34}";
995
996 # Bug #56444
997 ok $s1 =~ /$r1+/, 'my $r1 = qr/\N{THAI CHARACTER SARA I}/; my $s1 = "\x{E34}\x{E34}\x{E34}\x{E34}; $s1 =~ /$r1+/';
998
999 # Bug #62056
1000 ok "${s1}A" =~ m/$s1\N{LATIN CAPITAL LETTER A}/, '"${s1}A" =~ m/$s1\N{LATIN CAPITAL LETTER A}/';
1001
1002 ok "abbbbc" =~ m/\N{1}/ && $& eq "a", '"abbbbc" =~ m/\N{1}/ && $& eq "a"';
1003 ok "abbbbc" =~ m/\N{3,4}/ && $& eq "abbb", '"abbbbc" =~ m/\N{3,4}/ && $& eq "abbb"';
1004 }
1005
37820adc
KW
1006 {
1007 use charnames ":full";
4f890a30
NC
1008 my $message = '[perl #74982] Period coming after \N{}';
1009 ok("\x{ff08}." =~ m/\N{FULLWIDTH LEFT PARENTHESIS}./ && $& eq "\x{ff08}.", $message);
1010 ok("\x{ff08}." =~ m/[\N{FULLWIDTH LEFT PARENTHESIS}]./ && $& eq "\x{ff08}.", $message);
37820adc 1011 }
c9415951
YO
1012 {
1013 my $n= 50;
93f09d7b 1014 # this must be a high number and go from 0 to N, as the bug we are looking for doesn't
c9415951
YO
1015 # seem to be predictable. Slight changes to the test make it fail earlier or later.
1016 foreach my $i (0 .. $n)
1017 {
1018 my $str= "\n" x $i;
93f09d7b 1019 ok $str=~/.*\z/, "implicit MBOL check string disable does not break things length=$i";
c9415951
YO
1020 }
1021 }
92f3d482
YO
1022 {
1023 # we are actually testing that we dont die when executing these patterns
1024 use utf8;
1025 my $e = "Böck";
1026 ok(utf8::is_utf8($e),"got a unicode string - rt75680");
1027
1028 ok($e !~ m/.*?[x]$/, "unicode string against /.*?[x]\$/ - rt75680");
1029 ok($e !~ m/.*?\p{Space}$/i, "unicode string against /.*?\\p{space}\$/i - rt75680");
1030 ok($e !~ m/.*?[xyz]$/, "unicode string against /.*?[xyz]\$/ - rt75680");
1031 ok($e !~ m/(.*?)[,\p{isSpace}]+((?:\p{isAlpha}[\p{isSpace}\.]{1,2})+)\p{isSpace}*$/, "unicode string against big pattern - rt75680");
1032 }
1033 {
1034 # we are actually testing that we dont die when executing these patterns
1035 my $e = "B\x{f6}ck";
1036 ok(!utf8::is_utf8($e), "got a latin string - rt75680");
1037
1038 ok($e !~ m/.*?[x]$/, "latin string against /.*?[x]\$/ - rt75680");
1039 ok($e !~ m/.*?\p{Space}$/i, "latin string against /.*?\\p{space}\$/i - rt75680");
1040 ok($e !~ m/.*?[xyz]$/,"latin string against /.*?[xyz]\$/ - rt75680");
1041 ok($e !~ m/(.*?)[,\p{isSpace}]+((?:\p{isAlpha}[\p{isSpace}\.]{1,2})+)\p{isSpace}*$/,"latin string against big pattern - rt75680");
1042 }
c920e018
A
1043
1044 {
1045 #
1046 # Tests for bug 77414.
1047 #
1048
4f890a30 1049 my $message = '\p property after empty * match';
c920e018 1050 {
04934b6d 1051 local $::TODO = "Bug 77414";
4f890a30
NC
1052 like("1", qr/\s*\pN/, $message);
1053 like("-", qr/\s*\p{Dash}/, $message);
1054 like(" ", qr/\w*\p{Blank}/, $message);
c920e018
A
1055 }
1056
4f890a30
NC
1057 like("1", qr/\s*\pN+/, $message);
1058 like("-", qr/\s*\p{Dash}{1}/, $message);
1059 like(" ", qr/\w*\p{Blank}{1,4}/, $message);
c920e018
A
1060
1061 }
1062
7c17ea2f
KW
1063 SKIP: { # Some constructs with Latin1 characters cause a utf8 string not
1064 # to match itself in non-utf8
1065 if ($IS_EBCDIC) {
1066 skip "Needs to be customized to run on EBCDIC", 6;
1067 }
634c83a2
KW
1068 my $c = "\xc0";
1069 my $pattern = my $utf8_pattern = qr/((\xc0)+,?)/;
1070 utf8::upgrade($utf8_pattern);
1071 ok $c =~ $pattern, "\\xc0 =~ $pattern; Neither pattern nor target utf8";
d4e0b827 1072 ok $c =~ /$pattern/i, "\\xc0 =~ /$pattern/i; Neither pattern nor target utf8";
634c83a2 1073 ok $c =~ $utf8_pattern, "\\xc0 =~ $pattern; pattern utf8, target not";
d4e0b827 1074 ok $c =~ /$utf8_pattern/i, "\\xc0 =~ /$pattern/i; pattern utf8, target not";
634c83a2
KW
1075 utf8::upgrade($c);
1076 ok $c =~ $pattern, "\\xc0 =~ $pattern; target utf8, pattern not";
d4e0b827 1077 ok $c =~ /$pattern/i, "\\xc0 =~ /$pattern/i; target utf8, pattern not";
634c83a2 1078 ok $c =~ $utf8_pattern, "\\xc0 =~ $pattern; Both target and pattern utf8";
d4e0b827 1079 ok $c =~ /$utf8_pattern/i, "\\xc0 =~ /$pattern/i; Both target and pattern utf8";
634c83a2
KW
1080 }
1081
8cc86590
KW
1082 SKIP: { # Make sure can override the formatting
1083 if ($IS_EBCDIC) {
1084 skip "Needs to be customized to run on EBCDIC", 2;
1085 }
1086 use feature 'unicode_strings';
1087 ok "\xc0" =~ /\w/, 'Under unicode_strings: "\xc0" =~ /\w/';
1088 ok "\xc0" !~ /(?d:\w)/, 'Under unicode_strings: "\xc0" !~ /(?d:\w)/';
1089 }
1090
704f71be
B
1091 {
1092 # Test that a regex followed by an operator and/or a statement modifier work
1093 # These tests use string-eval so that it reports a clean error when it fails
1094 # (without the string eval the test script might be unparseable)
1095
1096 # Note: these test check the behaviour that currently is valid syntax
93f09d7b 1097 # If a new regex modifier is added and a test fails then there is a backwards-compatibility issue
704f71be
B
1098 # Note-2: a new deprecate warning was added for this with commit e6897b1a5db0410e387ccbf677e89fc4a1d8c97a
1099 # which indicate that this syntax will be removed in 5.16.
1100 # When this happens the tests can be removed
1101
1102 no warnings 'syntax';
de26e0cc
NC
1103 is(eval q#my $r = "a" =~ m/a/lt 2;"eval_ok $r"#, "eval_ok 1", "regex (m//) followed by lt");
1104 is(eval q#my $r = "a" =~ m/a/le 1;"eval_ok $r"#, "eval_ok 1", "regex (m//) followed by le");
1105 is(eval q#my $r = "a" =~ m/a/eq 1;"eval_ok $r"#, "eval_ok 1", "regex (m//) followed by eq");
1106 is(eval q#my $r = "a" =~ m/a/ne 0;"eval_ok $r"#, "eval_ok 1", "regex (m//) followed by ne");
1107 is(eval q#my $r = "a" =~ m/a/and 1;"eval_ok $r"#, "eval_ok 1", "regex (m//) followed by and");
1108 is(eval q#my $r = "a" =~ m/a/unless 0;"eval_ok $r"#, "eval_ok 1", "regex (m//) followed by unless");
1109 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");
1110 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");
1111 is(eval q#my $r; $r = "a" =~ m/a/for 1;"eval_ok $r"#, "eval_ok 1", "regex (m//) followed by for");
1112 is(eval q#my $r; $r = "a" =~ m/a/foreach 1;"eval_ok $r"#, "eval_ok 1", "regex (m//) followed by foreach");
1113
1114 is(eval q#my $t = "a"; my $r = $t =~ s/a//lt 2;"eval_ok $r"#, "eval_ok 1", "regex (s///) followed by lt");
1115 is(eval q#my $t = "a"; my $r = $t =~ s/a//le 1;"eval_ok $r"#, "eval_ok 1", "regex (s///) followed by le");
1116 is(eval q#my $t = "a"; my $r = $t =~ s/a//ne 0;"eval_ok $r"#, "eval_ok 1", "regex (s///) followed by ne");
1117 is(eval q#my $t = "a"; my $r = $t =~ s/a//and 1;"eval_ok $r"#, "eval_ok 1", "regex (s///) followed by and");
1118 is(eval q#my $t = "a"; my $r = $t =~ s/a//unless 0;"eval_ok $r"#, "eval_ok 1", "regex (s///) followed by unless");
1119
1120 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");
1121 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");
1122 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");
1123 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
1124 }
1125
5b6010b3
YO
1126 {
1127 my $str= "\x{100}";
1128 chop $str;
1129 my $qr= qr/$str/;
de26e0cc 1130 is("$qr", "(?^:)", "Empty pattern qr// stringifies to (?^:) with unicode flag enabled - Bug #80212");
5b6010b3
YO
1131 $str= "";
1132 $qr= qr/$str/;
de26e0cc 1133 is("$qr", "(?^:)", "Empty pattern qr// stringifies to (?^:) with unicode flag disabled - Bug #80212");
5b6010b3
YO
1134
1135 }
1136
72aa120d 1137 {
04934b6d 1138 local $::TODO = "[perl #38133]";
72aa120d
KW
1139
1140 "A" =~ /(((?:A))?)+/;
1141 my $first = $2;
1142
1143 "A" =~ /(((A))?)+/;
1144 my $second = $2;
1145
de26e0cc 1146 is($first, $second);
72aa120d
KW
1147 }
1148
99ca48e1
DM
1149 {
1150 # RT #3516: \G in a m//g expression causes problems
1151 my $count = 0;
1152 while ("abc" =~ m/(\G[ac])?/g) {
1153 last if $count++ > 10;
1154 }
1155 ok($count < 10, 'RT #3516 A');
1156
1157 $count = 0;
1158 while ("abc" =~ m/(\G|.)[ac]/g) {
1159 last if $count++ > 10;
1160 }
1161 ok($count < 10, 'RT #3516 B');
1162
1163 $count = 0;
1164 while ("abc" =~ m/(\G?[ac])?/g) {
1165 last if $count++ > 10;
1166 }
1167 ok($count < 10, 'RT #3516 C');
1168 }
84281c31
A
1169} # End of sub run_tests
1170
11711;