【问题标题】:Encoding: use 'utf8' [duplicate]编码:使用'utf8' [重复]
【发布时间】:2011-07-11 19:27:55
【问题描述】:

如果我已经在使用use encoding 'utf8',我可以省略use '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} );
}

【问题讨论】:

标签: perl utf-8 character-encoding encode


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    use encoding 官方不鼓励使用。 该模块已被弃用,因为它会导致非常奇怪的行为。相反,您应该使用以下内容:

    use utf8;                             # Source code is UTF-8
    use open ':std', ':encoding(UTF-8)';  # STDIN,STDOUT,STDERR are UTF-8.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-05
      • 1970-01-01
      • 2017-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多