Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translating single quote using trans

I can't find a way to translate using trans a single quote to a escaped single quote:

say ($ = "'well done'").=trans("'" => "\\\'" ) ;# OUTPUT: «\well done\␤»
say ($ = "'well done'").=trans(<'> => Q [\'] ) ;# OUTPUT: «\well done\␤»
say ($ = "'well done'").=trans("'" => q"\\\'" );# OUTPUT: «\well done\␤»

There's probably a workaround using split or any number of other things, including subst. In principle, the first one actually produces \', which is what I've been looking for. Maybe doubling up the scapes will help?

like image 636
jjmerelo Avatar asked Oct 06 '18 18:10

jjmerelo


1 Answers

I guess this is a gotcha with trans, but you actually need to specify a "from" list and a "to" list, otherwise it will just interpret the left side as a range of graphemes to be translated into the other range of graphemes:

say "'well done'".trans("abcde" => "vwxyz" );  # OUTPUT: 'wzll yonz'

If you create a list of strings to convert from one to the other, you get the desired result:

say "'well done'".trans(["'"] => ["\\'"] )  # OUTPUT: \'well done\'
like image 112
Elizabeth Mattijsen Avatar answered Oct 31 '22 15:10

Elizabeth Mattijsen