6 require Config; import Config;
15 ok( $_ eq '$x', ":$_: eq :\$x:" );
19 ok( $_ eq 'foo', ":$_: eq :foo:" );
23 ok( $_ eq '$x foo', ":$_: eq :\$x foo:" );
26 ($a = 'abcdef') =~ s<(b${b}e)>'\n$1';
27 ok( $1 eq 'bcde' && $a eq 'a\n$1f', ":$1: eq :bcde: ; :$a: eq :a\\n\$1f:" );
30 ok( ($a =~ s/a/x/g) == 4 && $a eq 'xbxcxdx' );
32 ok( ($a =~ s/a/y/g) == 0 && $a eq 'xbxcxdx' );
34 ok( ($a =~ s/b/y/g) == 1 && $a eq 'xyxcxdx' );
37 ok( /a/i && s///gi && $_ eq 'BCD' );
40 ok( length($_) == 4 );
42 ok( $_ eq '\\' x 8 && $snum == 4 );
45 ok( length($_) == 8 );
47 ok( $_ eq '\\//' x 4 && $snum == 4 );
48 ok( length($_) == 12 );
52 ok( $_ eq 'aaXXXXbbb' );
56 ok( $_ eq 'aaXXXXbbb' );
60 ok( $_ eq 'baaXXXXbbb' );
64 ok( $_ eq 'baaXXXXbbb' );
68 ok( $_ eq 'aXXXXbbb' );
72 ok( $_ eq 'baXXXXbbb' );
76 ok( $_ eq 'aaaXXXXbb' );
80 ok( $_ eq 'aaaXXXXbb' );
84 ok( $_ eq 'aaaXXXXb' );
88 ok( $_ eq 'aayXXXbbb' );
92 ok( $_ eq 'aaaXXXzbb' );
100 ok( $_ eq 'aaaXXXXxb' );
102 # now for some unoptimized versions of the same.
106 ok( $_ eq 'aaXXXXbbb' );
110 ok( $_ eq 'aaXXXXbbb' );
114 ok( $_ eq 'baaXXXXbbb' );
118 ok( $_ eq 'baaXXXXbbb' );
122 ok( $_ eq 'aXXXXbbb' );
126 ok( $_ eq 'baXXXXbbb' );
130 ok( $_ eq 'aaaXXXXbb' );
134 ok( $_ eq 'aaaXXXXbb' );
138 ok( $_ eq 'aaaXXXXb' );
142 ok( $_ eq 'aayXXXbbb' );
146 ok( $_ eq 'aaaXXXzbb' );
149 $x ne $x || s/aaX.*Xbb//;
154 ok( $_ eq 'aaaXXXXxb' );
157 s/(\d+)/$1*2/e; # yields 'abc246xyz'
158 ok( $_ eq 'abc246xyz' );
159 s/(\d+)/sprintf("%5d",$1)/e; # yields 'abc 246xyz'
160 ok( $_ eq 'abc 246xyz' );
161 s/(\w)/$1 x 2/eg; # yields 'aabbcc 224466xxyyzz'
162 ok( $_ eq 'aabbcc 224466xxyyzz' );
173 $_ = "Now is the %#*! time for all good men...";
174 ok( ($x=(y/a-zA-Z //cd)) == 7 );
177 $_ = 'abcdefghijklmnopqrstuvwxyz0123456789';
180 ok( $_ eq 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' );
182 # same as tr/A-Z/a-z/;
183 if (defined $Config{ebcdic} && $Config{ebcdic} eq 'define') { # EBCDIC.
185 y[\301-\351][\201-\251];
186 } else { # Ye Olde ASCII. Or something like it.
187 y[\101-\132][\141-\172];
190 ok( $_ eq 'abcdefghijklmnopqrstuvwxyz0123456789' );
193 skip("not ASCII",1) unless (ord("+") == ord(",") - 1
194 && ord(",") == ord("-") - 1
195 && ord("a") == ord("b") - 1
196 && ord("b") == ord("c") - 1);
211 # test recursive substitutions
212 # code based on the recursive expansion of makefile variables
215 AAAAA => '$(B)', B=>'$(C)', C => 'D', # long->short
216 E => '$(F)', F=>'p $(G) q', G => 'HHHHH', # short->long
217 DIR => '$(UNDEFINEDNAME)/xxx',
220 my($var,$level) = @_;
221 return "\$($var)" unless exists $MK{$var};
222 return exp_vars($MK{$var}, $level+1); # can recurse
225 my($str,$level) = @_;
226 $str =~ s/\$\((\w+)\)/var($1, $level+1)/ge; # can recurse
227 #warn "exp_vars $level = '$str'\n";
231 ok( exp_vars('$(AAAAA)',0) eq 'D' );
232 ok( exp_vars('$(E)',0) eq 'p HHHHH q' );
233 ok( exp_vars('$(DIR)',0) eq '$(UNDEFINEDNAME)/xxx' );
234 ok( exp_vars('foo $(DIR)/yyy bar',0) eq 'foo $(UNDEFINEDNAME)/xxx/yyy bar' );
237 s/(..)/$x = $1, m#.#/eg;
238 ok( $x eq "cd", 'a match nested in the RHS of a substitution' );
240 # Subst and lookbehind
243 $snum = s/(?<!x)c/x/g;
244 ok( $_ eq "xxxxx" && $snum == 5 );
247 $snum = s/(?<!x)(c)/x/g;
248 ok( $_ eq "xxxxx" && $snum == 5 );
251 $snum = s/(?<!r)foobbar/foobar/g;
252 ok( $_ eq "foobarfoobbar" && $snum == 1 );
255 $snum = s/(?<!ar)(foobbar)/foobar/g;
256 ok( $_ eq "foobarfoobbar" && $snum == 1 );
259 $snum = s/(?<!ar)foobbar/foobar/g;
260 ok( $_ eq "foobarfoobbar" && $snum == 1 );
262 eval 's{foo} # this is a comment, not a delimiter
264 ok( ! @?, 'parsing of split subst with comment' );
268 ok( $_ eq "bbcbb" && $snum == 4,
269 'check if squashing works at the end of string' );
275 $url = new URI::URL "http://www/"; die if $url eq "xXx";
279 $foo = ' $@%#lowercase $@%# lowercase UPPERCASE$@%#UPPERCASE' .
280 ' $@%#lowercase$@%#lowercase$@%# lowercase lowercase $@%#lowercase' .
281 ' lowercase $@%#MiXeD$@%# ';
284 s{ \d+ \b [,.;]? (?{ 'digits' })
286 [a-z]+ \b [,.;]? (?{ 'lowercase' })
288 [A-Z]+ \b [,.;]? (?{ 'UPPERCASE' })
290 [A-Z] [a-z]+ \b [,.;]? (?{ 'Capitalized' })
292 [A-Za-z]+ \b [,.;]? (?{ 'MiXeD' })
294 [A-Za-z0-9]+ \b [,.;]? (?{ 'alphanumeric' })
298 [^A-Za-z0-9\s]+ (?{ '$@%#' })
305 ok( $_ eq '' && $snum == 6 );
308 $snum = s/(\d*|x)/<$1>/g;
309 $foo = '<>' . ('<x><>' x 20) ;
310 ok( $_ eq $foo && $snum == 41 );
317 ok( $_ eq 'aaaaaaxxxxxx' && $snum == 3 );
322 ok( $_ eq 'aaaaaaxxx' && $snum == 3 );
327 ok( $_ eq 'aaaaaaxxaa' );
332 ok( $_ eq 'aaaaaaxaa' );
336 ok( $_ eq 'xxxxxxxxxxxxxxxxxx' && $snum == 9 );
340 ok( $_ eq 'xxxxxxxxx' && $snum == 9 );
344 ok( $_ eq 'xxaaaaaaaa' );
348 ok( $_ eq 'xaaaaaaaa' );
352 ok( $_ eq '.aaa' && $snum == 1 );
354 eval q% s/a/"b"}/e %;
355 ok( $@ =~ /Bad evalled substitution/ );
356 eval q% ($_ = "x") =~ s/(.)/"$1 "/e %;
357 ok( $_ eq "x " and !length $@ );
359 eval q% ($_ = "x") =~ s/x(($x)*)/"$1"/e %;
360 ok( $_ eq '' and !length $@ );
363 ok( !s/^([a-z]:)/\u$1/ );
365 $_ = "Charles Bronson";
367 ok( $_ eq "C B" && $snum == 12 );
371 my $s = "H\303\266he";
375 is($l, $r, "use utf8 \\w");
378 my $pv1 = my $pv2 = "Andreas J. K\303\266nig";
379 $pv1 =~ s/A/\x{100}/;
380 substr($pv2,0,1) = "\x{100}";
384 skip("EBCDIC", 3) if ord("A") == 193;
387 # Gregor Chrupala <gregor.chrupala@star-group.net>
389 $a = 'España';
391 like($a, qr/ñ/, "use utf8 RHS");
396 $a = 'España España';
398 like($a, qr/ñ/, "use utf8 LHS");
405 like($a, qr/ñ/, "use utf8 LHS and RHS");
410 # SADAHIRO Tomoyuki <bqw10602@nifty.com>
412 $a = "\x{100}\x{101}";
413 $a =~ s/\x{101}/\xFF/;
415 is(length($a), 2, "SADAHIRO utf8 s///");
417 $a = "\x{100}\x{101}";
418 $a =~ s/\x{101}/"\xFF"/e;
422 $a = "\x{100}\x{101}";
423 $a =~ s/\x{101}/\xFF\xFF\xFF/;
424 like($a, qr/\xFF\xFF\xFF/);
427 $a = "\x{100}\x{101}";
428 $a =~ s/\x{101}/"\xFF\xFF\xFF"/e;
429 like($a, qr/\xFF\xFF\xFF/);
433 $a =~ s/\xFF/\x{100}/;
434 like($a, qr/\x{100}/);
438 $a =~ s/\xFF/"\x{100}"/e;
439 like($a, qr/\x{100}/);
443 $a =~ s/\xFF/\x{100}/;
444 like($a, qr/\x{100}/);
448 $a =~ s/\xFF/"\x{100}"/e;
449 like($a, qr/\x{100}/);
454 # subst with mixed utf8/non-utf8 type
455 my($ua, $ub, $uc, $ud) = ("\x{101}", "\x{102}", "\x{103}", "\x{104}");
456 my($na, $nb) = ("\x{ff}", "\x{fe}");
459 ($b = $a) =~ s/--/$na/;
460 is($b, "$ua$na$ub", "s///: replace non-utf8 into utf8");
461 ($b = $a) =~ s/--/--$na--/;
462 is($b, "$ua--$na--$ub", "s///: replace long non-utf8 into utf8");
463 ($b = $a) =~ s/--/$uc/;
464 is($b, "$ua$uc$ub", "s///: replace utf8 into utf8");
465 ($b = $a) =~ s/--/--$uc--/;
466 is($b, "$ua--$uc--$ub", "s///: replace long utf8 into utf8");
468 ($b = $a) =~ s/--/$ua/;
469 is($b, "$na$ua$nb", "s///: replace utf8 into non-utf8");
470 ($b = $a) =~ s/--/--$ua--/;
471 is($b, "$na--$ua--$nb", "s///: replace long utf8 into non-utf8");
473 # now with utf8 pattern
475 ($b = $a) =~ s/-($ud)?-/$na/;
476 is($b, "$ua$na$ub", "s///: replace non-utf8 into utf8 (utf8 pattern)");
477 ($b = $a) =~ s/-($ud)?-/--$na--/;
478 is($b, "$ua--$na--$ub", "s///: replace long non-utf8 into utf8 (utf8 pattern)");
479 ($b = $a) =~ s/-($ud)?-/$uc/;
480 is($b, "$ua$uc$ub", "s///: replace utf8 into utf8 (utf8 pattern)");
481 ($b = $a) =~ s/-($ud)?-/--$uc--/;
482 is($b, "$ua--$uc--$ub", "s///: replace long utf8 into utf8 (utf8 pattern)");
484 ($b = $a) =~ s/-($ud)?-/$ua/;
485 is($b, "$na$ua$nb", "s///: replace utf8 into non-utf8 (utf8 pattern)");
486 ($b = $a) =~ s/-($ud)?-/--$ua--/;
487 is($b, "$na--$ua--$nb", "s///: replace long utf8 into non-utf8 (utf8 pattern)");
488 ($b = $a) =~ s/-($ud)?-/$na/;
489 is($b, "$na$na$nb", "s///: replace non-utf8 into non-utf8 (utf8 pattern)");
490 ($b = $a) =~ s/-($ud)?-/--$na--/;
491 is($b, "$na--$na--$nb", "s///: replace long non-utf8 into non-utf8 (utf8 pattern)");
497 is("<$_> <$s>", "<xxxx> <4>", "[perl #7806]");
501 is("<$_> <$s>", "<> <4>", "[perl #7806]");
503 # [perl #19048] Coredump in silly replacement
508 is($_, "\n", "[perl #19048]");
511 # [perl #17757] interaction between saw_ampersand and study
513 my $f = eval q{ $& };
517 is($f, "yy", "[perl #17757]");
520 # [perl #20684] returned a zero count
522 is(s/(??{1})/2/eg, 4, '#20684 s/// with (??{..}) inside');
524 # [perl #20682] @- not visible in replacement
526 /(2)/; # seed @- with something else
527 s/(1)(2)(3)/$#- (@-)/;
528 is($_, "3 (0 0 1 2)", '#20682 @- not visible in replacement');
530 # [perl #20682] $^N not visible in replacement
532 /(a)/; s/(b)|(c)/-$^N/g;
533 is($_,'a-b-c','#20682 $^N not visible in replacement');
535 # [perl #22351] perl bug with 'e' substitution modifier
538 no warnings 'uninitialized';
541 is($name, "cis", q[#22351 bug with 'e' substitution modifier]);
544 # [perl #34171] $1 didn't honour 'use bytes' in s//e
552 is(length($x), 2, '[perl #34171]');
556 { # [perl #27940] perlbug: [\x00-\x1f] works, [\c@-\c_] does not
559 ($c = "\x20\c@\x30\cA\x40\cZ\x50\c_\x60") =~ s/[\c@-\c_]//g;
560 is($c, "\x20\x30\x40\x50\x60", "s/[\\c\@-\\c_]//g");
562 ($c = "\x20\x00\x30\x01\x40\x1A\x50\x1F\x60") =~ s/[\x00-\x1f]//g;
563 is($c, "\x20\x30\x40\x50\x60", "s/[\\x00-\\x1f]//g");
567 no warnings 'uninitialized';
568 /(((((((((x)))))))))(z)/; # clear $10
569 s/(((((((((x)))))))))(y)/${10}/;
570 is($_,"y","RT#6006: \$_ eq '$_'");
572 s/(((((((((x)))))))))(r)/fooba${10}/;
573 is($_,"foobar","RT#6006: \$_ eq '$_'");
576 my $want=("\n" x 11).("B\n" x 11)."B";
583 is($want,$_,"RT#17542");