Skip to content

Instantly share code, notes, and snippets.

@opendevnet
Forked from nd3i/functions_sub_example.p6
Created June 20, 2016 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save opendevnet/58d2806cbfa3a2a370c3c86a026b24f0 to your computer and use it in GitHub Desktop.
Save opendevnet/58d2806cbfa3a2a370c3c86a026b24f0 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