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


In reply to Re: How to replace \t with \s by Don Coyote
in thread How to replace \t with \s by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.