Regex: URI & UTF8 en/decoding with Perl

Regex for URI encoding (RFC 2396)

# encode
s/([^\w()'*~!.-])/sprintf '%%%02x', ord $1/eg;

# decode
s/%([A-Fa-f\d]{2})/chr hex $1/eg;

Regex for UTF8 encoding

# Latin-1 to UTF8
s/([\x80-\xFF])/chr(0xC0|ord($1)>>6).chr(0x80|ord($1)&0x3F)/eg;

# UTF8 to Latin
s/([\xC0-\xDF])([\x80-\xBF])/chr(ord($1)<<6&0xC0|ord($2)&0x3F)/eg;