Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl: tr/// is not doing what I expect whereas s/// is

Tags:

People also ask

What is the use of TR () function in Perl?

The tr operator in Perl translates all characters of SearchList into the corresponding characters of ReplacementList. Here the SearchList is the given input characters which are to be converted into the corresponding characters given in the ReplacementList.

What does ~~ mean in Perl?

It is the smartmatch operator. In general, when you want information about operators in Perl, see perldoc perlop.

How do I match a pattern in Perl?

m operator in Perl is used to match a pattern within the given text. The string passed to m operator can be enclosed within any character which will be used as a delimiter to regular expressions.

What is $@ in Perl?

$@ The Perl syntax error or routine error message from the last eval, do-FILE, or require command. If set, either the compilation failed, or the die function was executed within the code of the eval.


I want to remove diacritic signs in some strings. tr/// should do the job but fails (see below). I thought I had an encoding/decoding problem, but I noticed s/// works as I expect. Could somebody explain why?

Here is an example of results I get:

my $str1 = 'èîü';
my $str2 = $str1;
$str1 =~ tr/î/i/;
print "$str1\n"; # => i�iii�
$str2 =~ s/î/i/;
print "$str2\n"; # => èiü

Note that tr/// also modified the first and third characters of the string, not just the middle one.

Edit: I use Ubuntu 16.04 with Mate desktop environment.