Perl Wide character in print问题解决

本文介绍了一种在Perl中使用Text::CSV模块解析CSV文件时遇到的宽字符警告问题及解决方案。通过在程序开始处添加binmode STDOUT, ':utf8'语句,可以消除与UTF-8编码相关的警告。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用Text::CSV模块解析CSV文件时遇到问题;

在开始加入binmode STDOUT, ':utf8';语句,症状消失,整个使用CSV的代码如下:

#!/usr/bin/perl

 

use strict;

use Text::CSV;

 

binmode STDOUT, ':utf8';

my @rows;

my $csv = Text::CSV->new ( { binary => 1 } )  # should set binary attribute.

                 or die "Cannot use CSV: ".Text::CSV->error_diag ();

 

open my $fh, "<:utf8", "test.csv" or die "test.csv: $!";

 

while ( my $row = $csv->getline( $fh ) ) {

     for (@$row) {

        print $_,",";

     }

     print "/n";

     push @rows, $row;

}

 

$csv->eof or $csv->error_diag();

close $fh;

 

以下是从网上搜索的解释

perldoc perldiag 
[snip] 
Wide character in %s 
(W utf8) Perl met a wide character (>255) when it wasn't 
expecting 
one. This warning is by default on for I/O (like print). The 
easiest way to quiet this warning is simply to add the ":utf8" 
layer 
to the output, e.g. "binmode STDOUT, ':utf8'". Another way to 
turn 
off the warning is to add "no warnings 'utf8';" but that is 
often 
closer to cheating. In general, you are supposed to 
explicitly mark 
the filehandle with an encoding, see open and "binmode" in 
perlfunc. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值