Search code examples
perldecodeencode

Decode UTF-8 URL in Perl


Consider:

use URI::Escape;
print uri_unescape("%C3%B3");

Output : ó

Decode with this http://meyerweb.com/eric/tools/dencoder/

Output : ó

This is the expected one.

What Perl library should I use to get the correct output?


Solution

  • If you know that the byte sequence is UTF-8, then use Encode::decode:

    use Encode;
    use URI::Escape;
    
    my $in = "%C3%B3";
    my $text = Encode::decode('utf8', uri_unescape($in));
    
    print length($text);    # Should print 1