FAQ 9.10 How do I decode or create those %-encodings on the web?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
This is an excerpt from the latest version perlfaq9.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .


9.10: How do I decode or create those %-encodings on the web?

    If you are writing a CGI script, you should be using the CGI.pm module
    that comes with perl, or some other equivalent module. The CGI module
    automatically decodes queries for you, and provides an escape() function
    to handle encoding.

    The best source of detailed information on URI encoding is RFC 2396.
    Basically, the following substitutions do it:

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

        s/%([A-Fa-f\d])/chr hex $1/eg;                # decode
            s/%([[:xdigit:]])/chr hex $1/eg;          # same thing

    However, you should only apply them to individual URI components, not
    the entire URI, otherwise you'll lose information and generally mess
    things up. If that didn't explain it, don't worry. Just go read section
    2 of the RFC, it's probably the best explanation there is.

    RFC 2396 also contains a lot of other useful information, including a
    regexp for breaking any arbitrary URI into components (Appendix B).


The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in

Re: FAQ 9.10 How do I decode or create those %-encodings on the web?

On Mon, 14 Apr 2008 18:03:02 -0700, PerlFAQ Server wrote:

Quoted text here. Click to load it

This only deals with CGI scripts, but one might need to create a URI
encoding even when one is not writing a CGI script. Perhaps this FAQ
should mention the URI::Escape module too.


Re: FAQ 9.10 How do I decode or create those %-encodings on the web?

Quoted text here. Click to load it

Good idea. I'll add that to my to do list.

Thanks, :)

Site Timeline