Friday, December 01, 2006

urlencode and urldecode in Perl

If you are looking for PHP's urlencode/urldecode equivalent in Perl, you won't find any. But here's what you can do:

Encode:
$str =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
Decode:
$str =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;

from: http://melecio.org/node/76

Another choice:

use URI::Escape;
uri_escape($val);
uri_unescape($val);

No comments: