perl对于utf8编码文件的substr

时间:2023-01-05 08:56:07

perl脚本本省使用utf8编码,环境默认编码为utf8

1.加上use utf8;后,print substr("哈哈",0,1),"\n";就正常输出了

2.但是,读入utf8编码的文件在做substr还是有问题,加上 FH,"<:utf8"就好了

Wide character in print 的警告信息,加上3.但是,会有

binmode(STDIN, ':encoding(utf8)');
binmode(STDOUT, ':encoding(utf8)');
binmode(STDERR, ':encoding(utf8)');

就好了


#!/usr/bin/perl
use strict;
use warnings;
use utf8;
binmode(STDIN, ':encoding(utf8)');
binmode(STDOUT, ':encoding(utf8)');
binmode(STDERR, ':encoding(utf8)');


print substr("哈哈",0,1),"\n";
open FH,"<:utf8", $file"||die "can't open file:$!";
my $i=0;
        while(<FH>){
        chomp $_;
        print $_,"\n";
        print substr($_,0,1),"\n";
        }
        close FH;