This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Quick integration of mainline changes to date
[perl5.git] / t / op / subst.t
CommitLineData
d9d8d8de
LW
1#!./perl
2
a1a0e61e
TD
3BEGIN {
4 chdir 't' if -d 't';
93430cb4 5 unshift @INC, '../lib' if -d '../lib';
a1a0e61e
TD
6 require Config; import Config;
7}
344462d3 8
146174a9 9print "1..84\n";
d9d8d8de
LW
10
11$x = 'foo';
12$_ = "x";
13s/x/\$x/;
14print "#1\t:$_: eq :\$x:\n";
15if ($_ eq '$x') {print "ok 1\n";} else {print "not ok 1\n";}
16
17$_ = "x";
18s/x/$x/;
19print "#2\t:$_: eq :foo:\n";
20if ($_ eq 'foo') {print "ok 2\n";} else {print "not ok 2\n";}
21
22$_ = "x";
23s/x/\$x $x/;
24print "#3\t:$_: eq :\$x foo:\n";
25if ($_ eq '$x foo') {print "ok 3\n";} else {print "not ok 3\n";}
26
27$b = 'cd';
79072805 28($a = 'abcdef') =~ s<(b${b}e)>'\n$1';
d9d8d8de
LW
29print "#4\t:$1: eq :bcde:\n";
30print "#4\t:$a: eq :a\\n\$1f:\n";
31if ($1 eq 'bcde' && $a eq 'a\n$1f') {print "ok 4\n";} else {print "not ok 4\n";}
32
33$a = 'abacada';
34if (($a =~ s/a/x/g) == 4 && $a eq 'xbxcxdx')
35 {print "ok 5\n";} else {print "not ok 5\n";}
36
37if (($a =~ s/a/y/g) == 0 && $a eq 'xbxcxdx')
38 {print "ok 6\n";} else {print "not ok 6 $a\n";}
39
40if (($a =~ s/b/y/g) == 1 && $a eq 'xyxcxdx')
41 {print "ok 7\n";} else {print "not ok 7 $a\n";}
42
43$_ = 'ABACADA';
44if (/a/i && s///gi && $_ eq 'BCD') {print "ok 8\n";} else {print "not ok 8 $_\n";}
45
46$_ = '\\' x 4;
47if (length($_) == 4) {print "ok 9\n";} else {print "not ok 9\n";}
48s/\\/\\\\/g;
49if ($_ eq '\\' x 8) {print "ok 10\n";} else {print "not ok 10 $_\n";}
50
51$_ = '\/' x 4;
52if (length($_) == 8) {print "ok 11\n";} else {print "not ok 11\n";}
53s/\//\/\//g;
54if ($_ eq '\\//' x 4) {print "ok 12\n";} else {print "not ok 12\n";}
55if (length($_) == 12) {print "ok 13\n";} else {print "not ok 13\n";}
56
57$_ = 'aaaXXXXbbb';
58s/^a//;
59print $_ eq 'aaXXXXbbb' ? "ok 14\n" : "not ok 14\n";
60
61$_ = 'aaaXXXXbbb';
62s/a//;
63print $_ eq 'aaXXXXbbb' ? "ok 15\n" : "not ok 15\n";
64
65$_ = 'aaaXXXXbbb';
66s/^a/b/;
67print $_ eq 'baaXXXXbbb' ? "ok 16\n" : "not ok 16\n";
68
69$_ = 'aaaXXXXbbb';
70s/a/b/;
71print $_ eq 'baaXXXXbbb' ? "ok 17\n" : "not ok 17\n";
72
73$_ = 'aaaXXXXbbb';
74s/aa//;
75print $_ eq 'aXXXXbbb' ? "ok 18\n" : "not ok 18\n";
76
77$_ = 'aaaXXXXbbb';
78s/aa/b/;
79print $_ eq 'baXXXXbbb' ? "ok 19\n" : "not ok 19\n";
80
81$_ = 'aaaXXXXbbb';
82s/b$//;
83print $_ eq 'aaaXXXXbb' ? "ok 20\n" : "not ok 20\n";
84
85$_ = 'aaaXXXXbbb';
86s/b//;
87print $_ eq 'aaaXXXXbb' ? "ok 21\n" : "not ok 21\n";
88
89$_ = 'aaaXXXXbbb';
90s/bb//;
91print $_ eq 'aaaXXXXb' ? "ok 22\n" : "not ok 22\n";
92
93$_ = 'aaaXXXXbbb';
94s/aX/y/;
95print $_ eq 'aayXXXbbb' ? "ok 23\n" : "not ok 23\n";
96
97$_ = 'aaaXXXXbbb';
98s/Xb/z/;
99print $_ eq 'aaaXXXzbb' ? "ok 24\n" : "not ok 24\n";
100
101$_ = 'aaaXXXXbbb';
102s/aaX.*Xbb//;
103print $_ eq 'ab' ? "ok 25\n" : "not ok 25\n";
104
105$_ = 'aaaXXXXbbb';
106s/bb/x/;
107print $_ eq 'aaaXXXXxb' ? "ok 26\n" : "not ok 26\n";
108
109# now for some unoptimized versions of the same.
110
111$_ = 'aaaXXXXbbb';
112$x ne $x || s/^a//;
113print $_ eq 'aaXXXXbbb' ? "ok 27\n" : "not ok 27\n";
114
115$_ = 'aaaXXXXbbb';
116$x ne $x || s/a//;
117print $_ eq 'aaXXXXbbb' ? "ok 28\n" : "not ok 28\n";
118
119$_ = 'aaaXXXXbbb';
120$x ne $x || s/^a/b/;
121print $_ eq 'baaXXXXbbb' ? "ok 29\n" : "not ok 29\n";
122
123$_ = 'aaaXXXXbbb';
124$x ne $x || s/a/b/;
125print $_ eq 'baaXXXXbbb' ? "ok 30\n" : "not ok 30\n";
126
127$_ = 'aaaXXXXbbb';
128$x ne $x || s/aa//;
129print $_ eq 'aXXXXbbb' ? "ok 31\n" : "not ok 31\n";
130
131$_ = 'aaaXXXXbbb';
132$x ne $x || s/aa/b/;
133print $_ eq 'baXXXXbbb' ? "ok 32\n" : "not ok 32\n";
134
135$_ = 'aaaXXXXbbb';
136$x ne $x || s/b$//;
137print $_ eq 'aaaXXXXbb' ? "ok 33\n" : "not ok 33\n";
138
139$_ = 'aaaXXXXbbb';
140$x ne $x || s/b//;
141print $_ eq 'aaaXXXXbb' ? "ok 34\n" : "not ok 34\n";
142
143$_ = 'aaaXXXXbbb';
144$x ne $x || s/bb//;
145print $_ eq 'aaaXXXXb' ? "ok 35\n" : "not ok 35\n";
146
147$_ = 'aaaXXXXbbb';
148$x ne $x || s/aX/y/;
149print $_ eq 'aayXXXbbb' ? "ok 36\n" : "not ok 36\n";
150
151$_ = 'aaaXXXXbbb';
152$x ne $x || s/Xb/z/;
153print $_ eq 'aaaXXXzbb' ? "ok 37\n" : "not ok 37\n";
154
155$_ = 'aaaXXXXbbb';
156$x ne $x || s/aaX.*Xbb//;
157print $_ eq 'ab' ? "ok 38\n" : "not ok 38\n";
158
159$_ = 'aaaXXXXbbb';
160$x ne $x || s/bb/x/;
161print $_ eq 'aaaXXXXxb' ? "ok 39\n" : "not ok 39\n";
162
163$_ = 'abc123xyz';
c277df42 164s/(\d+)/$1*2/e; # yields 'abc246xyz'
d9d8d8de 165print $_ eq 'abc246xyz' ? "ok 40\n" : "not ok 40\n";
c277df42 166s/(\d+)/sprintf("%5d",$1)/e; # yields 'abc 246xyz'
d9d8d8de 167print $_ eq 'abc 246xyz' ? "ok 41\n" : "not ok 41\n";
c277df42 168s/(\w)/$1 x 2/eg; # yields 'aabbcc 224466xxyyzz'
d9d8d8de
LW
169print $_ eq 'aabbcc 224466xxyyzz' ? "ok 42\n" : "not ok 42\n";
170
171$_ = "aaaaa";
172print y/a/b/ == 5 ? "ok 43\n" : "not ok 43\n";
173print y/a/b/ == 0 ? "ok 44\n" : "not ok 44\n";
174print y/b// == 5 ? "ok 45\n" : "not ok 45\n";
175print y/b/c/s == 5 ? "ok 46\n" : "not ok 46\n";
176print y/c// == 1 ? "ok 47\n" : "not ok 47\n";
177print y/c//d == 1 ? "ok 48\n" : "not ok 48\n";
178print $_ eq "" ? "ok 49\n" : "not ok 49\n";
179
180$_ = "Now is the %#*! time for all good men...";
181print (($x=(y/a-zA-Z //cd)) == 7 ? "ok 50\n" : "not ok 50\n");
182print y/ / /s == 8 ? "ok 51\n" : "not ok 51\n";
183
79072805
LW
184$_ = 'abcdefghijklmnopqrstuvwxyz0123456789';
185tr/a-z/A-Z/;
186
187print $_ eq 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' ? "ok 52\n" : "not ok 52\n";
188
189# same as tr/A-Z/a-z/;
a1a0e61e 190if ($Config{ebcdic} eq 'define') { # EBCDIC.
6e68dac8 191 no utf8;
9d116dd7
JH
192 y[\301-\351][\201-\251];
193} else { # Ye Olde ASCII. Or something like it.
194 y[\101-\132][\141-\172];
195}
79072805
LW
196
197print $_ eq 'abcdefghijklmnopqrstuvwxyz0123456789' ? "ok 53\n" : "not ok 53\n";
198
9d116dd7
JH
199if (ord("+") == ord(",") - 1 && ord(",") == ord("-") - 1 &&
200 ord("a") == ord("b") - 1 && ord("b") == ord("c") - 1) {
201 $_ = '+,-';
202 tr/+--/a-c/;
203 print "not " unless $_ eq 'abc';
204}
205print "ok 54\n";
79072805
LW
206
207$_ = '+,-';
208tr/+\--/a\/c/;
209print $_ eq 'a,/' ? "ok 55\n" : "not ok 55\n";
210
211$_ = '+,-';
212tr/-+,/ab\-/;
213print $_ eq 'b-a' ? "ok 56\n" : "not ok 56\n";
843b4603
TB
214
215
216# test recursive substitutions
217# code based on the recursive expansion of makefile variables
218
219my %MK = (
220 AAAAA => '$(B)', B=>'$(C)', C => 'D', # long->short
221 E => '$(F)', F=>'p $(G) q', G => 'HHHHH', # short->long
222 DIR => '$(UNDEFINEDNAME)/xxx',
223);
224sub var {
225 my($var,$level) = @_;
226 return "\$($var)" unless exists $MK{$var};
227 return exp_vars($MK{$var}, $level+1); # can recurse
228}
229sub exp_vars {
230 my($str,$level) = @_;
231 $str =~ s/\$\((\w+)\)/var($1, $level+1)/ge; # can recurse
232 #warn "exp_vars $level = '$str'\n";
233 $str;
234}
235
236print exp_vars('$(AAAAA)',0) eq 'D'
237 ? "ok 57\n" : "not ok 57\n";
238print exp_vars('$(E)',0) eq 'p HHHHH q'
239 ? "ok 58\n" : "not ok 58\n";
240print exp_vars('$(DIR)',0) eq '$(UNDEFINEDNAME)/xxx'
241 ? "ok 59\n" : "not ok 59\n";
242print exp_vars('foo $(DIR)/yyy bar',0) eq 'foo $(UNDEFINEDNAME)/xxx/yyy bar'
243 ? "ok 60\n" : "not ok 60\n";
244
3e3baf6d
TB
245# a match nested in the RHS of a substitution:
246
247$_ = "abcd";
c277df42 248s/(..)/$x = $1, m#.#/eg;
3e3baf6d 249print $x eq "cd" ? "ok 61\n" : "not ok 61\n";
fb73857a 250
c277df42
IZ
251# Subst and lookbehind
252
253$_="ccccc";
254s/(?<!x)c/x/g;
255print $_ eq "xxxxx" ? "ok 62\n" : "not ok 62 # `$_' ne `xxxxx'\n";
256
257$_="ccccc";
258s/(?<!x)(c)/x/g;
259print $_ eq "xxxxx" ? "ok 63\n" : "not ok 63 # `$_' ne `xxxxx'\n";
260
261$_="foobbarfoobbar";
262s/(?<!r)foobbar/foobar/g;
263print $_ eq "foobarfoobbar" ? "ok 64\n" : "not ok 64 # `$_' ne `foobarfoobbar'\n";
264
265$_="foobbarfoobbar";
266s/(?<!ar)(foobbar)/foobar/g;
267print $_ eq "foobarfoobbar" ? "ok 65\n" : "not ok 65 # `$_' ne `foobarfoobbar'\n";
268
269$_="foobbarfoobbar";
270s/(?<!ar)foobbar/foobar/g;
271print $_ eq "foobarfoobbar" ? "ok 66\n" : "not ok 66 # `$_' ne `foobarfoobbar'\n";
272
fb73857a 273# check parsing of split subst with comment
274eval 's{foo} # this is a comment, not a delimiter
275 {bar};';
c277df42 276print @? ? "not ok 67\n" : "ok 67\n";
f3ea7b5e
IH
277
278# check if squashing works at the end of string
279$_="baacbaa";
280tr/a/b/s;
281print $_ eq "bbcbb" ? "ok 68\n" : "not ok 68 # `$_' ne `bbcbb'\n";
282
2216f30a
GS
283# XXX TODO: Most tests above don't test return values of the ops. They should.
284$_ = "ab";
285print (s/a/b/ == 1 ? "ok 69\n" : "not ok 69\n");
ce862d02
IZ
286
287$_ = <<'EOL';
288 $url = new URI::URL "http://www/"; die if $url eq "xXx";
289EOL
290$^R = 'junk';
291
292$foo = ' $@%#lowercase $@%# lowercase UPPERCASE$@%#UPPERCASE' .
293 ' $@%#lowercase$@%#lowercase$@%# lowercase lowercase $@%#lowercase' .
294 ' lowercase $@%#MiXeD$@%# ';
295
296s{ \d+ \b [,.;]? (?{ 'digits' })
297 |
298 [a-z]+ \b [,.;]? (?{ 'lowercase' })
299 |
300 [A-Z]+ \b [,.;]? (?{ 'UPPERCASE' })
301 |
302 [A-Z] [a-z]+ \b [,.;]? (?{ 'Capitalized' })
303 |
304 [A-Za-z]+ \b [,.;]? (?{ 'MiXeD' })
305 |
306 [A-Za-z0-9]+ \b [,.;]? (?{ 'alphanumeric' })
307 |
308 \s+ (?{ ' ' })
309 |
310 [^A-Za-z0-9\s]+ (?{ '$@%#' })
311}{$^R}xg;
312print ($_ eq $foo ? "ok 70\n" : "not ok 70\n#'$_'\n#'$foo'\n");
313
2beec16e 314$_ = 'x' x 20;
f5c9036e 315s/(\d*|x)/<$1>/g;
2beec16e
IZ
316$foo = '<>' . ('<x><>' x 20) ;
317print ($_ eq $foo ? "ok 71\n" : "not ok 71\n#'$_'\n#'$foo'\n");
ad94a511
IZ
318
319$t = 'aaaaaaaaa';
320
321$_ = $t;
322pos = 6;
323s/\Ga/xx/g;
324print "not " unless $_ eq 'aaaaaaxxxxxx';
325print "ok 72\n";
326
327$_ = $t;
328pos = 6;
329s/\Ga/x/g;
330print "not " unless $_ eq 'aaaaaaxxx';
331print "ok 73\n";
332
333$_ = $t;
334pos = 6;
335s/\Ga/xx/;
336print "not " unless $_ eq 'aaaaaaxxaa';
337print "ok 74\n";
338
339$_ = $t;
340pos = 6;
341s/\Ga/x/;
342print "not " unless $_ eq 'aaaaaaxaa';
343print "ok 75\n";
344
345$_ = $t;
346s/\Ga/xx/g;
347print "not " unless $_ eq 'xxxxxxxxxxxxxxxxxx';
348print "ok 76\n";
349
350$_ = $t;
351s/\Ga/x/g;
352print "not " unless $_ eq 'xxxxxxxxx';
353print "ok 77\n";
354
355$_ = $t;
356s/\Ga/xx/;
357print "not " unless $_ eq 'xxaaaaaaaa';
358print "ok 78\n";
359
360$_ = $t;
361s/\Ga/x/;
362print "not " unless $_ eq 'xaaaaaaaa';
363print "ok 79\n";
364
f5c9036e
IZ
365$_ = 'aaaa';
366s/\ba/./g;
367print "#'$_'\nnot " unless $_ eq '.aaa';
ad94a511
IZ
368print "ok 80\n";
369
e9fa98b2 370eval q% s/a/"b"}/e %;
f5c9036e 371print ($@ =~ /Bad evalled substitution/ ? "ok 81\n" : "not ok 81\n");
e9fa98b2 372eval q% ($_ = "x") =~ s/(.)/"$1 "/e %;
f5c9036e 373print +($_ eq "x " and !length $@) ? "ok 82\n" : "not ok 82\n# \$_ eq $_, $@\n";
43a16006
HS
374$x = $x = 'interp';
375eval q% ($_ = "x") =~ s/x(($x)*)/"$1"/e %;
376print +($_ eq '' and !length $@) ? "ok 83\n" : "not ok 83\n# \$_ eq $_, $@\n";
e9fa98b2 377
146174a9
CB
378$_ = "C:/";
379s/^([a-z]:)/\u$1/ and print "not ";
380print "ok 84\n";
e9fa98b2 381