tr replaces a list of characters with another list of characters, so without modifiers this will not do as you think, it will only replace \t with the first incident of the replacement list.
my $word = q{thing};
$word =~ tr/i/oooo/;
__END__
thong
Use the substitution operator s///. This will replace a match with a replacement string.
my $word = q{Perl is a thing I like to do.};
$word =~ s/i/oooo/;
__END__
Perl oooos a thing I like to do.
Or globally on the line.
$word =~ s/i/oooo/g;
__END__
Perl oooos a thoooong I looooke to do.
Splitting on a repeated character singularly will produce multiple empty lists, that once replaced will essentially reconstruct the line as it was processed. Which is fine if that is what you want.
my @text = split /o/, $word;
print join qq{o},@text;
__END__
Perl oooos a thoooong I looooke to do.
This can be seen more easily joining with a linefeed character\n.
my @text = split /o/, $word;
print join qq{/n},@text;
__END__
Perl
s a th
ng I l
ke t
d
.
Also note, join uses a literal string as a first argument. So you are not able to use a match escape for general whitespace to replace a literal character escape for a particular whitespace character.
\t is a tab whitespace escape, that interpolates in double-quoted context.
\s is a matching character escape that matches against any whitespace literal.
\s would not interpolate inside double-quoted context as how would perl know to which whitespace literal you wanted to replace with?
Whereas split also has the option to use the match m// operator in place of a literal string as its first argument. Moreover, literal ' ' space string to split alters splits behaviour in regard to white space parsing.
Your goal is not clear about if that is your attempt to solve the problem or if that is something you are preparing the document for so that you can then perform. As such, depending on what it is you want to do you may be able to make use of a capturing split, or splitting directly on the tabs instead of spaces on the first pass.
my $line = q{Perl is a thing I like to do.};
print split /i/, $line;
print split /(i)/, $line;
__END__
Perl s a thng I lke to do.
Perl is a thing I like to do.
print join q{o}, split /i/, $line;
print join q{o}, split /(i)/, $line;
__END__
Perl os a thong I loke to do.
Perl oios a thoiong I loioke to do.
And splitting on only incidents of only two or more consecutive characters.
my $line = q{Perl is a thiiing I like to do.};
print join q{ooo}, split /i{2,}/, $line;
__END__
Perl is a thooong I like to do.
This also has the advantage of not actually mangling your documents in place. There could be loose tab characters anywhere in all those files, so set up some small test lines to ensure the operator behaves a expected, and make backups. As per usual.
edit: Clarified splits usage of string or match operator as first argument, and that \s does not interpolate.
Dooon Coooyoooteee |