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.)


In reply to Re^5: javascript encodeURI() -> perl uri_unescape_utf8() ? by graff
in thread javascript encodeURI() -> perl uri_unescape_utf8() ? by nkropols

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.