# Encode a string by replacing non-printable characters with \xXX; # escape char \ is also replaces sub encodex($) { local $_ = shift; s/(\\|[^ -~])/"\\x".uc unpack("H2",$1)/ge; return $_; }