#build a char->hex map for (0..255) { $escapes{chr($_)} = sprintf("%%%02X", $_); } $text =~ s/([^A-Za-z0-9\-_.!~*'()])/$escapes{$1}/g;