xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/t/op/subst.t (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!./perl -wT
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateBEGIN {
4*0Sstevel@tonic-gate    chdir 't' if -d 't';
5*0Sstevel@tonic-gate    @INC = '../lib';
6*0Sstevel@tonic-gate    require Config; import Config;
7*0Sstevel@tonic-gate}
8*0Sstevel@tonic-gate
9*0Sstevel@tonic-gaterequire './test.pl';
10*0Sstevel@tonic-gateplan( tests => 130 );
11*0Sstevel@tonic-gate
12*0Sstevel@tonic-gate$x = 'foo';
13*0Sstevel@tonic-gate$_ = "x";
14*0Sstevel@tonic-gates/x/\$x/;
15*0Sstevel@tonic-gateok( $_ eq '$x', ":$_: eq :\$x:" );
16*0Sstevel@tonic-gate
17*0Sstevel@tonic-gate$_ = "x";
18*0Sstevel@tonic-gates/x/$x/;
19*0Sstevel@tonic-gateok( $_ eq 'foo', ":$_: eq :foo:" );
20*0Sstevel@tonic-gate
21*0Sstevel@tonic-gate$_ = "x";
22*0Sstevel@tonic-gates/x/\$x $x/;
23*0Sstevel@tonic-gateok( $_ eq '$x foo', ":$_: eq :\$x foo:" );
24*0Sstevel@tonic-gate
25*0Sstevel@tonic-gate$b = 'cd';
26*0Sstevel@tonic-gate($a = 'abcdef') =~ s<(b${b}e)>'\n$1';
27*0Sstevel@tonic-gateok( $1 eq 'bcde' && $a eq 'a\n$1f', ":$1: eq :bcde: ; :$a: eq :a\\n\$1f:" );
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate$a = 'abacada';
30*0Sstevel@tonic-gateok( ($a =~ s/a/x/g) == 4 && $a eq 'xbxcxdx' );
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gateok( ($a =~ s/a/y/g) == 0 && $a eq 'xbxcxdx' );
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gateok( ($a =~ s/b/y/g) == 1 && $a eq 'xyxcxdx' );
35*0Sstevel@tonic-gate
36*0Sstevel@tonic-gate$_ = 'ABACADA';
37*0Sstevel@tonic-gateok( /a/i && s///gi && $_ eq 'BCD' );
38*0Sstevel@tonic-gate
39*0Sstevel@tonic-gate$_ = '\\' x 4;
40*0Sstevel@tonic-gateok( length($_) == 4 );
41*0Sstevel@tonic-gate$snum = s/\\/\\\\/g;
42*0Sstevel@tonic-gateok( $_ eq '\\' x 8 && $snum == 4 );
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gate$_ = '\/' x 4;
45*0Sstevel@tonic-gateok( length($_) == 8 );
46*0Sstevel@tonic-gate$snum = s/\//\/\//g;
47*0Sstevel@tonic-gateok( $_ eq '\\//' x 4 && $snum == 4 );
48*0Sstevel@tonic-gateok( length($_) == 12 );
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
51*0Sstevel@tonic-gates/^a//;
52*0Sstevel@tonic-gateok( $_ eq 'aaXXXXbbb' );
53*0Sstevel@tonic-gate
54*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
55*0Sstevel@tonic-gates/a//;
56*0Sstevel@tonic-gateok( $_ eq 'aaXXXXbbb' );
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
59*0Sstevel@tonic-gates/^a/b/;
60*0Sstevel@tonic-gateok( $_ eq 'baaXXXXbbb' );
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
63*0Sstevel@tonic-gates/a/b/;
64*0Sstevel@tonic-gateok( $_ eq 'baaXXXXbbb' );
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
67*0Sstevel@tonic-gates/aa//;
68*0Sstevel@tonic-gateok( $_ eq 'aXXXXbbb' );
69*0Sstevel@tonic-gate
70*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
71*0Sstevel@tonic-gates/aa/b/;
72*0Sstevel@tonic-gateok( $_ eq 'baXXXXbbb' );
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
75*0Sstevel@tonic-gates/b$//;
76*0Sstevel@tonic-gateok( $_ eq 'aaaXXXXbb' );
77*0Sstevel@tonic-gate
78*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
79*0Sstevel@tonic-gates/b//;
80*0Sstevel@tonic-gateok( $_ eq 'aaaXXXXbb' );
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
83*0Sstevel@tonic-gates/bb//;
84*0Sstevel@tonic-gateok( $_ eq 'aaaXXXXb' );
85*0Sstevel@tonic-gate
86*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
87*0Sstevel@tonic-gates/aX/y/;
88*0Sstevel@tonic-gateok( $_ eq 'aayXXXbbb' );
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
91*0Sstevel@tonic-gates/Xb/z/;
92*0Sstevel@tonic-gateok( $_ eq 'aaaXXXzbb' );
93*0Sstevel@tonic-gate
94*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
95*0Sstevel@tonic-gates/aaX.*Xbb//;
96*0Sstevel@tonic-gateok( $_ eq 'ab' );
97*0Sstevel@tonic-gate
98*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
99*0Sstevel@tonic-gates/bb/x/;
100*0Sstevel@tonic-gateok( $_ eq 'aaaXXXXxb' );
101*0Sstevel@tonic-gate
102*0Sstevel@tonic-gate# now for some unoptimized versions of the same.
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
105*0Sstevel@tonic-gate$x ne $x || s/^a//;
106*0Sstevel@tonic-gateok( $_ eq 'aaXXXXbbb' );
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
109*0Sstevel@tonic-gate$x ne $x || s/a//;
110*0Sstevel@tonic-gateok( $_ eq 'aaXXXXbbb' );
111*0Sstevel@tonic-gate
112*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
113*0Sstevel@tonic-gate$x ne $x || s/^a/b/;
114*0Sstevel@tonic-gateok( $_ eq 'baaXXXXbbb' );
115*0Sstevel@tonic-gate
116*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
117*0Sstevel@tonic-gate$x ne $x || s/a/b/;
118*0Sstevel@tonic-gateok( $_ eq 'baaXXXXbbb' );
119*0Sstevel@tonic-gate
120*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
121*0Sstevel@tonic-gate$x ne $x || s/aa//;
122*0Sstevel@tonic-gateok( $_ eq 'aXXXXbbb' );
123*0Sstevel@tonic-gate
124*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
125*0Sstevel@tonic-gate$x ne $x || s/aa/b/;
126*0Sstevel@tonic-gateok( $_ eq 'baXXXXbbb' );
127*0Sstevel@tonic-gate
128*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
129*0Sstevel@tonic-gate$x ne $x || s/b$//;
130*0Sstevel@tonic-gateok( $_ eq 'aaaXXXXbb' );
131*0Sstevel@tonic-gate
132*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
133*0Sstevel@tonic-gate$x ne $x || s/b//;
134*0Sstevel@tonic-gateok( $_ eq 'aaaXXXXbb' );
135*0Sstevel@tonic-gate
136*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
137*0Sstevel@tonic-gate$x ne $x || s/bb//;
138*0Sstevel@tonic-gateok( $_ eq 'aaaXXXXb' );
139*0Sstevel@tonic-gate
140*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
141*0Sstevel@tonic-gate$x ne $x || s/aX/y/;
142*0Sstevel@tonic-gateok( $_ eq 'aayXXXbbb' );
143*0Sstevel@tonic-gate
144*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
145*0Sstevel@tonic-gate$x ne $x || s/Xb/z/;
146*0Sstevel@tonic-gateok( $_ eq 'aaaXXXzbb' );
147*0Sstevel@tonic-gate
148*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
149*0Sstevel@tonic-gate$x ne $x || s/aaX.*Xbb//;
150*0Sstevel@tonic-gateok( $_ eq 'ab' );
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gate$_ = 'aaaXXXXbbb';
153*0Sstevel@tonic-gate$x ne $x || s/bb/x/;
154*0Sstevel@tonic-gateok( $_ eq 'aaaXXXXxb' );
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gate$_ = 'abc123xyz';
157*0Sstevel@tonic-gates/(\d+)/$1*2/e;              # yields 'abc246xyz'
158*0Sstevel@tonic-gateok( $_ eq 'abc246xyz' );
159*0Sstevel@tonic-gates/(\d+)/sprintf("%5d",$1)/e; # yields 'abc  246xyz'
160*0Sstevel@tonic-gateok( $_ eq 'abc  246xyz' );
161*0Sstevel@tonic-gates/(\w)/$1 x 2/eg;            # yields 'aabbcc  224466xxyyzz'
162*0Sstevel@tonic-gateok( $_ eq 'aabbcc  224466xxyyzz' );
163*0Sstevel@tonic-gate
164*0Sstevel@tonic-gate$_ = "aaaaa";
165*0Sstevel@tonic-gateok( y/a/b/ == 5 );
166*0Sstevel@tonic-gateok( y/a/b/ == 0 );
167*0Sstevel@tonic-gateok( y/b// == 5 );
168*0Sstevel@tonic-gateok( y/b/c/s == 5 );
169*0Sstevel@tonic-gateok( y/c// == 1 );
170*0Sstevel@tonic-gateok( y/c//d == 1 );
171*0Sstevel@tonic-gateok( $_ eq "" );
172*0Sstevel@tonic-gate
173*0Sstevel@tonic-gate$_ = "Now is the %#*! time for all good men...";
174*0Sstevel@tonic-gateok( ($x=(y/a-zA-Z //cd)) == 7 );
175*0Sstevel@tonic-gateok( y/ / /s == 8 );
176*0Sstevel@tonic-gate
177*0Sstevel@tonic-gate$_ = 'abcdefghijklmnopqrstuvwxyz0123456789';
178*0Sstevel@tonic-gatetr/a-z/A-Z/;
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gateok( $_ eq 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' );
181*0Sstevel@tonic-gate
182*0Sstevel@tonic-gate# same as tr/A-Z/a-z/;
183*0Sstevel@tonic-gateif (defined $Config{ebcdic} && $Config{ebcdic} eq 'define') {	# EBCDIC.
184*0Sstevel@tonic-gate    no utf8;
185*0Sstevel@tonic-gate    y[\301-\351][\201-\251];
186*0Sstevel@tonic-gate} else {		# Ye Olde ASCII.  Or something like it.
187*0Sstevel@tonic-gate    y[\101-\132][\141-\172];
188*0Sstevel@tonic-gate}
189*0Sstevel@tonic-gate
190*0Sstevel@tonic-gateok( $_ eq 'abcdefghijklmnopqrstuvwxyz0123456789' );
191*0Sstevel@tonic-gate
192*0Sstevel@tonic-gateSKIP: {
193*0Sstevel@tonic-gate    skip("not ASCII",1) unless (ord("+") == ord(",") - 1
194*0Sstevel@tonic-gate			     && ord(",") == ord("-") - 1
195*0Sstevel@tonic-gate			     && ord("a") == ord("b") - 1
196*0Sstevel@tonic-gate			     && ord("b") == ord("c") - 1);
197*0Sstevel@tonic-gate    $_ = '+,-';
198*0Sstevel@tonic-gate    tr/+--/a-c/;
199*0Sstevel@tonic-gate    ok( $_ eq 'abc' );
200*0Sstevel@tonic-gate}
201*0Sstevel@tonic-gate
202*0Sstevel@tonic-gate$_ = '+,-';
203*0Sstevel@tonic-gatetr/+\--/a\/c/;
204*0Sstevel@tonic-gateok( $_ eq 'a,/' );
205*0Sstevel@tonic-gate
206*0Sstevel@tonic-gate$_ = '+,-';
207*0Sstevel@tonic-gatetr/-+,/ab\-/;
208*0Sstevel@tonic-gateok( $_ eq 'b-a' );
209*0Sstevel@tonic-gate
210*0Sstevel@tonic-gate
211*0Sstevel@tonic-gate# test recursive substitutions
212*0Sstevel@tonic-gate# code based on the recursive expansion of makefile variables
213*0Sstevel@tonic-gate
214*0Sstevel@tonic-gatemy %MK = (
215*0Sstevel@tonic-gate    AAAAA => '$(B)', B=>'$(C)', C => 'D',			# long->short
216*0Sstevel@tonic-gate    E     => '$(F)', F=>'p $(G) q', G => 'HHHHH',	# short->long
217*0Sstevel@tonic-gate    DIR => '$(UNDEFINEDNAME)/xxx',
218*0Sstevel@tonic-gate);
219*0Sstevel@tonic-gatesub var {
220*0Sstevel@tonic-gate    my($var,$level) = @_;
221*0Sstevel@tonic-gate    return "\$($var)" unless exists $MK{$var};
222*0Sstevel@tonic-gate    return exp_vars($MK{$var}, $level+1); # can recurse
223*0Sstevel@tonic-gate}
224*0Sstevel@tonic-gatesub exp_vars {
225*0Sstevel@tonic-gate    my($str,$level) = @_;
226*0Sstevel@tonic-gate    $str =~ s/\$\((\w+)\)/var($1, $level+1)/ge; # can recurse
227*0Sstevel@tonic-gate    #warn "exp_vars $level = '$str'\n";
228*0Sstevel@tonic-gate    $str;
229*0Sstevel@tonic-gate}
230*0Sstevel@tonic-gate
231*0Sstevel@tonic-gateok( exp_vars('$(AAAAA)',0)           eq 'D' );
232*0Sstevel@tonic-gateok( exp_vars('$(E)',0)               eq 'p HHHHH q' );
233*0Sstevel@tonic-gateok( exp_vars('$(DIR)',0)             eq '$(UNDEFINEDNAME)/xxx' );
234*0Sstevel@tonic-gateok( exp_vars('foo $(DIR)/yyy bar',0) eq 'foo $(UNDEFINEDNAME)/xxx/yyy bar' );
235*0Sstevel@tonic-gate
236*0Sstevel@tonic-gate$_ = "abcd";
237*0Sstevel@tonic-gates/(..)/$x = $1, m#.#/eg;
238*0Sstevel@tonic-gateok( $x eq "cd", 'a match nested in the RHS of a substitution' );
239*0Sstevel@tonic-gate
240*0Sstevel@tonic-gate# Subst and lookbehind
241*0Sstevel@tonic-gate
242*0Sstevel@tonic-gate$_="ccccc";
243*0Sstevel@tonic-gate$snum = s/(?<!x)c/x/g;
244*0Sstevel@tonic-gateok( $_ eq "xxxxx" && $snum == 5 );
245*0Sstevel@tonic-gate
246*0Sstevel@tonic-gate$_="ccccc";
247*0Sstevel@tonic-gate$snum = s/(?<!x)(c)/x/g;
248*0Sstevel@tonic-gateok( $_ eq "xxxxx" && $snum == 5 );
249*0Sstevel@tonic-gate
250*0Sstevel@tonic-gate$_="foobbarfoobbar";
251*0Sstevel@tonic-gate$snum = s/(?<!r)foobbar/foobar/g;
252*0Sstevel@tonic-gateok( $_ eq "foobarfoobbar" && $snum == 1 );
253*0Sstevel@tonic-gate
254*0Sstevel@tonic-gate$_="foobbarfoobbar";
255*0Sstevel@tonic-gate$snum = s/(?<!ar)(foobbar)/foobar/g;
256*0Sstevel@tonic-gateok( $_ eq "foobarfoobbar" && $snum == 1 );
257*0Sstevel@tonic-gate
258*0Sstevel@tonic-gate$_="foobbarfoobbar";
259*0Sstevel@tonic-gate$snum = s/(?<!ar)foobbar/foobar/g;
260*0Sstevel@tonic-gateok( $_ eq "foobarfoobbar" && $snum == 1 );
261*0Sstevel@tonic-gate
262*0Sstevel@tonic-gateeval 's{foo} # this is a comment, not a delimiter
263*0Sstevel@tonic-gate       {bar};';
264*0Sstevel@tonic-gateok( ! @?, 'parsing of split subst with comment' );
265*0Sstevel@tonic-gate
266*0Sstevel@tonic-gate$_="baacbaa";
267*0Sstevel@tonic-gate$snum = tr/a/b/s;
268*0Sstevel@tonic-gateok( $_ eq "bbcbb" && $snum == 4,
269*0Sstevel@tonic-gate    'check if squashing works at the end of string' );
270*0Sstevel@tonic-gate
271*0Sstevel@tonic-gate$_ = "ab";
272*0Sstevel@tonic-gateok( s/a/b/ == 1 );
273*0Sstevel@tonic-gate
274*0Sstevel@tonic-gate$_ = <<'EOL';
275*0Sstevel@tonic-gate     $url = new URI::URL "http://www/";   die if $url eq "xXx";
276*0Sstevel@tonic-gateEOL
277*0Sstevel@tonic-gate$^R = 'junk';
278*0Sstevel@tonic-gate
279*0Sstevel@tonic-gate$foo = ' $@%#lowercase $@%# lowercase UPPERCASE$@%#UPPERCASE' .
280*0Sstevel@tonic-gate  ' $@%#lowercase$@%#lowercase$@%# lowercase lowercase $@%#lowercase' .
281*0Sstevel@tonic-gate  ' lowercase $@%#MiXeD$@%# ';
282*0Sstevel@tonic-gate
283*0Sstevel@tonic-gate$snum =
284*0Sstevel@tonic-gates{  \d+          \b [,.;]? (?{ 'digits' })
285*0Sstevel@tonic-gate   |
286*0Sstevel@tonic-gate    [a-z]+       \b [,.;]? (?{ 'lowercase' })
287*0Sstevel@tonic-gate   |
288*0Sstevel@tonic-gate    [A-Z]+       \b [,.;]? (?{ 'UPPERCASE' })
289*0Sstevel@tonic-gate   |
290*0Sstevel@tonic-gate    [A-Z] [a-z]+ \b [,.;]? (?{ 'Capitalized' })
291*0Sstevel@tonic-gate   |
292*0Sstevel@tonic-gate    [A-Za-z]+    \b [,.;]? (?{ 'MiXeD' })
293*0Sstevel@tonic-gate   |
294*0Sstevel@tonic-gate    [A-Za-z0-9]+ \b [,.;]? (?{ 'alphanumeric' })
295*0Sstevel@tonic-gate   |
296*0Sstevel@tonic-gate    \s+                    (?{ ' ' })
297*0Sstevel@tonic-gate   |
298*0Sstevel@tonic-gate    [^A-Za-z0-9\s]+          (?{ '$@%#' })
299*0Sstevel@tonic-gate}{$^R}xg;
300*0Sstevel@tonic-gateok( $_ eq $foo );
301*0Sstevel@tonic-gateok( $snum == 31 );
302*0Sstevel@tonic-gate
303*0Sstevel@tonic-gate$_ = 'a' x 6;
304*0Sstevel@tonic-gate$snum = s/a(?{})//g;
305*0Sstevel@tonic-gateok( $_ eq '' && $snum == 6 );
306*0Sstevel@tonic-gate
307*0Sstevel@tonic-gate$_ = 'x' x 20;
308*0Sstevel@tonic-gate$snum = s/(\d*|x)/<$1>/g;
309*0Sstevel@tonic-gate$foo = '<>' . ('<x><>' x 20) ;
310*0Sstevel@tonic-gateok( $_ eq $foo && $snum == 41 );
311*0Sstevel@tonic-gate
312*0Sstevel@tonic-gate$t = 'aaaaaaaaa';
313*0Sstevel@tonic-gate
314*0Sstevel@tonic-gate$_ = $t;
315*0Sstevel@tonic-gatepos = 6;
316*0Sstevel@tonic-gate$snum = s/\Ga/xx/g;
317*0Sstevel@tonic-gateok( $_ eq 'aaaaaaxxxxxx' && $snum == 3 );
318*0Sstevel@tonic-gate
319*0Sstevel@tonic-gate$_ = $t;
320*0Sstevel@tonic-gatepos = 6;
321*0Sstevel@tonic-gate$snum = s/\Ga/x/g;
322*0Sstevel@tonic-gateok( $_ eq 'aaaaaaxxx' && $snum == 3 );
323*0Sstevel@tonic-gate
324*0Sstevel@tonic-gate$_ = $t;
325*0Sstevel@tonic-gatepos = 6;
326*0Sstevel@tonic-gates/\Ga/xx/;
327*0Sstevel@tonic-gateok( $_ eq 'aaaaaaxxaa' );
328*0Sstevel@tonic-gate
329*0Sstevel@tonic-gate$_ = $t;
330*0Sstevel@tonic-gatepos = 6;
331*0Sstevel@tonic-gates/\Ga/x/;
332*0Sstevel@tonic-gateok( $_ eq 'aaaaaaxaa' );
333*0Sstevel@tonic-gate
334*0Sstevel@tonic-gate$_ = $t;
335*0Sstevel@tonic-gate$snum = s/\Ga/xx/g;
336*0Sstevel@tonic-gateok( $_ eq 'xxxxxxxxxxxxxxxxxx' && $snum == 9 );
337*0Sstevel@tonic-gate
338*0Sstevel@tonic-gate$_ = $t;
339*0Sstevel@tonic-gate$snum = s/\Ga/x/g;
340*0Sstevel@tonic-gateok( $_ eq 'xxxxxxxxx' && $snum == 9 );
341*0Sstevel@tonic-gate
342*0Sstevel@tonic-gate$_ = $t;
343*0Sstevel@tonic-gates/\Ga/xx/;
344*0Sstevel@tonic-gateok( $_ eq 'xxaaaaaaaa' );
345*0Sstevel@tonic-gate
346*0Sstevel@tonic-gate$_ = $t;
347*0Sstevel@tonic-gates/\Ga/x/;
348*0Sstevel@tonic-gateok( $_ eq 'xaaaaaaaa' );
349*0Sstevel@tonic-gate
350*0Sstevel@tonic-gate$_ = 'aaaa';
351*0Sstevel@tonic-gate$snum = s/\ba/./g;
352*0Sstevel@tonic-gateok( $_ eq '.aaa' && $snum == 1 );
353*0Sstevel@tonic-gate
354*0Sstevel@tonic-gateeval q% s/a/"b"}/e %;
355*0Sstevel@tonic-gateok( $@ =~ /Bad evalled substitution/ );
356*0Sstevel@tonic-gateeval q% ($_ = "x") =~ s/(.)/"$1 "/e %;
357*0Sstevel@tonic-gateok( $_ eq "x " and !length $@ );
358*0Sstevel@tonic-gate$x = $x = 'interp';
359*0Sstevel@tonic-gateeval q% ($_ = "x") =~ s/x(($x)*)/"$1"/e %;
360*0Sstevel@tonic-gateok( $_ eq '' and !length $@ );
361*0Sstevel@tonic-gate
362*0Sstevel@tonic-gate$_ = "C:/";
363*0Sstevel@tonic-gateok( !s/^([a-z]:)/\u$1/ );
364*0Sstevel@tonic-gate
365*0Sstevel@tonic-gate$_ = "Charles Bronson";
366*0Sstevel@tonic-gate$snum = s/\B\w//g;
367*0Sstevel@tonic-gateok( $_ eq "C B" && $snum == 12 );
368*0Sstevel@tonic-gate
369*0Sstevel@tonic-gate{
370*0Sstevel@tonic-gate    use utf8;
371*0Sstevel@tonic-gate    my $s = "H\303\266he";
372*0Sstevel@tonic-gate    my $l = my $r = $s;
373*0Sstevel@tonic-gate    $l =~ s/[^\w]//g;
374*0Sstevel@tonic-gate    $r =~ s/[^\w\.]//g;
375*0Sstevel@tonic-gate    is($l, $r, "use utf8 \\w");
376*0Sstevel@tonic-gate}
377*0Sstevel@tonic-gate
378*0Sstevel@tonic-gatemy $pv1 = my $pv2  = "Andreas J. K\303\266nig";
379*0Sstevel@tonic-gate$pv1 =~ s/A/\x{100}/;
380*0Sstevel@tonic-gatesubstr($pv2,0,1) = "\x{100}";
381*0Sstevel@tonic-gateis($pv1, $pv2);
382*0Sstevel@tonic-gate
383*0Sstevel@tonic-gateSKIP: {
384*0Sstevel@tonic-gate    skip("EBCDIC", 3) if ord("A") == 193;
385*0Sstevel@tonic-gate
386*0Sstevel@tonic-gate    {
387*0Sstevel@tonic-gate	# Gregor Chrupala <gregor.chrupala@star-group.net>
388*0Sstevel@tonic-gate	use utf8;
389*0Sstevel@tonic-gate	$a = 'Espa&ntilde;a';
390*0Sstevel@tonic-gate	$a =~ s/&ntilde;/ñ/;
391*0Sstevel@tonic-gate	like($a, qr/ñ/, "use utf8 RHS");
392*0Sstevel@tonic-gate    }
393*0Sstevel@tonic-gate
394*0Sstevel@tonic-gate    {
395*0Sstevel@tonic-gate	use utf8;
396*0Sstevel@tonic-gate	$a = 'España España';
397*0Sstevel@tonic-gate	$a =~ s/ñ/&ntilde;/;
398*0Sstevel@tonic-gate	like($a, qr/ñ/, "use utf8 LHS");
399*0Sstevel@tonic-gate    }
400*0Sstevel@tonic-gate
401*0Sstevel@tonic-gate    {
402*0Sstevel@tonic-gate	use utf8;
403*0Sstevel@tonic-gate	$a = 'España';
404*0Sstevel@tonic-gate	$a =~ s/ñ/ñ/;
405*0Sstevel@tonic-gate	like($a, qr/ñ/, "use utf8 LHS and RHS");
406*0Sstevel@tonic-gate    }
407*0Sstevel@tonic-gate}
408*0Sstevel@tonic-gate
409*0Sstevel@tonic-gate{
410*0Sstevel@tonic-gate    # SADAHIRO Tomoyuki <bqw10602@nifty.com>
411*0Sstevel@tonic-gate
412*0Sstevel@tonic-gate    $a = "\x{100}\x{101}";
413*0Sstevel@tonic-gate    $a =~ s/\x{101}/\xFF/;
414*0Sstevel@tonic-gate    like($a, qr/\xFF/);
415*0Sstevel@tonic-gate    is(length($a), 2, "SADAHIRO utf8 s///");
416*0Sstevel@tonic-gate
417*0Sstevel@tonic-gate    $a = "\x{100}\x{101}";
418*0Sstevel@tonic-gate    $a =~ s/\x{101}/"\xFF"/e;
419*0Sstevel@tonic-gate    like($a, qr/\xFF/);
420*0Sstevel@tonic-gate    is(length($a), 2);
421*0Sstevel@tonic-gate
422*0Sstevel@tonic-gate    $a = "\x{100}\x{101}";
423*0Sstevel@tonic-gate    $a =~ s/\x{101}/\xFF\xFF\xFF/;
424*0Sstevel@tonic-gate    like($a, qr/\xFF\xFF\xFF/);
425*0Sstevel@tonic-gate    is(length($a), 4);
426*0Sstevel@tonic-gate
427*0Sstevel@tonic-gate    $a = "\x{100}\x{101}";
428*0Sstevel@tonic-gate    $a =~ s/\x{101}/"\xFF\xFF\xFF"/e;
429*0Sstevel@tonic-gate    like($a, qr/\xFF\xFF\xFF/);
430*0Sstevel@tonic-gate    is(length($a), 4);
431*0Sstevel@tonic-gate
432*0Sstevel@tonic-gate    $a = "\xFF\x{101}";
433*0Sstevel@tonic-gate    $a =~ s/\xFF/\x{100}/;
434*0Sstevel@tonic-gate    like($a, qr/\x{100}/);
435*0Sstevel@tonic-gate    is(length($a), 2);
436*0Sstevel@tonic-gate
437*0Sstevel@tonic-gate    $a = "\xFF\x{101}";
438*0Sstevel@tonic-gate    $a =~ s/\xFF/"\x{100}"/e;
439*0Sstevel@tonic-gate    like($a, qr/\x{100}/);
440*0Sstevel@tonic-gate    is(length($a), 2);
441*0Sstevel@tonic-gate
442*0Sstevel@tonic-gate    $a = "\xFF";
443*0Sstevel@tonic-gate    $a =~ s/\xFF/\x{100}/;
444*0Sstevel@tonic-gate    like($a, qr/\x{100}/);
445*0Sstevel@tonic-gate    is(length($a), 1);
446*0Sstevel@tonic-gate
447*0Sstevel@tonic-gate    $a = "\xFF";
448*0Sstevel@tonic-gate    $a =~ s/\xFF/"\x{100}"/e;
449*0Sstevel@tonic-gate    like($a, qr/\x{100}/);
450*0Sstevel@tonic-gate    is(length($a), 1);
451*0Sstevel@tonic-gate}
452*0Sstevel@tonic-gate
453*0Sstevel@tonic-gate{
454*0Sstevel@tonic-gate    # subst with mixed utf8/non-utf8 type
455*0Sstevel@tonic-gate    my($ua, $ub, $uc, $ud) = ("\x{101}", "\x{102}", "\x{103}", "\x{104}");
456*0Sstevel@tonic-gate    my($na, $nb) = ("\x{ff}", "\x{fe}");
457*0Sstevel@tonic-gate    my $a = "$ua--$ub";
458*0Sstevel@tonic-gate    my $b;
459*0Sstevel@tonic-gate    ($b = $a) =~ s/--/$na/;
460*0Sstevel@tonic-gate    is($b, "$ua$na$ub", "s///: replace non-utf8 into utf8");
461*0Sstevel@tonic-gate    ($b = $a) =~ s/--/--$na--/;
462*0Sstevel@tonic-gate    is($b, "$ua--$na--$ub", "s///: replace long non-utf8 into utf8");
463*0Sstevel@tonic-gate    ($b = $a) =~ s/--/$uc/;
464*0Sstevel@tonic-gate    is($b, "$ua$uc$ub", "s///: replace utf8 into utf8");
465*0Sstevel@tonic-gate    ($b = $a) =~ s/--/--$uc--/;
466*0Sstevel@tonic-gate    is($b, "$ua--$uc--$ub", "s///: replace long utf8 into utf8");
467*0Sstevel@tonic-gate    $a = "$na--$nb";
468*0Sstevel@tonic-gate    ($b = $a) =~ s/--/$ua/;
469*0Sstevel@tonic-gate    is($b, "$na$ua$nb", "s///: replace utf8 into non-utf8");
470*0Sstevel@tonic-gate    ($b = $a) =~ s/--/--$ua--/;
471*0Sstevel@tonic-gate    is($b, "$na--$ua--$nb", "s///: replace long utf8 into non-utf8");
472*0Sstevel@tonic-gate
473*0Sstevel@tonic-gate    # now with utf8 pattern
474*0Sstevel@tonic-gate    $a = "$ua--$ub";
475*0Sstevel@tonic-gate    ($b = $a) =~ s/-($ud)?-/$na/;
476*0Sstevel@tonic-gate    is($b, "$ua$na$ub", "s///: replace non-utf8 into utf8 (utf8 pattern)");
477*0Sstevel@tonic-gate    ($b = $a) =~ s/-($ud)?-/--$na--/;
478*0Sstevel@tonic-gate    is($b, "$ua--$na--$ub", "s///: replace long non-utf8 into utf8 (utf8 pattern)");
479*0Sstevel@tonic-gate    ($b = $a) =~ s/-($ud)?-/$uc/;
480*0Sstevel@tonic-gate    is($b, "$ua$uc$ub", "s///: replace utf8 into utf8 (utf8 pattern)");
481*0Sstevel@tonic-gate    ($b = $a) =~ s/-($ud)?-/--$uc--/;
482*0Sstevel@tonic-gate    is($b, "$ua--$uc--$ub", "s///: replace long utf8 into utf8 (utf8 pattern)");
483*0Sstevel@tonic-gate    $a = "$na--$nb";
484*0Sstevel@tonic-gate    ($b = $a) =~ s/-($ud)?-/$ua/;
485*0Sstevel@tonic-gate    is($b, "$na$ua$nb", "s///: replace utf8 into non-utf8 (utf8 pattern)");
486*0Sstevel@tonic-gate    ($b = $a) =~ s/-($ud)?-/--$ua--/;
487*0Sstevel@tonic-gate    is($b, "$na--$ua--$nb", "s///: replace long utf8 into non-utf8 (utf8 pattern)");
488*0Sstevel@tonic-gate    ($b = $a) =~ s/-($ud)?-/$na/;
489*0Sstevel@tonic-gate    is($b, "$na$na$nb", "s///: replace non-utf8 into non-utf8 (utf8 pattern)");
490*0Sstevel@tonic-gate    ($b = $a) =~ s/-($ud)?-/--$na--/;
491*0Sstevel@tonic-gate    is($b, "$na--$na--$nb", "s///: replace long non-utf8 into non-utf8 (utf8 pattern)");
492*0Sstevel@tonic-gate}
493*0Sstevel@tonic-gate
494*0Sstevel@tonic-gate$_ = 'aaaa';
495*0Sstevel@tonic-gate$r = 'x';
496*0Sstevel@tonic-gate$s = s/a(?{})/$r/g;
497*0Sstevel@tonic-gateis("<$_> <$s>", "<xxxx> <4>", "[perl #7806]");
498*0Sstevel@tonic-gate
499*0Sstevel@tonic-gate$_ = 'aaaa';
500*0Sstevel@tonic-gate$s = s/a(?{})//g;
501*0Sstevel@tonic-gateis("<$_> <$s>", "<> <4>", "[perl #7806]");
502*0Sstevel@tonic-gate
503*0Sstevel@tonic-gate# [perl #19048] Coredump in silly replacement
504*0Sstevel@tonic-gate{
505*0Sstevel@tonic-gate    local $^W = 0;
506*0Sstevel@tonic-gate    $_="abcdef\n";
507*0Sstevel@tonic-gate    s!.!!eg;
508*0Sstevel@tonic-gate    is($_, "\n", "[perl #19048]");
509*0Sstevel@tonic-gate}
510*0Sstevel@tonic-gate
511*0Sstevel@tonic-gate# [perl #17757] interaction between saw_ampersand and study
512*0Sstevel@tonic-gate{
513*0Sstevel@tonic-gate    my $f = eval q{ $& };
514*0Sstevel@tonic-gate    $f = "xx";
515*0Sstevel@tonic-gate    study $f;
516*0Sstevel@tonic-gate    $f =~ s/x/y/g;
517*0Sstevel@tonic-gate    is($f, "yy", "[perl #17757]");
518*0Sstevel@tonic-gate}
519*0Sstevel@tonic-gate
520*0Sstevel@tonic-gate# [perl #20684] returned a zero count
521*0Sstevel@tonic-gate$_ = "1111";
522*0Sstevel@tonic-gateis(s/(??{1})/2/eg, 4, '#20684 s/// with (??{..}) inside');
523*0Sstevel@tonic-gate
524*0Sstevel@tonic-gate# [perl #20682] @- not visible in replacement
525*0Sstevel@tonic-gate$_ = "123";
526*0Sstevel@tonic-gate/(2)/;	# seed @- with something else
527*0Sstevel@tonic-gates/(1)(2)(3)/$#- (@-)/;
528*0Sstevel@tonic-gateis($_, "3 (0 0 1 2)", '#20682 @- not visible in replacement');
529*0Sstevel@tonic-gate
530*0Sstevel@tonic-gate# [perl #20682] $^N not visible in replacement
531*0Sstevel@tonic-gate$_ = "abc";
532*0Sstevel@tonic-gate/(a)/; s/(b)|(c)/-$^N/g;
533*0Sstevel@tonic-gateis($_,'a-b-c','#20682 $^N not visible in replacement');
534*0Sstevel@tonic-gate
535*0Sstevel@tonic-gate# [perl #22351] perl bug with 'e' substitution modifier
536*0Sstevel@tonic-gatemy $name = "chris";
537*0Sstevel@tonic-gate{
538*0Sstevel@tonic-gate    no warnings 'uninitialized';
539*0Sstevel@tonic-gate    $name =~ s/hr//e;
540*0Sstevel@tonic-gate}
541*0Sstevel@tonic-gateis($name, "cis", q[#22351 bug with 'e' substitution modifier]);
542