是否可以在 Perl 中将 'é' 打印为 '%C3%A9'?

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

我有一些带重音符号的字符串,例如“é”,目标是将我的字符串放入 URL 中,因此我需要将“é”转换为“%C3%A9”

我测试了一些模块 HTML::EntitieEncodeURI::Encode 但没有成功

实际结果: %C3%83%C2%A9

预期结果: %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;

你错过了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;.