Perl 6:音译中的反斜杠 (tr///)

Perl 6: Backslashes in transliteration (tr///)

我在试验 tr/// 时注意到它似乎不翻译反斜杠,即使在转义时也是如此。例如,

say TR"\^/v"." given 'v^/\';
say TR"\^/v"." given 'v^/\';
say TR"\ ^/v"." given 'v^/\';

全部output ...\ 而不是我的预期,....

还有一些其他奇怪的行为,比如\貌似only escaping lowercase letters, but the docs page没有太多信息...反斜杠(\)在音译中的行为到底是什么( tr///)?

我没有对观察到的问题的解释。但是,当您使用 Perl 6 Str.trans 方法时,它看起来像预期的那样工作:

say 'v^/\'.trans( "\^/v" => "." );

Outputs:

....

参考:

在 tr/// 的语法中,反斜杠被吞没而不是正确转义内容导致了错误。

say TR/\// given '\'
===SORRY!=== Error while compiling:
Malformed replacement part; couldn't find final /
at line 2
------> <BOL>⏏<EOL>

我提出了 https://github.com/rakudo/rakudo/issues/2456 and submitted https://github.com/rakudo/rakudo/pull/2457 来修复它。

答案的第二部分是 Perl 6 在某些引用结构中非常努力地只将 \ 解释为有效转义序列的转义,即 \n\r , \s, \', 等等。否则它被保留为文字 \.