Skip to content

Instantly share code, notes, and snippets.

@nd3i
Created April 30, 2016 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nd3i/484520fdf9543a044350818bf7fc5ef6 to your computer and use it in GitHub Desktop.
Save nd3i/484520fdf9543a044350818bf7fc5ef6 to your computer and use it in GitHub Desktop.
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