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