NAME
URI::Escape - Escape and unescape unsafe characters
SYNOPSIS
use URI::Escape;
$safe = uriescape("10% is enough\n");
$verysafe = uriescape("foo", "\0-\377");
$str = uriunescape($safe);
DESCRIPTION
This module provides functions to escape and unescape URI strings as defined by RFC 2396 (and updated by RFC 2732). A URI consists of a restricted set of characters, denoted as "uric" in RFC 2396. The restricted set of characters consists of digits, letters, and a few graphic symbols chosen from those common to most of the character encodings and input facilities available to Internet users: "A" .. "Z", "a" .. "z", "0" .. "9",";", "/", "?", ":", "@", "&", "=", "+", "$", ",", "[", "]", # reserved
"-", "", ".", "!", "~", "*", "'", "(", ")"
In addition, any byte (octet) can be represented in a URI by an escapesequence: a triplet consisting of the character "%" followed by two
hexadecimal digits. A byte can also be represented directly by acharacter, using the US-ASCII character for that octet (iff the
character is part of "uric"). Some of the "uric" characters are reserved for use as delimiters or as part of certain URI components. These must be escaped if they are to be treated as ordinary data. Read RFC 2396 for further details. The functions provided (and exported by default) from this module are:uriescape( $string )
uriescape( $string, $unsafe )
Replaces each unsafe character in the $string with the
corresponding escape sequence and returns the result. The $string
argument should be a string of bytes. The uriescape() function will croak if given a characters with code above 255. Use uriescapeutf8() if you know you have such chars or/and want charsin the 128 .. 255 range treated as UTF-8.
The uriescape() function takes an optional second argument that overrides the set of characters that are to be escaped. The set is specified as a string that can be used in a regular expression character class (between [ ]). E.g.:"\x00-\x1f\x7f-\xff" # all control and hi-bit characters
"a-z" # all lower case characters
"^A-Za-z" # everything not a letter
The default set of characters to be escaped is all those which are not part of the "uric" character class shown above as well as the reserved characters. I.e. the default is:"^A-Za-z0-9\-.!~*'()"
uriescapeutf8( $string )
uriescapeutf8( $string, $unsafe )
Works like uriescape(), but will encode chars as UTF-8 before
escaping them. This makes this function able do deal withcharacters with code above 255 in $string. Note that chars in the
128 .. 255 range will be escaped differently by this function compared to what uriescape() would. For chars in the 0 .. 127 range there is no difference. The call:$uri = uriescapeutf8($string);
will be the same as: use Encode qw(encode);$uri = uriescape(encode("UTF-8", $string));
but will even work for perl-5.6 for chars in the 128 .. 255 range.
Note: Javascript has a function called escape() that produce thesequence "%uXXXX" for chars in the 256 .. 65535 range. This
function has really nothing to do with URI escaping but some folks got confused since it "does the right thing" in the 0 .. 255 range. Because of this you sometimes see "URIs" with these kind of escapes. The JavaScript encodeURI() function is similar to uriescapeutf8().uriunescape($string,...)
Returns a string with each %XX sequence replaced with the actual
byte (octet). This does the same as:$string =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
but does not modify the string in-place as this RE would. Using
the uriunescape() function instead of the RE might make the code look cleaner and is a few characters less to type. In a simple benchmark test I did, calling the function (instead of the inline RE above) if a few chars were unescaped was somethinglike 40% slower, and something like 700% slower if none were. If
you are going to unescape a lot of times it might be a good idea to inline the RE. If the uriunescape() function is passed multiple strings, then each one is returned unescaped.The module can also export the %escapes hash, which contains the
mapping from all 256 bytes to the corresponding escape codes. Lookupin this hash is faster than evaluating "sprintf("%%%02X", ord($byte))"
each time.SEE ALSO
URI COPYRIGHTCopyright 1995-2004 Gisle Aas.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.perl v5.8.8 2004-11-05 URI::Escape(3)