perl中的大写重音字符

时间:2012-11-07 00:23:00

标签: perl uppercase

有没有办法在perl中大写重音字符,

my $string = "éléphant";

print uc($string);

它实际上是打印ÉLÉPHANT?

我的perl脚本用ISO-8859-1编码,$ string用xml文件打印,编码相同。

2 个答案:

答案 0 :(得分:8)

perl只能理解US-ASCII和UTF-8,而后者需要

use utf8;

如果您想将文件保留为iso-8859-1,则需要明确解码文字。

use open ':std', ':encoding(locale)';
use Encode qw( decode );

# Source is encoded using iso-8859-1, so we need to decode ourselves.
my $string = decode("iso-8859-1", "éléphant");
print uc($string);

但将脚本转换为UTF-8可能更好。

use utf8;  # Source is encoded using UTF-8
use open ':std', ':encoding(locale)';

my $string = "éléphant";
print uc($string);

如果要打印到文件,请确保在打开文件时使用:encoding(iso-8859-1)(无论您使用哪种替代方案)。

答案 1 :(得分:3)

尝试这样做:

use Encode qw/encode decode/;

my $enc = 'utf-8'; # This script is stored as UTF-8
my $str = "éléphant\n";

my $text_str = decode($enc, $str);
$text_str = uc $text_str;
print encode($enc, $text_str);

输出

ÉLÉPHANT