Created
April 30, 2016 13:20
-
-
Save nd3i/484520fdf9543a044350818bf7fc5ef6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub escape ($str) { | |
# Puts a slash before non-alphanumeric characters | |
S:g[<-alpha -digit>] = "\\$/" given $str | |
} | |
say escape 'foo#bar?'; # foo\#bar\? | |
{ | |
sub escape ($str) { | |
# Writes each non-alphanumeric character in its hexadecimal escape | |
S:g[<-alpha -digit>] = "\\x[{ $/.ord.base(16) }]" given $str | |
} | |
say escape 'foo#bar?' # foo\x[23]bar\x[3F] | |
} | |
# Back to original escape function | |
say escape 'foo#bar?'; # foo\#bar\? | |
# OUTPUT: | |
# foo\#bar\? | |
# foo\x[23]bar\x[3F] | |
# foo\#bar\? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment