FAQ 9.10 How do I decode or create those %-encodings on the web?
am 15.04.2008 03:03:02 von PerlFAQ ServerThis 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]{2})/chr hex $1/eg; # decode
s/%([[:xdigit:]]{2})/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
perlfaq.pod.