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