subst.t revision 7c478bd95313f5f23a4c958a745db2134aa03244
#!./perl
BEGIN {
chdir 't' if -d 't';
}
print "1..84\n";
$x = 'foo';
$_ = "x";
s/x/\$x/;
print "#1\t:$_: eq :\$x:\n";
$_ = "x";
s/x/$x/;
print "#2\t:$_: eq :foo:\n";
$_ = "x";
s/x/\$x $x/;
print "#3\t:$_: eq :\$x foo:\n";
$b = 'cd';
($a = 'abcdef') =~ s<(b${b}e)>'\n$1';
print "#4\t:$1: eq :bcde:\n";
print "#4\t:$a: eq :a\\n\$1f:\n";
$a = 'abacada';
{print "ok 5\n";} else {print "not ok 5\n";}
{print "ok 6\n";} else {print "not ok 6 $a\n";}
{print "ok 7\n";} else {print "not ok 7 $a\n";}
$_ = 'ABACADA';
$_ = '\\' x 4;
if (length($_) == 4) {print "ok 9\n";} else {print "not ok 9\n";}
s/\\/\\\\/g;
if ($_ eq '\\' x 8) {print "ok 10\n";} else {print "not ok 10 $_\n";}
$_ = '\/' x 4;
if (length($_) == 8) {print "ok 11\n";} else {print "not ok 11\n";}
s/\//\/\//g;
if ($_ eq '\\//' x 4) {print "ok 12\n";} else {print "not ok 12\n";}
if (length($_) == 12) {print "ok 13\n";} else {print "not ok 13\n";}
s/^a//;
s/a//;
s/^a/b/;
s/a/b/;
s/b$//;
s/b//;
# now for some unoptimized versions of the same.
$x ne $x || s/^a//;
$x ne $x || s/a//;
$x ne $x || s/^a/b/;
$x ne $x || s/a/b/;
$x ne $x || s/b$//;
$x ne $x || s/b//;
$_ = "aaaaa";
print y/a/b/ == 5 ? "ok 43\n" : "not ok 43\n";
print y/a/b/ == 0 ? "ok 44\n" : "not ok 44\n";
print y/b// == 5 ? "ok 45\n" : "not ok 45\n";
print y/b/c/s == 5 ? "ok 46\n" : "not ok 46\n";
print y/c// == 1 ? "ok 47\n" : "not ok 47\n";
print y/c//d == 1 ? "ok 48\n" : "not ok 48\n";
print $_ eq "" ? "ok 49\n" : "not ok 49\n";
$_ = "Now is the %#*! time for all good men...";
print y/ / /s == 8 ? "ok 51\n" : "not ok 51\n";
no utf8;
y[\301-\351][\201-\251];
} else { # Ye Olde ASCII. Or something like it.
y[\101-\132][\141-\172];
}
if (ord("+") == ord(",") - 1 && ord(",") == ord("-") - 1 &&
ord("a") == ord("b") - 1 && ord("b") == ord("c") - 1) {
$_ = '+,-';
tr/+--/a-c/;
}
print "ok 54\n";
$_ = '+,-';
tr/+\--/a\/c/;
print $_ eq 'a,/' ? "ok 55\n" : "not ok 55\n";
$_ = '+,-';
tr/-+,/ab\-/;
print $_ eq 'b-a' ? "ok 56\n" : "not ok 56\n";
# test recursive substitutions
# code based on the recursive expansion of makefile variables
my %MK = (
AAAAA => '$(B)', B=>'$(C)', C => 'D', # long->short
);
sub var {
my($var,$level) = @_;
return "\$($var)" unless exists $MK{$var};
return exp_vars($MK{$var}, $level+1); # can recurse
}
sub exp_vars {
my($str,$level) = @_;
$str =~ s/\$\((\w+)\)/var($1, $level+1)/ge; # can recurse
$str;
}
? "ok 57\n" : "not ok 57\n";
? "ok 58\n" : "not ok 58\n";
? "ok 59\n" : "not ok 59\n";
? "ok 60\n" : "not ok 60\n";
# a match nested in the RHS of a substitution:
$_ = "abcd";
s/(..)/$x = $1, m#.#/eg;
print $x eq "cd" ? "ok 61\n" : "not ok 61\n";
# Subst and lookbehind
$_="ccccc";
s/(?<!x)c/x/g;
$_="ccccc";
s/(?<!x)(c)/x/g;
$_="foobbarfoobbar";
$_="foobbarfoobbar";
s/(?<!ar)(foobbar)/foobar/g;
$_="foobbarfoobbar";
# check parsing of split subst with comment
{bar};';
print @? ? "not ok 67\n" : "ok 67\n";
# check if squashing works at the end of string
$_="baacbaa";
tr/a/b/s;
$_ = "ab";
print (s/a/b/ == 1 ? "ok 69\n" : "not ok 69\n");
$_ = <<'EOL';
$^R = 'junk';
$foo = ' $@%#lowercase $@%# lowercase UPPERCASE$@%#UPPERCASE' .
' $@%#lowercase$@%#lowercase$@%# lowercase lowercase $@%#lowercase' .
' lowercase $@%#MiXeD$@%# ';
s{ \d+ \b [,.;]? (?{ 'digits' })
|
[a-z]+ \b [,.;]? (?{ 'lowercase' })
|
[A-Z]+ \b [,.;]? (?{ 'UPPERCASE' })
|
[A-Z] [a-z]+ \b [,.;]? (?{ 'Capitalized' })
|
|
|
\s+ (?{ ' ' })
|
}{$^R}xg;
$_ = 'x' x 20;
s/(\d*|x)/<$1>/g;
$t = 'aaaaaaaaa';
$_ = $t;
pos = 6;
print "ok 72\n";
$_ = $t;
pos = 6;
s/\Ga/x/g;
print "ok 73\n";
$_ = $t;
pos = 6;
print "ok 74\n";
$_ = $t;
pos = 6;
s/\Ga/x/;
print "ok 75\n";
$_ = $t;
print "ok 76\n";
$_ = $t;
s/\Ga/x/g;
print "ok 77\n";
$_ = $t;
print "ok 78\n";
$_ = $t;
s/\Ga/x/;
print "ok 79\n";
$_ = 'aaaa';
s/\ba/./g;
print "ok 80\n";
eval q% s/a/"b"}/e %;
eval q% ($_ = "x") =~ s/(.)/"$1 "/e %;
$x = $x = 'interp';
eval q% ($_ = "x") =~ s/x(($x)*)/"$1"/e %;
$_ = "C:/";
print "ok 84\n";