编码:使用'utf8' - 问题

时间:2011-03-09 10:54:14

标签: perl utf-8 character-encoding encode

当我使用use 'utf8'时,我可以省略use encoding 'utf8' - pragma吗?

#!/usr/bin/env perl
use warnings;
use 5.012;
use Encode qw(is_utf8);

use encoding 'utf8';


my %hash = ( '☺' => "☺", '\x{263a}' => "\x{263a}", 'ä' => "ä", 'a' => "a" );
for my $key ( sort keys %hash ) {
    say "UTF8 flag is turned on in the STRING $key" if is_utf8( $hash{$key} );
    say "UTF8 flag is NOT turned on in the STRING $key" if not is_utf8( $hash{$key} );
}

2 个答案:

答案 0 :(得分:2)

是的,但请确保您熟悉perldoc中的CAVEATS

答案 1 :(得分:2)

我建议您停止使用encoding,而不是utf8。前者导致非常奇怪的行为。

use utf8;                  # Source code is UTF-8
use open ':std', ':utf8';  # STDIN,STOUT,STERR is UTF-8.