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?
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\'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With