0
通过“掌握perl”我已经覆盖了“编码”模块的“编码”功能。有没有更简单的方法来使编码 - utf8 - 警告致命?使模块警告致命的最简单方法是什么?
#!/usr/bin/env perl
use warnings;
use 5.012;
binmode STDOUT, ':encoding(utf-8)';
BEGIN {
use Encode;
no warnings 'redefine';
*Encode::encode = sub ($$;$) {
my ($name, $string, $check) = @_;
return undef unless defined $string;
$string .= '' if ref $string;
$check ||= 0;
unless (defined $name) {
require Carp;
Carp::croak("Encoding name should not be undef");
}
my $enc = find_encoding($name);
unless (defined $enc) {
require Carp;
Carp::croak("Unknown encoding '$name'");
}
use warnings FATAL => 'utf8'; ###
my $octets = $enc->encode($string, $check);
$_[1] = $string if $check and !ref $check and !($check & LEAVE_SRC());
return $octets;
}
}
use Encode qw(encode);
use warnings FATAL => 'utf8';
my $character;
{
no warnings 'utf8';
$character = "\x{ffff}";
# $character = "\x{263a}";
}
my $utf32;
eval { $utf32 = encode('utf-32', $character) };
if ([email protected]) {
(my $error_message = [email protected]) =~ s/\K\sin\ssubroutine.*$//;
chomp $error_message; # where does the newline come from?
say $error_message;
}
else {
my @a = unpack('(B8)*', $utf32);
printf "utf-32 encoded:\t%8s %8s %8s %8s %8s %8s %8s %8s\n", @a;
}
subquestion:s ///之后的$ error_message中的换行符来自哪里?
我必须覆盖编码功能,使UTF8的警告致命? – 2011-03-02 10:12:35
@sid_com:是的,如果你打算通过使用警告FATAL来做到这一点,因为'使用警告'是词法范围的,并且不会影响另一个文件中的代码。但是,您可以从$ SIG {__ WARN__} = sub {die @_; };然后添加一个条件,以便它只会在UTF-8错误上死掉 - 但是您可能必须通过检查错误信息来检测它们,这些信息充满了龙。 – 2011-03-02 11:19:01