"0123" ); # "3200212\n" ...">
📜 ⬆️ ⬇️

Transliteration in Perl6

"Transliteration" means replacing characters. This is exactly what Str.trans method does.

say "GATTACA".trans( "TCAG" => "0123" ); #  "3200212\n" 


People familiar with Perl 5 or with the Unix shell recognize tr / tcag / 0123 / in this, and for the rest, we explain: each letter T is replaced by 0, each C by 1, and so on. The two lines, TCAG and 0123, provide alphabets that must be interchanged.
')
This can be used for speed when implementing various operations. For example, a simple function that “encrypts” the text with the ROT-13 method (replacing a character with what stands after 13 positions from it):

 sub rot13($text) { $text.trans( "A..Za..z" => "N..ZA..Mn..za..m" ) } 


.trans expands the ranges ..., so "n..z" would mean "nopqrstuvwxyz". Thus, the function rot13 will replace specific parts of the ASCII alphabet with other parts.

In Perl 5, instead of two points ... a dash is used, but in Perl 6 we decided that two points better represent the concept of a “gap” - in language, in regulars and in transliteration.

The .trans method does not change $ text, but only returns a new value. This is what Perl 6 does — we offer methods without side effects. But if necessary, such behavior can be defined through. =:

 $kabbala.=trans("A..Ia..i" => "1..91..9"); 


This also works for all other methods. Programmers are still encouraged to write libraries that support the first method, and then our world will be brighter and better.

But it would not be Perl 6, do not contain .trans hidden force exceeding the simple tr ///. That's what he does.

Suppose we need to escape some characters for HTML, that is, replace them according to the list:

  & => & < => < > => > 


You can restrict regulars. But it may happen that at this step you are stuck in an infinite loop (& => & => & amp; => ...). But the problem is not even in this, but in the fact that you will be engaged in sewing together pieces of lines, instead of covering the entire task at a higher level. In addition, I would like the method to work like this:

   =>   =>  


If the first replacement is performed first, then the second share will have nothing left - and the result will be wrong. Usually we need to change longer lines first.

And this behavior gives us .trans in Perl 6. This is his hidden weapon - passing a pair of arrays, not just a pair of lines. To escape characters, you only need to specify:

 my $escaped = $html.trans( [ '&', '<', '>' ] => [ '&amp;', '&lt;', '&gt;' ] ); 


and all these non-trivial, but tedious tasks of replacing characters in the correct order without falling out into the perpetual cycle will be solved for us.

Source: https://habr.com/ru/post/257021/


All Articles