Search code examples
perlurlencodehtml-encode

Is it possible to print 'é' as '%C3%A9' in Perl?


I have some string with accent like "é" and the goal is to put my string into an URL so I need to convert "é" to "%C3%A9"

I have tested some module as HTML::Entitie, Encode or URI::Encode without any success

Actual Result: %C3%83%C2%A9

Expected Result: %C3%A9

#!/usr/bin/perl
use strict;
use warnings;
use HTML::Entities;
use feature 'say';
use URI::Encode qw( uri_encode );

my $var = "é";
say $var;

$var = uri_encode( $var );

say $var;

Solution

  • You are missing use utf8.

    The use utf8 pragma tells the Perl parser to allow UTF-8 in the program text in the current lexical scope. The no utf8 pragma tells Perl to switch back to treating the source text as literal bytes in the current lexical scope. (On EBCDIC platforms, technically it is allowing UTF-EBCDIC, and not UTF-8, but this distinction is academic, so in this document the term UTF-8 is used to mean both).

    Do not use this pragma for anything else than telling Perl that your script is written in UTF-8. The utility functions described below are directly usable without use utf8;.