in reply to Re^3: javascript encodeURI() -> perl uri_unescape_utf8() ?
in thread javascript encodeURI() -> perl uri_unescape_utf8() ?

Thanks for your suggestion.
This does not solve my problem, unfortunately. I have found that the decode function in the Encode module should be able to do the job.
use Encode; print decode('utf8', "\xC3\xA5");
This prints the character I need.
The problem now is:
How to go from %C3%A5 etc. to \xC3\xA5?
I tried
use Encode; $_='%C3%A5'; s/%/\\x/g; eval { $_=$_}; print decode('utf8', $_);
but it does not work as expected. :-)

This was actually solved in the previous post by graff.

Replies are listed 'Best First'.
Re^5: javascript encodeURI() -> perl uri_unescape_utf8() ?
by graff (Chancellor) on Dec 15, 2004 at 15:32 UTC
    The problem now is: How to go from %C3%A5 etc. to \xC3\xA5?

    That's exactly what this line of code does:

    s/\%([0-9A-Z]{2})/chr(hex($1))/egi;
    Did you try that? The left side matches any "%HH" pattern, and captures the two hex digits into $1; in the right side, "hex()" interprets $1 as a hex number, and "chr()" turns that value into a single-byte character. (Or it should, unless maybe you've put a "use utf8" somewhere above this line -- if that's the problem, put this regex into its own block with "use bytes":
    { use bytes; s/\%([0-9A-Z]{2})/chr(hex($1))/egi; }

    As for your eval attempt, I think the first "$_" in "$_=$_" needs to have a backslash in front of the dollar-sign: "\$_ = $_". In any case, I would advise against changing every percent-sign into "\x" -- there might be some cases of "%" that are not followed by a pair of hex digits.

    (update: Actually, in the context of dealing with uri strings, that last point is moot -- it surely must be the case in uri encoding that every "%" character is part of a "%HH" expression, and "%25" is used to express a literal percent-sign in the data. But the point is relevant for any data that might be a "defective" combination of uri encoding and plain text.)