7 require Config; Config->import;
9 constant->import(constcow => *Config::{NAME});
10 require './charset_tools.pl';
11 require './loc_tools.pl';
18 ok( $_ eq 'david' && $a eq 'rules', 'non-destructive substitute' );
20 $a = "david" =~ s/david/rules/r;
21 ok( $a eq 'rules', 's///r with constant' );
23 #[perl #127635] failed with -DPERL_NO_COW perl build (George smoker uses flag)
24 #Modification of a read-only value attempted at ../t/re/subst.t line 23.
25 $a = constcow =~ s/Config/David/r;
26 ok( $a eq 'David::', 's///r with COW constant' );
28 $a = "david" =~ s/david/"is"."great"/er;
29 ok( $a eq 'isgreat', 's///er' );
31 $a = "daviddavid" =~ s/david/cool/gr;
32 ok( $a eq 'coolcool', 's///gr' );
35 $b = $a =~ s/david/sucks/r =~ s/sucks/rules/r;
36 ok( $a eq 'david' && $b eq 'rules', 'chained s///r' );
39 $b = $a =~ s/xxx/sucks/r;
40 ok( $a eq 'david' && $b eq 'david', 'non matching s///r' );
44 ok( 'david' =~ s/$a/rules/ro eq 'rules', 's///ro '.$_ );
48 eval '$b = $a !~ s/david/is great/r';
49 like( $@, qr{Using !~ with s///r doesn't make sense}, 's///r !~ operator gives error' );
52 no warnings 'uninitialized';
54 $b = $a =~ s/left/right/r;
55 ok ( !defined $a && !defined $b, 's///r with undef input' );
58 warning_like(sub { $b = $a =~ s/left/right/r },
59 qr/^Use of uninitialized value/,
60 's///r Uninitialized warning');
63 warning_like(sub {eval 's/david/sucks/r; 1'},
64 qr/^Useless use of non-destructive substitution/,
65 's///r void context warning');
69 $b = $a =~ s/david/rules/r;
70 ok( $a eq '' && $b eq '', 's///r on empty string' );
74 ok( $_ eq 'david' && $b[0] eq 'rules', 's///r in list context' );
76 # Magic value and s///r
78 tie $m, 'Tie::StdScalar'; # makes $a magical
80 $b = $m =~ s/david/rules/r;
81 ok( $m eq 'david' && $b eq 'rules', 's///r with magic input' );
83 $m = $b =~ s/rules/david/r;
84 ok( defined tied($m), 's///r magic isn\'t lost' );
86 $b = $m =~ s/xxx/yyy/r;
87 ok( ! defined tied($b), 's///r magic isn\'t contagious' );
89 my $ref = \("aaa" =~ s/aaa/bbb/r);
90 refcount_is $ref, 1, 's///r does not leak';
91 $ref = \("aaa" =~ s/aaa/bbb/rg);
92 refcount_is $ref, 1, 's///rg does not leak';
97 ok( $_ eq '$x', ":$_: eq :\$x:" );
101 ok( $_ eq 'foo', ":$_: eq :foo:" );
105 ok( $_ eq '$x foo', ":$_: eq :\$x foo:" );
108 ($a = 'abcdef') =~ s<(b${b}e)>'\n$1';
109 ok( $1 eq 'bcde' && $a eq 'a\n$1f', ":$1: eq :bcde: ; :$a: eq :a\\n\$1f:" );
112 ok( ($a =~ s/a/x/g) == 4 && $a eq 'xbxcxdx' );
114 ok( ($a =~ s/a/y/g) == 0 && $a eq 'xbxcxdx' );
116 ok( ($a =~ s/b/y/g) == 1 && $a eq 'xyxcxdx' );
119 ok( /a/i && s///gi && $_ eq 'BCD' );
122 ok( length($_) == 4 );
124 ok( $_ eq '\\' x 8 && $snum == 4 );
127 ok( length($_) == 8 );
129 ok( $_ eq '\\//' x 4 && $snum == 4 );
130 ok( length($_) == 12 );
134 ok( $_ eq 'aaXXXXbbb' );
138 ok( $_ eq 'aaXXXXbbb' );
142 ok( $_ eq 'baaXXXXbbb' );
146 ok( $_ eq 'baaXXXXbbb' );
150 ok( $_ eq 'aXXXXbbb' );
154 ok( $_ eq 'baXXXXbbb' );
158 ok( $_ eq 'aaaXXXXbb' );
162 ok( $_ eq 'aaaXXXXbb' );
166 ok( $_ eq 'aaaXXXXb' );
170 ok( $_ eq 'aayXXXbbb' );
174 ok( $_ eq 'aaaXXXzbb' );
182 ok( $_ eq 'aaaXXXXxb' );
184 # now for some unoptimized versions of the same.
188 ok( $_ eq 'aaXXXXbbb' );
192 ok( $_ eq 'aaXXXXbbb' );
196 ok( $_ eq 'baaXXXXbbb' );
200 ok( $_ eq 'baaXXXXbbb' );
204 ok( $_ eq 'aXXXXbbb' );
208 ok( $_ eq 'baXXXXbbb' );
212 ok( $_ eq 'aaaXXXXbb' );
216 ok( $_ eq 'aaaXXXXbb' );
220 ok( $_ eq 'aaaXXXXb' );
224 ok( $_ eq 'aayXXXbbb' );
228 ok( $_ eq 'aaaXXXzbb' );
231 $x ne $x || s/aaX.*Xbb//;
236 ok( $_ eq 'aaaXXXXxb' );
239 s/(\d+)/$1*2/e; # yields 'abc246xyz'
240 ok( $_ eq 'abc246xyz' );
241 s/(\d+)/sprintf("%5d",$1)/e; # yields 'abc 246xyz'
242 ok( $_ eq 'abc 246xyz' );
243 s/(\w)/$1 x 2/eg; # yields 'aabbcc 224466xxyyzz'
244 ok( $_ eq 'aabbcc 224466xxyyzz' );
255 $_ = "Now is the %#*! time for all good men...";
256 ok( ($x=(y/a-zA-Z //cd)) == 7 );
259 $_ = 'abcdefghijklmnopqrstuvwxyz0123456789';
262 ok( $_ eq 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' );
264 # same as tr/A-Z/a-z/;
265 if (defined $Config{ebcdic} && $Config{ebcdic} eq 'define') { # EBCDIC.
267 y[\301-\351][\201-\251];
268 } else { # Ye Olde ASCII. Or something like it.
269 y[\101-\132][\141-\172];
272 ok( $_ eq 'abcdefghijklmnopqrstuvwxyz0123456789' );
275 skip("ASCII-centric test",1) unless (ord("+") == ord(",") - 1
276 && ord(",") == ord("-") - 1
277 && ord("a") == ord("b") - 1
278 && ord("b") == ord("c") - 1);
293 # test recursive substitutions
294 # code based on the recursive expansion of makefile variables
297 AAAAA => '$(B)', B=>'$(C)', C => 'D', # long->short
298 E => '$(F)', F=>'p $(G) q', G => 'HHHHH', # short->long
299 DIR => '$(UNDEFINEDNAME)/xxx',
302 my($var,$level) = @_;
303 return "\$($var)" unless exists $MK{$var};
304 return exp_vars($MK{$var}, $level+1); # can recurse
307 my($str,$level) = @_;
308 $str =~ s/\$\((\w+)\)/var($1, $level+1)/ge; # can recurse
309 #warn "exp_vars $level = '$str'\n";
313 ok( exp_vars('$(AAAAA)',0) eq 'D' );
314 ok( exp_vars('$(E)',0) eq 'p HHHHH q' );
315 ok( exp_vars('$(DIR)',0) eq '$(UNDEFINEDNAME)/xxx' );
316 ok( exp_vars('foo $(DIR)/yyy bar',0) eq 'foo $(UNDEFINEDNAME)/xxx/yyy bar' );
319 s/(..)/$x = $1, m#.#/eg;
320 ok( $x eq "cd", 'a match nested in the RHS of a substitution' );
322 # Subst and lookbehind
325 $snum = s/(?<!x)c/x/g;
326 ok( $_ eq "xxxxx" && $snum == 5 );
329 $snum = s/(?<!x)(c)/x/g;
330 ok( $_ eq "xxxxx" && $snum == 5 );
333 $snum = s/(?<!r)foobbar/foobar/g;
334 ok( $_ eq "foobarfoobbar" && $snum == 1 );
337 $snum = s/(?<!ar)(foobbar)/foobar/g;
338 ok( $_ eq "foobarfoobbar" && $snum == 1 );
341 $snum = s/(?<!ar)foobbar/foobar/g;
342 ok( $_ eq "foobarfoobbar" && $snum == 1 );
344 eval 's{foo} # this is a comment, not a delimiter
346 ok( ! @?, 'parsing of split subst with comment' );
348 $snum = eval '$_="exactly"; s sxsys;m 3(yactl)3;$1';
349 is( $snum, 'yactl', 'alpha delimiters are allowed' );
353 ok( $_ eq "bbcbb" && $snum == 4,
354 'check if squashing works at the end of string' );
360 $url = new URI::URL "http://www/"; die if $url eq "xXx";
364 $foo = ' $@%#lowercase $@%# lowercase UPPERCASE$@%#UPPERCASE' .
365 ' $@%#lowercase$@%#lowercase$@%# lowercase lowercase $@%#lowercase' .
366 ' lowercase $@%#MiXeD$@%# ';
369 s{ \d+ \b [,.;]? (?{ 'digits' })
371 [a-z]+ \b [,.;]? (?{ 'lowercase' })
373 [A-Z]+ \b [,.;]? (?{ 'UPPERCASE' })
375 [A-Z] [a-z]+ \b [,.;]? (?{ 'Capitalized' })
377 [A-Za-z]+ \b [,.;]? (?{ 'MiXeD' })
379 [A-Za-z0-9]+ \b [,.;]? (?{ 'alphanumeric' })
383 [^A-Za-z0-9\s]+ (?{ '$@%#' })
390 ok( $_ eq '' && $snum == 6 );
393 $snum = s/(\d*|x)/<$1>/g;
394 $foo = '<>' . ('<x><>' x 20) ;
395 ok( $_ eq $foo && $snum == 41 );
402 ok( $_ eq 'aaaaaaxxxxxx' && $snum == 3 );
407 ok( $_ eq 'aaaaaaxxx' && $snum == 3 );
412 ok( $_ eq 'aaaaaaxxaa' );
417 ok( $_ eq 'aaaaaaxaa' );
421 ok( $_ eq 'xxxxxxxxxxxxxxxxxx' && $snum == 9 );
425 ok( $_ eq 'xxxxxxxxx' && $snum == 9 );
429 ok( $_ eq 'xxaaaaaaaa' );
433 ok( $_ eq 'xaaaaaaaa' );
437 ok( $_ eq '.aaa' && $snum == 1 );
439 eval q% s/a/"b"}/e %;
440 ok( $@ =~ /Bad evalled substitution/ );
441 eval q% ($_ = "x") =~ s/(.)/"$1 "/e %;
442 ok( $_ eq "x " and !length $@ );
444 eval q% ($_ = "x") =~ s/x(($x)*)/"$1"/e %;
445 ok( $_ eq '' and !length $@ );
448 ok( !s/^([a-z]:)/\u$1/ );
450 $_ = "Charles Bronson";
452 ok( $_ eq "C B" && $snum == 12 );
456 my $s = "H\303\266he";
460 is($l, $r, "use utf8 \\w");
463 my $pv1 = my $pv2 = "Andreas J. K\303\266nig";
464 $pv1 =~ s/A/\x{100}/;
465 substr($pv2,0,1) = "\x{100}";
470 # Gregor Chrupala <gregor.chrupala@star-group.net>
472 $a = 'España';
473 $a =~ s/ñ/ñ/;
474 like($a, qr/ñ/, "use utf8 RHS");
479 $a = 'España España';
480 $a =~ s/ñ/ñ/;
481 like($a, qr/ñ/, "use utf8 LHS");
488 like($a, qr/ñ/, "use utf8 LHS and RHS");
493 # SADAHIRO Tomoyuki <bqw10602@nifty.com>
495 $a = "\x{100}\x{101}";
496 $a =~ s/\x{101}/\xFF/;
498 is(length($a), 2, "SADAHIRO utf8 s///");
500 $a = "\x{100}\x{101}";
501 $a =~ s/\x{101}/"\xFF"/e;
505 $a = "\x{100}\x{101}";
506 $a =~ s/\x{101}/\xFF\xFF\xFF/;
507 like($a, qr/\xFF\xFF\xFF/);
510 $a = "\x{100}\x{101}";
511 $a =~ s/\x{101}/"\xFF\xFF\xFF"/e;
512 like($a, qr/\xFF\xFF\xFF/);
516 $a =~ s/\xFF/\x{100}/;
517 like($a, qr/\x{100}/);
521 $a =~ s/\xFF/"\x{100}"/e;
522 like($a, qr/\x{100}/);
526 $a =~ s/\xFF/\x{100}/;
527 like($a, qr/\x{100}/);
531 $a =~ s/\xFF/"\x{100}"/e;
532 like($a, qr/\x{100}/);
537 # subst with mixed utf8/non-utf8 type
538 my($ua, $ub, $uc, $ud) = ("\x{101}", "\x{102}", "\x{103}", "\x{104}");
539 my($na, $nb) = ("\x{ff}", "\x{fe}");
542 ($b = $a) =~ s/--/$na/;
543 is($b, "$ua$na$ub", "s///: replace non-utf8 into utf8");
544 ($b = $a) =~ s/--/--$na--/;
545 is($b, "$ua--$na--$ub", "s///: replace long non-utf8 into utf8");
546 ($b = $a) =~ s/--/$uc/;
547 is($b, "$ua$uc$ub", "s///: replace utf8 into utf8");
548 ($b = $a) =~ s/--/--$uc--/;
549 is($b, "$ua--$uc--$ub", "s///: replace long utf8 into utf8");
551 ($b = $a) =~ s/--/$ua/;
552 is($b, "$na$ua$nb", "s///: replace utf8 into non-utf8");
553 ($b = $a) =~ s/--/--$ua--/;
554 is($b, "$na--$ua--$nb", "s///: replace long utf8 into non-utf8");
556 # now with utf8 pattern
558 ($b = $a) =~ s/-($ud)?-/$na/;
559 is($b, "$ua$na$ub", "s///: replace non-utf8 into utf8 (utf8 pattern)");
560 ($b = $a) =~ s/-($ud)?-/--$na--/;
561 is($b, "$ua--$na--$ub", "s///: replace long non-utf8 into utf8 (utf8 pattern)");
562 ($b = $a) =~ s/-($ud)?-/$uc/;
563 is($b, "$ua$uc$ub", "s///: replace utf8 into utf8 (utf8 pattern)");
564 ($b = $a) =~ s/-($ud)?-/--$uc--/;
565 is($b, "$ua--$uc--$ub", "s///: replace long utf8 into utf8 (utf8 pattern)");
567 ($b = $a) =~ s/-($ud)?-/$ua/;
568 is($b, "$na$ua$nb", "s///: replace utf8 into non-utf8 (utf8 pattern)");
569 ($b = $a) =~ s/-($ud)?-/--$ua--/;
570 is($b, "$na--$ua--$nb", "s///: replace long utf8 into non-utf8 (utf8 pattern)");
571 ($b = $a) =~ s/-($ud)?-/$na/;
572 is($b, "$na$na$nb", "s///: replace non-utf8 into non-utf8 (utf8 pattern)");
573 ($b = $a) =~ s/-($ud)?-/--$na--/;
574 is($b, "$na--$na--$nb", "s///: replace long non-utf8 into non-utf8 (utf8 pattern)");
580 is("<$_> <$s>", "<xxxx> <4>", "[perl #7806]");
584 is("<$_> <$s>", "<> <4>", "[perl #7806]");
586 # [perl #19048] Coredump in silly replacement
591 is($_, "\n", "[perl #19048]");
594 # [perl #17757] interaction between saw_ampersand and study
596 my $f = eval q{ $& };
600 is($f, "yy", "[perl #17757]");
603 # [perl #20684] returned a zero count
605 is(s/(??{1})/2/eg, 4, '#20684 s/// with (??{..}) inside');
607 # [perl #20682] @- not visible in replacement
609 /(2)/; # seed @- with something else
610 s/(1)(2)(3)/$#- (@-)/;
611 is($_, "3 (0 0 1 2)", '#20682 @- not visible in replacement');
613 # [perl #20682] $^N not visible in replacement
615 /(a)/; s/(b)|(c)/-$^N/g;
616 is($_,'a-b-c','#20682 $^N not visible in replacement');
618 # [perl #22351] perl bug with 'e' substitution modifier
621 no warnings 'uninitialized';
624 is($name, "cis", q[#22351 bug with 'e' substitution modifier]);
627 # [perl #34171] $1 didn't honour 'use bytes' in s//e
635 is(length($x), 2, '[perl #34171]');
639 { # [perl #27940] perlbug: [\x00-\x1f] works, [\c@-\c_] does not
642 ($c = "\x20\c@\x30\cA\x40\cZ\x50\c_\x60") =~ s/[\c@-\c_]//g;
643 is($c, "\x20\x30\x40\x50\x60", "s/[\\c\@-\\c_]//g");
645 ($c = "\x20\x00\x30\x01\x40\x1A\x50\x1F\x60") =~ s/[\x00-\x1f]//g;
646 is($c, "\x20\x30\x40\x50\x60", "s/[\\x00-\\x1f]//g");
650 no warnings 'uninitialized';
651 /(((((((((x)))))))))(z)/; # clear $10
652 s/(((((((((x)))))))))(y)/${10}/;
653 is($_,"y","RT#6006: \$_ eq '$_'");
655 s/(((((((((x)))))))))(r)/fooba${10}/;
656 is($_,"foobar","RT#6006: \$_ eq '$_'");
659 my $want=("\n" x 11).("B\n" x 11)."B";
666 is($want,$_,"RT#17542");
670 my @tests = ('ABC', "\xA3\xA4\xA5", "\x{410}\x{411}\x{412}");
674 is($_, "012", "RT#52104: $id");
678 fresh_perl_is( '$_=q(foo);s/(.)\G//g;print' => 'foo', {},
679 '[perl #69056] positive GPOS regex segfault' );
680 fresh_perl_is( '$_="abcdef"; s/bc|(.)\G(.)/$1 ? "[$1-$2]" : "XX"/ge; print' => 'aXXdef', {},
681 'positive GPOS regex substitution failure (#69056, #114884)' );
682 fresh_perl_is( '$_="abcdefg123456"; s/(?<=...\G)?(\d)/($1)/; print' => 'abcdefg(1)23456', {},
683 'positive GPOS lookbehind regex substitution failure #114884' );
685 # s/..\G//g should stop after the first iteration, rather than working its
686 # way backwards, or looping infinitely, or SEGVing (for example)
690 # use a function to disable constant folding
691 my $f = sub { substr("789", 0, $_[0]) };
695 $count = $s =~ s/\d\d\G/7/g;
696 is($count, 1, "..\\G count (short)");
697 is($s, "12756", "..\\G s (short)");
701 $count = $s =~ s/\d\d\G/78/g;
702 is($count, 1, "..\\G count (equal)");
703 is($s, "127856", "..\\G s (equal)");
707 $count = $s =~ s/\d\d\G/789/g;
708 is($count, 1, "..\\G count (long)");
709 is($s, "1278956", "..\\G s (long)");
714 $count = $s =~ s/\d\d\G/$f->(1)/eg;
715 is($count, 1, "..\\G count (short code)");
716 is($s, "12756", "..\\G s (short code)");
720 $count = $s =~ s/\d\d\G/$f->(2)/eg;
721 is($count, 1, "..\\G count (equal code)");
722 is($s, "127856", "..\\G s (equal code)");
726 $count = $s =~ s/\d\d\G/$f->(3)/eg;
727 is($count, 1, "..\\G count (long code)");
728 is($s, "1278956", "..\\G s (long code)");
732 $count = $s =~ s/\d\d(?=\d\G)/7/g;
733 is($count, 1, "..\\G count (lookahead short)");
734 is($s, "17456", "..\\G s (lookahead short)");
738 $count = $s =~ s/\d\d(?=\d\G)/78/g;
739 is($count, 1, "..\\G count (lookahead equal)");
740 is($s, "178456", "..\\G s (lookahead equal)");
744 $count = $s =~ s/\d\d(?=\d\G)/789/g;
745 is($count, 1, "..\\G count (lookahead long)");
746 is($s, "1789456", "..\\G s (lookahead long)");
751 $count = $s =~ s/\d\d(?=\d\G)/$f->(1)/eg;
752 is($count, 1, "..\\G count (lookahead short code)");
753 is($s, "17456", "..\\G s (lookahead short code)");
757 $count = $s =~ s/\d\d(?=\d\G)/$f->(2)/eg;
758 is($count, 1, "..\\G count (lookahead equal code)");
759 is($s, "178456", "..\\G s (lookahead equal code)");
763 $count = $s =~ s/\d\d(?=\d\G)/$f->(3)/eg;
764 is($count, 1, "..\\G count (lookahead long code)");
765 is($s, "1789456", "..\\G s (lookahead long code)");
769 # [perl #71470] $var =~ s/$qr//e calling get-magic on $_ as well as $var
773 sub qrBug::TIESCALAR { bless[pop], 'qrBug' }
774 sub qrBug::FETCH { $scratch .= "[fetching $_[0][0]]"; 'prew' }
776 tie my $kror, qrBug => '$kror';
777 tie $_, qrBug => '$_';
781 $scratch, '[fetching $kror]',
782 'bug: $var =~ s/$qr//e calling get-magic on $_ as well as $var',
786 { # Bug #41530; replacing non-utf8 with a utf8 causes problems
787 my $string = "a\x{a0}a";
788 my $sub_string = $string;
789 ok(! utf8::is_utf8($sub_string), "Verify that string isn't initially utf8");
790 $sub_string =~ s/a/\x{100}/g;
791 ok(utf8::is_utf8($sub_string),
792 'Verify replace of non-utf8 with utf8 upgrades to utf8');
793 is($sub_string, "\x{100}\x{A0}\x{100}",
794 'Verify #41530 fixed: replace of non-utf8 with utf8');
796 my $non_sub_string = $string;
797 ok(! utf8::is_utf8($non_sub_string),
798 "Verify that string isn't initially utf8");
799 $non_sub_string =~ s/b/\x{100}/g;
800 ok(! utf8::is_utf8($non_sub_string),
801 "Verify that failed substitute doesn't change string's utf8ness");
802 is($non_sub_string, $string,
803 "Verify that failed substitute doesn't change string");
806 { # Verify largish octal in replacement pattern
809 $string =~ s/a/\400/;
810 is($string, chr 0x100, "Verify that handles s/foo/\\400/");
811 $string =~ s/./\600/;
812 is($string, chr 0x180, "Verify that handles s/foo/\\600/");
813 $string =~ s/./\777/;
814 is($string, chr 0x1FF, "Verify that handles s/foo/\\777/");
817 # Scoping of s//the RHS/ when there is no /e
818 # Tests based on [perl #19078]
821 my $output = ''; my %a;
822 no warnings 'uninitialized';
825 s!.!<@a{$output .= ("$&"),/[$&]/g}>!g;
828 $output, "CCCGGG< >< >< >< >< >< >",
829 's/// sets PL_curpm for each iteration even when the RHS has set it'
835 'Match vars reflect the last match after s/pat/$a{m|pat|}/ without /e'
840 # a tied scalar that returned a plain string, got messed up
841 # when substituted with a UTF8 replacement string, due to
842 # magic getting called multiple times, and pointers now pointing
843 # to stale/freed strings
844 # The original fix for this caused infinite loops for non- or cow-
845 # strings, so we test those, too.
848 sub TIESCALAR { bless [ "abcdefgh" ] }
849 sub FETCH { $fc++; $_[0][0] }
850 sub STORE { $_[0][0] = $_[1] }
854 $s =~ s/..../\x{101}/;
855 ::is($fc, 1, "tied UTF8 stuff FETCH count");
856 ::is("$s", "\x{101}efgh", "tied UTF8 stuff");
861 $s =~ s/..../\x{101}/;
862 ::is($fc, 1, '$tied_glob =~ s/non-utf8/utf8/ fetch count');
863 ::is("$s", "\x{101}::foo", '$tied_glob =~ s/non-utf8/utf8/ result');
866 $s =~ s/(....)/\x{101}/g;
867 ::is($fc, 1, '$tied_glob =~ s/(non-utf8)/utf8/g fetch count');
868 ::is("$s", "\x{101}\x{101}o",
869 '$tied_glob =~ s/(non-utf8)/utf8/g result');
871 $s = "\xff\xff\xff\xff\xff";
872 $s =~ s/..../\x{101}/;
873 ::is($fc, 1, '$tied_latin1 =~ s/non-utf8/utf8/ fetch count');
874 ::is("$s", "\x{101}\xff", '$tied_latin1 =~ s/non-utf8/utf8/ result');
876 { package package_name; tied($s)->[0] = __PACKAGE__ };
877 $s =~ s/..../\x{101}/;
878 ::is($fc, 1, '$tied_cow =~ s/non-utf8/utf8/ fetch count');
879 ::is("$s", "\x{101}age_name", '$tied_cow =~ s/non-utf8/utf8/ result');
882 $s =~ s/..../\x{101}/;
883 ::is($fc, 1, '$tied_ref =~ s/non-utf8/utf8/ fetch count');
884 ::like("$s", qr/^\x{101}AR\(0x.*\)\z/,
885 '$tied_ref =~ s/non-utf8/utf8/ result');
896 my $z_zapp = bless [], 'bam';
899 is($count, 1, '1 object');
900 is($z_zapp =~ s/.*/R/r, 'R', 'substitution happens');
901 is(ref $z_zapp, 'bam', 'still 1 object');
902 is($count, 1, 'still 1 object');
904 is($count, 0, 'now 0 objects');
906 $z_zapp = bless [], 'bam';
909 is($count, 1, '1 object');
910 like($z_zapp =~ s/./R/rg, qr/\AR{8,}\z/, 'substitution happens');
911 is(ref $z_zapp, 'bam', 'still 1 object');
912 is($count, 1, 'still 1 object');
914 is($count, 0, 'now 0 objects');
917 is(*bam =~ s/\*//r, 'main::bam', 'Can s///r a tyepglob');
918 is(*bam =~ s/\*//rg, 'main::bam', 'Can s///rg a tyepglob');
921 sub cowBug::TIESCALAR { bless[], 'cowBug' }
922 sub cowBug::FETCH { __PACKAGE__ }
924 tie my $kror, cowBug =>;
925 $kror =~ s/(?:)/""/e;
927 pass("s/// on tied var returning a cow");
929 # a test for 6502e08109cd003b2cdf39bc94ef35e52203240b
930 # previously this would segfault
934 eval { $s =~ s/(.)/die/e; };
935 like($@, qr/Died at/, "s//die/e");
939 # Test problems with constant replacement optimisation
940 # [perl #26986] logop in repl resulting in incorrect optimisation
942 @l{'a'..'z'} = 'A'..':';
944 { s/(.)/$l{my $a||$1}/g }
946 'logop in s/// repl does not result in "constant" repl optimisation';
947 # Aliases to match vars
954 is $_, 'helo', 's/pat/$alias_to_match_var/';
961 is $_, 'halo', 's/pat/foo$alias_to_match_var/';
962 # Last-used pattern containing re-evals that modify "constant" rhs
966 $x =~ /(?{*a = \"a"})./;
970 'last-used pattern disables constant repl optimisation';
978 is $_, "\xc4\x80", "empty utf8 repl does not result in mangled utf8";
980 $@ = "\x{30cb}eval 18";
981 $@ =~ s/eval \d+/eval 11/;
982 is $@, "\x{30cb}eval 11",
983 'loading utf8 tables does not interfere with matches against $@';
986 $reftobe =~ s/3/$reftobe=\ 3;4/e;
987 is $reftobe, '4', 'clobbering target with ref in s//.../e';
990 skip "no Hash::Util under miniperl", 2 if is_miniperl;
995 &Hash::Util::lock_hash(\%locker);4
998 is $locker{key}, '3', 'locking target in $hash{key} =~ s//.../e';
999 like $@, qr/^Modification of a read-only value/, 'err msg' . ($@ ? ": $@" : "");
1001 delete $::{does_not_exist}; # just in case
1002 eval { no warnings; $::{does_not_exist}=~s/(?:)/*{"does_not_exist"}; 4/e };
1003 like $@, qr/^Modification of a read-only value/,
1004 'vivifying stash elem in $that::{elem} =~ s//.../e';
1006 # COWs should not be exempt from read-only checks. s/// croaks on read-
1007 # only values even when the pattern does not match, but it was not doing so
1009 eval { for (__PACKAGE__) { s/b/c/; } };
1010 like $@, qr/^Modification of a read-only value/,
1011 'read-only COW =~ s/does not match// should croak';
1014 my $a_acute = chr utf8::unicode_to_native(0xE1); # LATIN SMALL LETTER A WITH ACUTE
1015 my $egrave = chr utf8::unicode_to_native(0xE8); # LATIN SMALL LETTER E WITH GRAVE
1016 my $u_umlaut = chr utf8::unicode_to_native(0xFC); # LATIN SMALL LETTER U WITH DIAERESIS
1017 my $division = chr utf8::unicode_to_native(0xF7); # DIVISION SIGN
1019 is("ab.c" =~ s/\b/!/agr, "!ab!.!c!", '\\b matches ASCII before string, mid, and end, /a');
1020 is("$a_acute$egrave.$u_umlaut" =~ s/\b/!/agr, "$a_acute$egrave.$u_umlaut", '\\b matches Latin1 before string, mid, and end, /a');
1021 is("\x{100}\x{101}.\x{102}" =~ s/\b/!/agr, "\x{100}\x{101}.\x{102}", '\\b matches above-Latin1 before string, mid, and end, /a');
1023 is("..." =~ s/\B/!/agr, "!.!.!.!", '\\B matches ASCII before string, mid, and end, /a');
1024 is("$division$division$division" =~ s/\B/!/agr, "!$division!$division!$division!", '\\B matches Latin1 before string, mid, and end, /a');
1025 is("\x{2028}\x{2028}\x{2028}" =~ s/\B/!/agr, "!\x{2028}!\x{2028}!\x{2028}!", '\\B matches above-Latin1 before string, mid, and end, /a');
1027 is("ab.c" =~ s/\b/!/dgr, "!ab!.!c!", '\\b matches ASCII before string, mid, and end, /d');
1028 { is("$a_acute$egrave.$u_umlaut" =~ s/\b/!/dgr, "$a_acute$egrave.$u_umlaut", '\\b matches Latin1 before string, mid, and end, /d'); }
1029 is("\x{100}\x{101}.\x{102}" =~ s/\b/!/dgr, "!\x{100}\x{101}!.!\x{102}!", '\\b matches above-Latin1 before string, mid, and end, /d');
1031 is("..." =~ s/\B/!/dgr, "!.!.!.!", '\\B matches ASCII before string, mid, and end, /d');
1032 is("$division$division$division" =~ s/\B/!/dgr, "!$division!$division!$division!", '\\B matches Latin1 before string, mid, and end, /d');
1033 is("\x{2028}\x{2028}\x{2028}" =~ s/\B/!/dgr, "!\x{2028}!\x{2028}!\x{2028}!", '\\B matches above-Latin1 before string, mid, and end, /d');
1035 is("ab.c" =~ s/\b/!/ugr, "!ab!.!c!", '\\b matches ASCII before string, mid, and end, /u');
1036 is("$a_acute$egrave.$u_umlaut" =~ s/\b/!/ugr, "!$a_acute$egrave!.!$u_umlaut!", '\\b matches Latin1 before string, mid, and end, /u');
1037 is("\x{100}\x{101}.\x{102}" =~ s/\b/!/ugr, "!\x{100}\x{101}!.!\x{102}!", '\\b matches above-Latin1 before string, mid, and end, /u');
1039 is("..." =~ s/\B/!/ugr, "!.!.!.!", '\\B matches ASCII before string, mid, and end, /u');
1040 is("$division$division$division" =~ s/\B/!/ugr, "!$division!$division!$division!", '\\B matches Latin1 before string, mid, and end, /u');
1041 is("\x{2028}\x{2028}\x{2028}" =~ s/\B/!/ugr, "!\x{2028}!\x{2028}!\x{2028}!", '\\B matches above-Latin1 before string, mid, and end, /u');
1043 fresh_perl_like( '$_=""; /\b{gcb}/; s///g', qr/^$/, {},
1044 '[perl #126319: Segmentation fault in Perl_sv_catpvn_flags with \b{gcb}'
1046 fresh_perl_like( '$_=""; /\B{gcb}/; s///g', qr/^$/, {},
1047 '[perl #126319: Segmentation fault in Perl_sv_catpvn_flags with \b{gcb}'
1049 fresh_perl_like( '$_=""; /\b{wb}/; s///g', qr/^$/, {},
1050 '[perl #126319: Segmentation fault in Perl_sv_catpvn_flags with \b{wb}'
1052 fresh_perl_like( '$_=""; /\B{wb}/; s///g', qr/^$/, {},
1053 '[perl #126319: Segmentation fault in Perl_sv_catpvn_flags with \b{wb}'
1055 fresh_perl_like( '$_=""; /\b{sb}/; s///g', qr/^$/, {},
1056 '[perl #126319: Segmentation fault in Perl_sv_catpvn_flags with \b{sb}'
1058 fresh_perl_like( '$_=""; /\B{sb}/; s///g', qr/^$/, {},
1059 '[perl #126319: Segmentation fault in Perl_sv_catpvn_flags with \b{sb}'
1063 if (! locales_enabled('LC_ALL')) {
1064 skip "Can't test locale (maybe you are missing POSIX)", 6;
1067 setlocale(&POSIX::LC_ALL, "C");
1069 is("a.b" =~ s/\b/!/gr, "!a!.!b!", '\\b matches ASCII before string, mid, and end, /l');
1070 is("$a_acute.$egrave" =~ s/\b/!/gr, "$a_acute.$egrave", '\\b matches Latin1 before string, mid, and end, /l');
1071 is("\x{100}\x{101}.\x{102}" =~ s/\b/!/gr, "!\x{100}\x{101}!.!\x{102}!", '\\b matches above-Latin1 before string, mid, and end, /l');
1073 is("..." =~ s/\B/!/gr, "!.!.!.!", '\\B matches ASCII before string, mid, and end, /l');
1074 is("$division$division$division" =~ s/\B/!/gr, "!$division!$division!$division!", '\\B matches Latin1 before string, mid, and end, /l');
1075 is("\x{2028}\x{2028}\x{2028}" =~ s/\B/!/gr, "!\x{2028}!\x{2028}!\x{2028}!", '\\B matches above-Latin1 before string, mid, and end, /l');
1081 # RT #123954 if the string getting matched against got converted during
1082 # s///e so that it was no longer SvPOK, an assertion would fail when
1085 $s1 =~ s/.?/$s1++/ge;
1086 is($s1, "01","RT #123954 s1");
1089 # RT #126602 double free if the value being modified is freed in the replacement
1090 fresh_perl_is('s//*_=0;s|0||;00.y0/e; print qq(ok\n)', "ok\n", { stderr => 1 },
1091 "[perl #126602] s//*_=0;s|0||/e crashes");
1095 #RT 126260 gofs is in chars, not bytes
1097 # in something like /..\G/, the engine should start matching two
1098 # chars before pos(). At one point it was matching two bytes before.
1100 my $s = "\x{121}\x{122}\x{123}";
1103 is($s, "\x{123}", "#RT 126260 gofs");
1107 if (! locales_enabled('LC_CTYPE')) {
1108 skip "Can't test locale", 1;
1111 # To cause breakeage, we need a locale in which \xff matches whatever
1112 # POSIX class is used in the pattern. Easiest is C, with \W.
1113 fresh_perl_is(' use POSIX qw(locale_h);
1114 setlocale(&POSIX::LC_CTYPE, "C");
1120 '[perl #129038 ] s/\xff//l no longer crashes');
1124 skip("no Tie::Hash::NamedCapture under miniperl", 3) if is_miniperl;
1126 # RT #23624 scoping of @+/@- when used with tie()
1129 package Tie::Prematch;
1130 sub TIEHASH { bless \my $dummy => __PACKAGE__ }
1131 sub FETCH { return substr $_[1], 0, $-[0] }
1136 tie my %pre, 'Tie::Prematch';
1138 $foo =~ s/.ob/$pre{ $foo }/;
1139 is($foo, 'ffar', 'RT #23624');
1142 $foo =~ s/.ob/tied(%pre)->FETCH($foo)/e;
1143 is($foo, 'ffar', 'RT #23624');
1145 tie %-, 'Tie::Prematch';
1147 $foo =~ s/.ob/$-{$foo}/;
1148 is($foo, 'ffar', 'RT #23624');
1150 undef *Tie::Prematch::TIEHASH;
1151 undef *Tie::Prematch::FETCH;
1155 # [perl #130188] crash on return from substitution in subroutine
1156 # make sure returning from s///e doesn't SEGV
1160 $x =~ s/./return;/e;
1167 # a multi-line s/// wasn't resetting the cop_line correctly
1173 my $lines = __LINE__ - $l0;
1174 is $lines, 4, "RT #131930";
1177 { # [perl #133899], would panic
1179 fresh_perl_is('my $a = "ha"; $a =~ s!|0?h\x{300}(?{})!!gi', "", {},
1180 "[perl #133899] s!|0?h\\x{300}(?{})!!gi panics");
1184 fresh_perl_is("s//00000000000format \0 '0000000\\x{800}/;eval", "", {}, "RT #133882");
1190 for my $upgrade (0,1) {
1192 utf8::upgrade($copy) if $upgrade;
1193 my $r= $copy=~s/b{0}//gr;
1196 is( $ret[1], $ret[0],
1197 "Issue #20690 - s/b{0}//gr should work the same for utf8 and non-utf8 strings");
1199 "Issue #20690 - s/b{0}//gr on non-utf8 string should not remove anything");
1201 "Issue #20690 - s/b{0}//gr on utf8 string should not remove anything");