This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Simplify the fix for bug #41530
[perl5.git] / t / re / subst.t
CommitLineData
25aae3a7 1#!./perl -w
d9d8d8de 2
a1a0e61e
TD
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
a1a0e61e 6 require Config; import Config;
86e69b18 7 require './test.pl';
a1a0e61e 8}
344462d3 9
64534138 10plan( tests => 202 );
4f4d7508 11
4f4d7508
DC
12$_ = 'david';
13$a = s/david/rules/r;
14ok( $_ eq 'david' && $a eq 'rules', 'non-destructive substitute' );
15
16$a = "david" =~ s/david/rules/r;
17ok( $a eq 'rules', 's///r with constant' );
18
19$a = "david" =~ s/david/"is"."great"/er;
20ok( $a eq 'isgreat', 's///er' );
21
22$a = "daviddavid" =~ s/david/cool/gr;
23ok( $a eq 'coolcool', 's///gr' );
24
25$a = 'david';
26$b = $a =~ s/david/sucks/r =~ s/sucks/rules/r;
27ok( $a eq 'david' && $b eq 'rules', 'chained s///r' );
28
29$a = 'david';
30$b = $a =~ s/xxx/sucks/r;
31ok( $a eq 'david' && $b eq 'david', 'non matching s///r' );
32
33$a = 'david';
34for (0..2) {
35 ok( 'david' =~ s/$a/rules/ro eq 'rules', 's///ro '.$_ );
36}
37
38$a = 'david';
39eval '$b = $a !~ s/david/is great/r';
40like( $@, qr{Using !~ with s///r doesn't make sense}, 's///r !~ operator gives error' );
41
42{
43 no warnings 'uninitialized';
44 $a = undef;
45 $b = $a =~ s/left/right/r;
46 ok ( !defined $a && !defined $b, 's///r with undef input' );
47
48 use warnings;
4d18b353
NC
49 warning_like(sub { $b = $a =~ s/left/right/r },
50 qr/^Use of uninitialized value/,
51 's///r Uninitialized warning');
4f4d7508
DC
52
53 $a = 'david';
4d18b353
NC
54 warning_like(sub {eval 's/david/sucks/r; 1'},
55 qr/^Useless use of non-destructive substitution/,
56 's///r void context warning');
4f4d7508
DC
57}
58
59$a = '';
60$b = $a =~ s/david/rules/r;
61ok( $a eq '' && $b eq '', 's///r on empty string' );
62
63$_ = 'david';
64@b = s/david/rules/r;
65ok( $_ eq 'david' && $b[0] eq 'rules', 's///r in list context' );
66
67# Magic value and s///r
68require Tie::Scalar;
69tie $m, 'Tie::StdScalar'; # makes $a magical
70$m = "david";
71$b = $m =~ s/david/rules/r;
72ok( $m eq 'david' && $b eq 'rules', 's///r with magic input' );
73
74$m = $b =~ s/rules/david/r;
75ok( defined tied($m), 's///r magic isn\'t lost' );
76
77$b = $m =~ s/xxx/yyy/r;
78ok( ! defined tied($b), 's///r magic isn\'t contagious' );
d9d8d8de 79
4eedab49
FC
80my $ref = \("aaa" =~ s/aaa/bbb/r);
81is (Internals::SvREFCNT($$ref), 1, 's///r does not leak');
82$ref = \("aaa" =~ s/aaa/bbb/rg);
83is (Internals::SvREFCNT($$ref), 1, 's///rg does not leak');
84
d9d8d8de
LW
85$x = 'foo';
86$_ = "x";
87s/x/\$x/;
e8ebd21b 88ok( $_ eq '$x', ":$_: eq :\$x:" );
d9d8d8de
LW
89
90$_ = "x";
91s/x/$x/;
e8ebd21b 92ok( $_ eq 'foo', ":$_: eq :foo:" );
d9d8d8de
LW
93
94$_ = "x";
95s/x/\$x $x/;
e8ebd21b 96ok( $_ eq '$x foo', ":$_: eq :\$x foo:" );
d9d8d8de
LW
97
98$b = 'cd';
79072805 99($a = 'abcdef') =~ s<(b${b}e)>'\n$1';
e8ebd21b 100ok( $1 eq 'bcde' && $a eq 'a\n$1f', ":$1: eq :bcde: ; :$a: eq :a\\n\$1f:" );
d9d8d8de
LW
101
102$a = 'abacada';
e8ebd21b 103ok( ($a =~ s/a/x/g) == 4 && $a eq 'xbxcxdx' );
d9d8d8de 104
e8ebd21b 105ok( ($a =~ s/a/y/g) == 0 && $a eq 'xbxcxdx' );
d9d8d8de 106
e8ebd21b 107ok( ($a =~ s/b/y/g) == 1 && $a eq 'xyxcxdx' );
d9d8d8de
LW
108
109$_ = 'ABACADA';
e8ebd21b 110ok( /a/i && s///gi && $_ eq 'BCD' );
d9d8d8de
LW
111
112$_ = '\\' x 4;
e8ebd21b
RGS
113ok( length($_) == 4 );
114$snum = s/\\/\\\\/g;
115ok( $_ eq '\\' x 8 && $snum == 4 );
d9d8d8de
LW
116
117$_ = '\/' x 4;
e8ebd21b
RGS
118ok( length($_) == 8 );
119$snum = s/\//\/\//g;
120ok( $_ eq '\\//' x 4 && $snum == 4 );
121ok( length($_) == 12 );
d9d8d8de
LW
122
123$_ = 'aaaXXXXbbb';
124s/^a//;
e8ebd21b 125ok( $_ eq 'aaXXXXbbb' );
d9d8d8de
LW
126
127$_ = 'aaaXXXXbbb';
128s/a//;
e8ebd21b 129ok( $_ eq 'aaXXXXbbb' );
d9d8d8de
LW
130
131$_ = 'aaaXXXXbbb';
132s/^a/b/;
e8ebd21b 133ok( $_ eq 'baaXXXXbbb' );
d9d8d8de
LW
134
135$_ = 'aaaXXXXbbb';
136s/a/b/;
e8ebd21b 137ok( $_ eq 'baaXXXXbbb' );
d9d8d8de
LW
138
139$_ = 'aaaXXXXbbb';
140s/aa//;
e8ebd21b 141ok( $_ eq 'aXXXXbbb' );
d9d8d8de
LW
142
143$_ = 'aaaXXXXbbb';
144s/aa/b/;
e8ebd21b 145ok( $_ eq 'baXXXXbbb' );
d9d8d8de
LW
146
147$_ = 'aaaXXXXbbb';
148s/b$//;
e8ebd21b 149ok( $_ eq 'aaaXXXXbb' );
d9d8d8de
LW
150
151$_ = 'aaaXXXXbbb';
152s/b//;
e8ebd21b 153ok( $_ eq 'aaaXXXXbb' );
d9d8d8de
LW
154
155$_ = 'aaaXXXXbbb';
156s/bb//;
e8ebd21b 157ok( $_ eq 'aaaXXXXb' );
d9d8d8de
LW
158
159$_ = 'aaaXXXXbbb';
160s/aX/y/;
e8ebd21b 161ok( $_ eq 'aayXXXbbb' );
d9d8d8de
LW
162
163$_ = 'aaaXXXXbbb';
164s/Xb/z/;
e8ebd21b 165ok( $_ eq 'aaaXXXzbb' );
d9d8d8de
LW
166
167$_ = 'aaaXXXXbbb';
168s/aaX.*Xbb//;
e8ebd21b 169ok( $_ eq 'ab' );
d9d8d8de
LW
170
171$_ = 'aaaXXXXbbb';
172s/bb/x/;
e8ebd21b 173ok( $_ eq 'aaaXXXXxb' );
d9d8d8de
LW
174
175# now for some unoptimized versions of the same.
176
177$_ = 'aaaXXXXbbb';
178$x ne $x || s/^a//;
e8ebd21b 179ok( $_ eq 'aaXXXXbbb' );
d9d8d8de
LW
180
181$_ = 'aaaXXXXbbb';
182$x ne $x || s/a//;
e8ebd21b 183ok( $_ eq 'aaXXXXbbb' );
d9d8d8de
LW
184
185$_ = 'aaaXXXXbbb';
186$x ne $x || s/^a/b/;
e8ebd21b 187ok( $_ eq 'baaXXXXbbb' );
d9d8d8de
LW
188
189$_ = 'aaaXXXXbbb';
190$x ne $x || s/a/b/;
e8ebd21b 191ok( $_ eq 'baaXXXXbbb' );
d9d8d8de
LW
192
193$_ = 'aaaXXXXbbb';
194$x ne $x || s/aa//;
e8ebd21b 195ok( $_ eq 'aXXXXbbb' );
d9d8d8de
LW
196
197$_ = 'aaaXXXXbbb';
198$x ne $x || s/aa/b/;
e8ebd21b 199ok( $_ eq 'baXXXXbbb' );
d9d8d8de
LW
200
201$_ = 'aaaXXXXbbb';
202$x ne $x || s/b$//;
e8ebd21b 203ok( $_ eq 'aaaXXXXbb' );
d9d8d8de
LW
204
205$_ = 'aaaXXXXbbb';
206$x ne $x || s/b//;
e8ebd21b 207ok( $_ eq 'aaaXXXXbb' );
d9d8d8de
LW
208
209$_ = 'aaaXXXXbbb';
210$x ne $x || s/bb//;
e8ebd21b 211ok( $_ eq 'aaaXXXXb' );
d9d8d8de
LW
212
213$_ = 'aaaXXXXbbb';
214$x ne $x || s/aX/y/;
e8ebd21b 215ok( $_ eq 'aayXXXbbb' );
d9d8d8de
LW
216
217$_ = 'aaaXXXXbbb';
218$x ne $x || s/Xb/z/;
e8ebd21b 219ok( $_ eq 'aaaXXXzbb' );
d9d8d8de
LW
220
221$_ = 'aaaXXXXbbb';
222$x ne $x || s/aaX.*Xbb//;
e8ebd21b 223ok( $_ eq 'ab' );
d9d8d8de
LW
224
225$_ = 'aaaXXXXbbb';
226$x ne $x || s/bb/x/;
e8ebd21b 227ok( $_ eq 'aaaXXXXxb' );
d9d8d8de
LW
228
229$_ = 'abc123xyz';
c277df42 230s/(\d+)/$1*2/e; # yields 'abc246xyz'
e8ebd21b 231ok( $_ eq 'abc246xyz' );
c277df42 232s/(\d+)/sprintf("%5d",$1)/e; # yields 'abc 246xyz'
e8ebd21b 233ok( $_ eq 'abc 246xyz' );
c277df42 234s/(\w)/$1 x 2/eg; # yields 'aabbcc 224466xxyyzz'
e8ebd21b 235ok( $_ eq 'aabbcc 224466xxyyzz' );
d9d8d8de
LW
236
237$_ = "aaaaa";
e8ebd21b
RGS
238ok( y/a/b/ == 5 );
239ok( y/a/b/ == 0 );
240ok( y/b// == 5 );
241ok( y/b/c/s == 5 );
242ok( y/c// == 1 );
243ok( y/c//d == 1 );
244ok( $_ eq "" );
d9d8d8de
LW
245
246$_ = "Now is the %#*! time for all good men...";
e8ebd21b
RGS
247ok( ($x=(y/a-zA-Z //cd)) == 7 );
248ok( y/ / /s == 8 );
d9d8d8de 249
79072805
LW
250$_ = 'abcdefghijklmnopqrstuvwxyz0123456789';
251tr/a-z/A-Z/;
252
e8ebd21b 253ok( $_ eq 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' );
79072805
LW
254
255# same as tr/A-Z/a-z/;
e8ebd21b 256if (defined $Config{ebcdic} && $Config{ebcdic} eq 'define') { # EBCDIC.
6e68dac8 257 no utf8;
9d116dd7
JH
258 y[\301-\351][\201-\251];
259} else { # Ye Olde ASCII. Or something like it.
260 y[\101-\132][\141-\172];
261}
79072805 262
e8ebd21b 263ok( $_ eq 'abcdefghijklmnopqrstuvwxyz0123456789' );
79072805 264
e8ebd21b
RGS
265SKIP: {
266 skip("not ASCII",1) unless (ord("+") == ord(",") - 1
267 && ord(",") == ord("-") - 1
268 && ord("a") == ord("b") - 1
269 && ord("b") == ord("c") - 1);
270 $_ = '+,-';
271 tr/+--/a-c/;
272 ok( $_ eq 'abc' );
9d116dd7 273}
79072805
LW
274
275$_ = '+,-';
276tr/+\--/a\/c/;
e8ebd21b 277ok( $_ eq 'a,/' );
79072805
LW
278
279$_ = '+,-';
280tr/-+,/ab\-/;
e8ebd21b 281ok( $_ eq 'b-a' );
843b4603
TB
282
283
284# test recursive substitutions
285# code based on the recursive expansion of makefile variables
286
287my %MK = (
288 AAAAA => '$(B)', B=>'$(C)', C => 'D', # long->short
289 E => '$(F)', F=>'p $(G) q', G => 'HHHHH', # short->long
290 DIR => '$(UNDEFINEDNAME)/xxx',
291);
292sub var {
293 my($var,$level) = @_;
294 return "\$($var)" unless exists $MK{$var};
295 return exp_vars($MK{$var}, $level+1); # can recurse
296}
297sub exp_vars {
298 my($str,$level) = @_;
299 $str =~ s/\$\((\w+)\)/var($1, $level+1)/ge; # can recurse
300 #warn "exp_vars $level = '$str'\n";
301 $str;
302}
303
e8ebd21b
RGS
304ok( exp_vars('$(AAAAA)',0) eq 'D' );
305ok( exp_vars('$(E)',0) eq 'p HHHHH q' );
306ok( exp_vars('$(DIR)',0) eq '$(UNDEFINEDNAME)/xxx' );
307ok( exp_vars('foo $(DIR)/yyy bar',0) eq 'foo $(UNDEFINEDNAME)/xxx/yyy bar' );
3e3baf6d
TB
308
309$_ = "abcd";
c277df42 310s/(..)/$x = $1, m#.#/eg;
e8ebd21b 311ok( $x eq "cd", 'a match nested in the RHS of a substitution' );
fb73857a 312
c277df42
IZ
313# Subst and lookbehind
314
315$_="ccccc";
e8ebd21b
RGS
316$snum = s/(?<!x)c/x/g;
317ok( $_ eq "xxxxx" && $snum == 5 );
c277df42
IZ
318
319$_="ccccc";
e8ebd21b
RGS
320$snum = s/(?<!x)(c)/x/g;
321ok( $_ eq "xxxxx" && $snum == 5 );
c277df42
IZ
322
323$_="foobbarfoobbar";
e8ebd21b
RGS
324$snum = s/(?<!r)foobbar/foobar/g;
325ok( $_ eq "foobarfoobbar" && $snum == 1 );
c277df42
IZ
326
327$_="foobbarfoobbar";
e8ebd21b
RGS
328$snum = s/(?<!ar)(foobbar)/foobar/g;
329ok( $_ eq "foobarfoobbar" && $snum == 1 );
c277df42
IZ
330
331$_="foobbarfoobbar";
e8ebd21b
RGS
332$snum = s/(?<!ar)foobbar/foobar/g;
333ok( $_ eq "foobarfoobbar" && $snum == 1 );
c277df42 334
fb73857a
PP
335eval 's{foo} # this is a comment, not a delimiter
336 {bar};';
e8ebd21b 337ok( ! @?, 'parsing of split subst with comment' );
f3ea7b5e 338
ed02a3bf
DN
339$snum = eval '$_="exactly"; s sxsys;m 3(yactl)3;$1';
340is( $snum, 'yactl', 'alpha delimiters are allowed' );
341
f3ea7b5e 342$_="baacbaa";
e8ebd21b
RGS
343$snum = tr/a/b/s;
344ok( $_ eq "bbcbb" && $snum == 4,
345 'check if squashing works at the end of string' );
f3ea7b5e 346
2216f30a 347$_ = "ab";
e8ebd21b 348ok( s/a/b/ == 1 );
ce862d02
IZ
349
350$_ = <<'EOL';
351 $url = new URI::URL "http://www/"; die if $url eq "xXx";
352EOL
353$^R = 'junk';
354
355$foo = ' $@%#lowercase $@%# lowercase UPPERCASE$@%#UPPERCASE' .
356 ' $@%#lowercase$@%#lowercase$@%# lowercase lowercase $@%#lowercase' .
357 ' lowercase $@%#MiXeD$@%# ';
358
e8ebd21b 359$snum =
ce862d02
IZ
360s{ \d+ \b [,.;]? (?{ 'digits' })
361 |
362 [a-z]+ \b [,.;]? (?{ 'lowercase' })
363 |
364 [A-Z]+ \b [,.;]? (?{ 'UPPERCASE' })
365 |
366 [A-Z] [a-z]+ \b [,.;]? (?{ 'Capitalized' })
367 |
368 [A-Za-z]+ \b [,.;]? (?{ 'MiXeD' })
369 |
370 [A-Za-z0-9]+ \b [,.;]? (?{ 'alphanumeric' })
371 |
372 \s+ (?{ ' ' })
373 |
374 [^A-Za-z0-9\s]+ (?{ '$@%#' })
375}{$^R}xg;
e8ebd21b 376ok( $_ eq $foo );
8e5e9ebe
RGS
377ok( $snum == 31 );
378
379$_ = 'a' x 6;
380$snum = s/a(?{})//g;
381ok( $_ eq '' && $snum == 6 );
ce862d02 382
2beec16e 383$_ = 'x' x 20;
e8ebd21b 384$snum = s/(\d*|x)/<$1>/g;
2beec16e 385$foo = '<>' . ('<x><>' x 20) ;
e8ebd21b 386ok( $_ eq $foo && $snum == 41 );
ad94a511
IZ
387
388$t = 'aaaaaaaaa';
389
390$_ = $t;
391pos = 6;
e8ebd21b
RGS
392$snum = s/\Ga/xx/g;
393ok( $_ eq 'aaaaaaxxxxxx' && $snum == 3 );
ad94a511
IZ
394
395$_ = $t;
396pos = 6;
e8ebd21b
RGS
397$snum = s/\Ga/x/g;
398ok( $_ eq 'aaaaaaxxx' && $snum == 3 );
ad94a511
IZ
399
400$_ = $t;
401pos = 6;
402s/\Ga/xx/;
e8ebd21b 403ok( $_ eq 'aaaaaaxxaa' );
ad94a511
IZ
404
405$_ = $t;
406pos = 6;
407s/\Ga/x/;
e8ebd21b 408ok( $_ eq 'aaaaaaxaa' );
ad94a511
IZ
409
410$_ = $t;
e8ebd21b
RGS
411$snum = s/\Ga/xx/g;
412ok( $_ eq 'xxxxxxxxxxxxxxxxxx' && $snum == 9 );
ad94a511
IZ
413
414$_ = $t;
e8ebd21b
RGS
415$snum = s/\Ga/x/g;
416ok( $_ eq 'xxxxxxxxx' && $snum == 9 );
ad94a511
IZ
417
418$_ = $t;
419s/\Ga/xx/;
e8ebd21b 420ok( $_ eq 'xxaaaaaaaa' );
ad94a511
IZ
421
422$_ = $t;
423s/\Ga/x/;
e8ebd21b 424ok( $_ eq 'xaaaaaaaa' );
ad94a511 425
f5c9036e 426$_ = 'aaaa';
e8ebd21b
RGS
427$snum = s/\ba/./g;
428ok( $_ eq '.aaa' && $snum == 1 );
ad94a511 429
e9fa98b2 430eval q% s/a/"b"}/e %;
e8ebd21b 431ok( $@ =~ /Bad evalled substitution/ );
e9fa98b2 432eval q% ($_ = "x") =~ s/(.)/"$1 "/e %;
e8ebd21b 433ok( $_ eq "x " and !length $@ );
43a16006
HS
434$x = $x = 'interp';
435eval q% ($_ = "x") =~ s/x(($x)*)/"$1"/e %;
e8ebd21b 436ok( $_ eq '' and !length $@ );
e9fa98b2 437
653099ff 438$_ = "C:/";
e8ebd21b 439ok( !s/^([a-z]:)/\u$1/ );
e9fa98b2 440
12d33761 441$_ = "Charles Bronson";
e8ebd21b
RGS
442$snum = s/\B\w//g;
443ok( $_ eq "C B" && $snum == 12 );
5b71a6a7
A
444
445{
446 use utf8;
447 my $s = "H\303\266he";
448 my $l = my $r = $s;
449 $l =~ s/[^\w]//g;
450 $r =~ s/[^\w\.]//g;
aefe6dfc 451 is($l, $r, "use utf8 \\w");
5b71a6a7 452}
89afcb60
A
453
454my $pv1 = my $pv2 = "Andreas J. K\303\266nig";
455$pv1 =~ s/A/\x{100}/;
456substr($pv2,0,1) = "\x{100}";
457is($pv1, $pv2);
aefe6dfc 458
8e9639e9
JH
459SKIP: {
460 skip("EBCDIC", 3) if ord("A") == 193;
461
462 {
463 # Gregor Chrupala <gregor.chrupala@star-group.net>
464 use utf8;
465 $a = 'Espa&ntilde;a';
466 $a =~ s/&ntilde;/ñ/;
467 like($a, qr/ñ/, "use utf8 RHS");
468 }
469
470 {
471 use utf8;
472 $a = 'España España';
473 $a =~ s/ñ/&ntilde;/;
474 like($a, qr/ñ/, "use utf8 LHS");
475 }
476
477 {
478 use utf8;
479 $a = 'España';
480 $a =~ s/ñ/ñ/;
481 like($a, qr/ñ/, "use utf8 LHS and RHS");
482 }
aefe6dfc
JH
483}
484
8514a05a
JH
485{
486 # SADAHIRO Tomoyuki <bqw10602@nifty.com>
487
488 $a = "\x{100}\x{101}";
489 $a =~ s/\x{101}/\xFF/;
490 like($a, qr/\xFF/);
4a176938 491 is(length($a), 2, "SADAHIRO utf8 s///");
8514a05a
JH
492
493 $a = "\x{100}\x{101}";
494 $a =~ s/\x{101}/"\xFF"/e;
495 like($a, qr/\xFF/);
496 is(length($a), 2);
497
498 $a = "\x{100}\x{101}";
499 $a =~ s/\x{101}/\xFF\xFF\xFF/;
500 like($a, qr/\xFF\xFF\xFF/);
501 is(length($a), 4);
502
503 $a = "\x{100}\x{101}";
504 $a =~ s/\x{101}/"\xFF\xFF\xFF"/e;
505 like($a, qr/\xFF\xFF\xFF/);
506 is(length($a), 4);
507
508 $a = "\xFF\x{101}";
509 $a =~ s/\xFF/\x{100}/;
510 like($a, qr/\x{100}/);
511 is(length($a), 2);
512
513 $a = "\xFF\x{101}";
514 $a =~ s/\xFF/"\x{100}"/e;
515 like($a, qr/\x{100}/);
516 is(length($a), 2);
517
518 $a = "\xFF";
519 $a =~ s/\xFF/\x{100}/;
520 like($a, qr/\x{100}/);
521 is(length($a), 1);
522
523 $a = "\xFF";
524 $a =~ s/\xFF/"\x{100}"/e;
525 like($a, qr/\x{100}/);
526 is(length($a), 1);
527}
d6d0e86e
HS
528
529{
530 # subst with mixed utf8/non-utf8 type
531 my($ua, $ub, $uc, $ud) = ("\x{101}", "\x{102}", "\x{103}", "\x{104}");
532 my($na, $nb) = ("\x{ff}", "\x{fe}");
533 my $a = "$ua--$ub";
534 my $b;
535 ($b = $a) =~ s/--/$na/;
536 is($b, "$ua$na$ub", "s///: replace non-utf8 into utf8");
537 ($b = $a) =~ s/--/--$na--/;
538 is($b, "$ua--$na--$ub", "s///: replace long non-utf8 into utf8");
539 ($b = $a) =~ s/--/$uc/;
540 is($b, "$ua$uc$ub", "s///: replace utf8 into utf8");
541 ($b = $a) =~ s/--/--$uc--/;
542 is($b, "$ua--$uc--$ub", "s///: replace long utf8 into utf8");
543 $a = "$na--$nb";
544 ($b = $a) =~ s/--/$ua/;
545 is($b, "$na$ua$nb", "s///: replace utf8 into non-utf8");
546 ($b = $a) =~ s/--/--$ua--/;
547 is($b, "$na--$ua--$nb", "s///: replace long utf8 into non-utf8");
548
549 # now with utf8 pattern
550 $a = "$ua--$ub";
551 ($b = $a) =~ s/-($ud)?-/$na/;
552 is($b, "$ua$na$ub", "s///: replace non-utf8 into utf8 (utf8 pattern)");
553 ($b = $a) =~ s/-($ud)?-/--$na--/;
554 is($b, "$ua--$na--$ub", "s///: replace long non-utf8 into utf8 (utf8 pattern)");
555 ($b = $a) =~ s/-($ud)?-/$uc/;
556 is($b, "$ua$uc$ub", "s///: replace utf8 into utf8 (utf8 pattern)");
557 ($b = $a) =~ s/-($ud)?-/--$uc--/;
558 is($b, "$ua--$uc--$ub", "s///: replace long utf8 into utf8 (utf8 pattern)");
559 $a = "$na--$nb";
560 ($b = $a) =~ s/-($ud)?-/$ua/;
561 is($b, "$na$ua$nb", "s///: replace utf8 into non-utf8 (utf8 pattern)");
562 ($b = $a) =~ s/-($ud)?-/--$ua--/;
563 is($b, "$na--$ua--$nb", "s///: replace long utf8 into non-utf8 (utf8 pattern)");
564 ($b = $a) =~ s/-($ud)?-/$na/;
565 is($b, "$na$na$nb", "s///: replace non-utf8 into non-utf8 (utf8 pattern)");
566 ($b = $a) =~ s/-($ud)?-/--$na--/;
567 is($b, "$na--$na--$nb", "s///: replace long non-utf8 into non-utf8 (utf8 pattern)");
568}
569
6c8d78fb
HS
570$_ = 'aaaa';
571$r = 'x';
572$s = s/a(?{})/$r/g;
f14c76ed 573is("<$_> <$s>", "<xxxx> <4>", "[perl #7806]");
6c8d78fb
HS
574
575$_ = 'aaaa';
576$s = s/a(?{})//g;
f14c76ed 577is("<$_> <$s>", "<> <4>", "[perl #7806]");
6c8d78fb 578
f14c76ed
RGS
579# [perl #19048] Coredump in silly replacement
580{
581 local $^W = 0;
582 $_="abcdef\n";
583 s!.!!eg;
584 is($_, "\n", "[perl #19048]");
585}
586
4addbd3b
HS
587# [perl #17757] interaction between saw_ampersand and study
588{
589 my $f = eval q{ $& };
590 $f = "xx";
591 study $f;
592 $f =~ s/x/y/g;
593 is($f, "yy", "[perl #17757]");
594}
22e13caa
AE
595
596# [perl #20684] returned a zero count
597$_ = "1111";
598is(s/(??{1})/2/eg, 4, '#20684 s/// with (??{..}) inside');
599
83b43d92
AE
600# [perl #20682] @- not visible in replacement
601$_ = "123";
602/(2)/; # seed @- with something else
603s/(1)(2)(3)/$#- (@-)/;
604is($_, "3 (0 0 1 2)", '#20682 @- not visible in replacement');
605
76ec6486
AE
606# [perl #20682] $^N not visible in replacement
607$_ = "abc";
608/(a)/; s/(b)|(c)/-$^N/g;
609is($_,'a-b-c','#20682 $^N not visible in replacement');
7357df17
JH
610
611# [perl #22351] perl bug with 'e' substitution modifier
612my $name = "chris";
613{
614 no warnings 'uninitialized';
615 $name =~ s/hr//e;
616}
617is($name, "cis", q[#22351 bug with 'e' substitution modifier]);
01b35787
DM
618
619
620# [perl #34171] $1 didn't honour 'use bytes' in s//e
621{
622 my $s="\x{100}";
623 my $x;
624 {
625 use bytes;
626 $s=~ s/(..)/$x=$1/e
627 }
628 is(length($x), 2, '[perl #34171]');
629}
630
631
1749ea0d
ST
632{ # [perl #27940] perlbug: [\x00-\x1f] works, [\c@-\c_] does not
633 my $c;
634
635 ($c = "\x20\c@\x30\cA\x40\cZ\x50\c_\x60") =~ s/[\c@-\c_]//g;
636 is($c, "\x20\x30\x40\x50\x60", "s/[\\c\@-\\c_]//g");
637
638 ($c = "\x20\x00\x30\x01\x40\x1A\x50\x1F\x60") =~ s/[\x00-\x1f]//g;
639 is($c, "\x20\x30\x40\x50\x60", "s/[\\x00-\\x1f]//g");
640}
3be69782 641{
f0852a51
YO
642 $_ = "xy";
643 no warnings 'uninitialized';
644 /(((((((((x)))))))))(z)/; # clear $10
645 s/(((((((((x)))))))))(y)/${10}/;
646 is($_,"y","RT#6006: \$_ eq '$_'");
3be69782
RGS
647 $_ = "xr";
648 s/(((((((((x)))))))))(r)/fooba${10}/;
649 is($_,"foobar","RT#6006: \$_ eq '$_'");
f0852a51 650}
336b1602
YO
651{
652 my $want=("\n" x 11).("B\n" x 11)."B";
653 $_="B";
654 our $i;
655 for $i(1..11){
656 s/^.*$/$&/gm;
657 $_="\n$_\n$&";
658 }
659 is($want,$_,"RT#17542");
660}
1749ea0d 661
ce474962
NC
662{
663 my @tests = ('ABC', "\xA3\xA4\xA5", "\x{410}\x{411}\x{412}");
664 foreach (@tests) {
665 my $id = ord $_;
666 s/./pos/ge;
667 is($_, "012", "RT#52104: $id");
668 }
669}
831a7dd7
MM
670
671fresh_perl_is( '$_=q(foo);s/(.)\G//g;print' => 'foo', '[perl #69056] positive GPOS regex segfault' );
2c296965 672fresh_perl_is( '$_="abcef"; s/bc|(.)\G(.)/$1 ? "[$1-$2]" : "XX"/ge; print' => 'aXX[c-e][e-f]f', 'positive GPOS regex substitution failure' );
831a7dd7 673
92c404cd 674# [perl #71470] $var =~ s/$qr//e calling get-magic on $_ as well as $var
455d9033
FC
675{
676 local *_;
677 my $scratch;
678 sub qrBug::TIESCALAR { bless[pop], 'qrBug' }
679 sub qrBug::FETCH { $scratch .= "[fetching $_[0][0]]"; 'prew' }
680 sub qrBug::STORE{}
681 tie my $kror, qrBug => '$kror';
682 tie $_, qrBug => '$_';
683 my $qr = qr/(?:)/;
684 $kror =~ s/$qr/""/e;
685 is(
686 $scratch, '[fetching $kror]',
687 'bug: $var =~ s/$qr//e calling get-magic on $_ as well as $var',
688 );
689}
3e462cdc
KW
690
691{ # Bug #41530; replacing non-utf8 with a utf8 causes problems
692 my $string = "a\x{a0}a";
693 my $sub_string = $string;
694 ok(! utf8::is_utf8($sub_string), "Verify that string isn't initially utf8");
695 $sub_string =~ s/a/\x{100}/g;
696 ok(utf8::is_utf8($sub_string),
697 'Verify replace of non-utf8 with utf8 upgrades to utf8');
698 is($sub_string, "\x{100}\x{A0}\x{100}",
699 'Verify #41530 fixed: replace of non-utf8 with utf8');
700
701 my $non_sub_string = $string;
702 ok(! utf8::is_utf8($non_sub_string),
703 "Verify that string isn't initially utf8");
704 $non_sub_string =~ s/b/\x{100}/g;
705 ok(! utf8::is_utf8($non_sub_string),
706 "Verify that failed substitute doesn't change string's utf8ness");
707 is($non_sub_string, $string,
708 "Verify that failed substitute doesn't change string");
709}
84bb2957
KW
710
711{ # Verify largish octal in replacement pattern
712
713 my $string = "a";
714 $string =~ s/a/\400/;
715 is($string, chr 0x100, "Verify that handles s/foo/\\400/");
716 $string =~ s/./\600/;
717 is($string, chr 0x180, "Verify that handles s/foo/\\600/");
718 $string =~ s/./\777/;
719 is($string, chr 0x1FF, "Verify that handles s/foo/\\777/");
720}
af9838cc
FC
721
722# Scoping of s//the RHS/ when there is no /e
723# Tests based on [perl #19078]
724{
725 local *_;
726 my $output = ''; my %a;
727 no warnings 'uninitialized';
728
729 $_="CCCGGG";
730 s!.!<@a{$output .= ("$&"),/[$&]/g}>!g;
731 $output .= $_;
732 is(
733 $output, "CCCGGG< >< >< >< >< >< >",
734 's/// sets PL_curpm for each iteration even when the RHS has set it'
735 );
736
737 s/C/$a{m\G\}/;
738 is(
739 "$&", G =>
740 'Match vars reflect the last match after s/pat/$a{m|pat|}/ without /e'
741 );
742}
c95ca9b8
DM
743
744{
745 # a tied scalar that returned a plain string, got messed up
746 # when substituted with a UTF8 replacement string, due to
747 # magic getting called multiple times, and pointers now pointing
748 # to stale/freed strings
5c1648b0
FC
749 # The original fix for this caused infinite loops for non- or cow-
750 # strings, so we test those, too.
c95ca9b8
DM
751 package FOO;
752 my $fc;
753 sub TIESCALAR { bless [ "abcdefgh" ] }
754 sub FETCH { $fc++; $_[0][0] }
755 sub STORE { $_[0][0] = $_[1] }
756
757 my $s;
758 tie $s, 'FOO';
759 $s =~ s/..../\x{101}/;
760 ::is($fc, 1, "tied UTF8 stuff FETCH count");
761 ::is("$s", "\x{101}efgh", "tied UTF8 stuff");
5c1648b0
FC
762
763 ::watchdog(300);
764 $fc = 0;
765 $s = *foo;
766 $s =~ s/..../\x{101}/;
767 ::is($fc, 1, '$tied_glob =~ s/non-utf8/utf8/ fetch count');
768 ::is("$s", "\x{101}::foo", '$tied_glob =~ s/non-utf8/utf8/ result');
769 $fc = 0;
770 $s = *foo;
771 $s =~ s/(....)/\x{101}/g;
772 ::is($fc, 1, '$tied_glob =~ s/(non-utf8)/utf8/g fetch count');
773 ::is("$s", "\x{101}\x{101}o",
774 '$tied_glob =~ s/(non-utf8)/utf8/g result');
775 $fc = 0;
776 $s = "\xff\xff\xff\xff\xff";
777 $s =~ s/..../\x{101}/;
778 ::is($fc, 1, '$tied_latin1 =~ s/non-utf8/utf8/ fetch count');
779 ::is("$s", "\x{101}\xff", '$tied_latin1 =~ s/non-utf8/utf8/ result');
780 $fc = 0;
781 { package package_name; tied($s)->[0] = __PACKAGE__ };
782 $s =~ s/..../\x{101}/;
783 ::is($fc, 1, '$tied_cow =~ s/non-utf8/utf8/ fetch count');
784 ::is("$s", "\x{101}age_name", '$tied_cow =~ s/non-utf8/utf8/ result');
785 $fc = 0;
786 $s = \1;
787 $s =~ s/..../\x{101}/;
788 ::is($fc, 1, '$tied_ref =~ s/non-utf8/utf8/ fetch count');
789 ::like("$s", qr/^\x{101}AR\(0x.*\)\z/,
790 '$tied_ref =~ s/non-utf8/utf8/ result');
c95ca9b8 791}
0c1438a1
NC
792
793# RT #97954
794{
795 my $count;
796
797 sub bam::DESTROY {
798 --$count;
799 }
800
801 my $z_zapp = bless [], 'bam';
802 ++$count;
803
804 is($count, 1, '1 object');
805 is($z_zapp =~ s/.*/R/r, 'R', 'substitution happens');
806 is(ref $z_zapp, 'bam', 'still 1 object');
807 is($count, 1, 'still 1 object');
808 undef $z_zapp;
809 is($count, 0, 'now 0 objects');
810
811 $z_zapp = bless [], 'bam';
812 ++$count;
813
814 is($count, 1, '1 object');
815 like($z_zapp =~ s/./R/rg, qr/\AR{8,}\z/, 'substitution happens');
816 is(ref $z_zapp, 'bam', 'still 1 object');
817 is($count, 1, 'still 1 object');
818 undef $z_zapp;
819 is($count, 0, 'now 0 objects');
820}
821
822is(*bam =~ s/\*//r, 'main::bam', 'Can s///r a tyepglob');
823is(*bam =~ s/\*//rg, 'main::bam', 'Can s///rg a tyepglob');
6fc20922
FC
824
825{
826 sub cowBug::TIESCALAR { bless[], 'cowBug' }
827 sub cowBug::FETCH { __PACKAGE__ }
828 sub cowBug::STORE{}
829 tie my $kror, cowBug =>;
830 $kror =~ s/(?:)/""/e;
831}
610272b5
DM
832pass("s/// on tied var returning a cow");
833
834# a test for 6502e08109cd003b2cdf39bc94ef35e52203240b
835# previously this would segfault
836
837{
838 my $s = "abc";
839 eval { $s =~ s/(.)/die/e; };
840 like($@, qr/Died at/, "s//die/e");
841}
86e69b18
FC
842
843
844# [perl #26986] logop in repl resulting in incorrect optimisation
845"g" =~ /(.)/;
846@l{'a'..'z'} = 'A'..':';
847$_ = "hello";
848{ s/(.)/$l{my $a||$1}/g }
849is $_, "HELLO",
850 'logop in s/// repl does not result in "constant" repl optimisation';
64534138
FC
851
852$_ = "\xc4\x80";
853$a = "";
854utf8::upgrade $a;
855$_ =~ s/$/$a/;
856is $_, "\xc4\x80", "empty utf8 repl does not result in mangled utf8";