【发布时间】:2014-04-07 02:30:49
【问题描述】:
我想要关于 Perl 的建议。
我有想要用 Perl 处理的文本文件。这些文本文件以 cp932 编码,但由于某些原因,它们可能包含格式错误的字符。
我的程序是这样的:
#! /usr/bin/perl -w
use strict;
use encoding 'utf-8';
# 'workfile.txt' is supposed to be encoded in cp932
open my $in, "<:encoding(cp932)", "./workfile.txt";
while ( my $line = <$in> ) {
# my process comes here
print $line;
}
如果 workfile.txt 包含格式错误的字符,Perl 会抱怨:
cp932 "\x81" does not map to Unicode at ./my_program.pl line 8, <$in> line 1234.
Perl 知道它的输入是否包含格式错误的字符。所以我想重写以查看我的输入是好是坏并采取相应的行动,例如打印所有好的行(不包含格式错误的字符的行)以输出文件句柄 A,并打印包含格式错误的字符的行以输出文件句柄 B。
#! /usr/bin/perl -w
use strict;
use encoding 'utf-8';
use English;
# 'workfile.txt' is supposed to be encoded in cp932
open my $in, "<:encoding(cp932)", "./workfile.txt";
open my $output_good, ">:encoding(utf8)", "good.txt";
open my $output_bad, ">:encoding(utf8)", "bad.txt";
select $output_good; # in most cases workfile.txt lines are good
while ( my $line = <$in> ) {
if ( $line contains malformed characters ) {
select $output_bad;
}
print "$INPUT_LINE_NUMBER: $line";
select $output_good;
}
我的问题是如何编写“if ($line contains malfoomed characters)”部分。如何检查输入的好坏。
提前致谢。
【问题讨论】:
-
不要使用
use encoding。越野车。已弃用。