编码:使用'utf8'

sid*_*com 4 perl encode utf-8 character-encoding

use 'utf8'当我已经在使用时,我可以省略-pragmause encoding 'utf8'吗?

#!/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} );
}
Run Code Online (Sandbox Code Playgroud)

ike*_*ami 5

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

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