使模块警告致命的最简单方法是什么?
通过“掌握perl”,我覆盖了“Encode”模块的“encode”函数。有没有更短的方法使encode-utf8-warnings致命?
#!/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 ( $@ ) {
( my $error_message = $@ ) =~ 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;
}
子问题: $error_message 中 s/// 后面的换行符来自哪里?
By means of "mastering perl" I've overwritten the "encode"-function of the "Encode"-module. Is there a shorter way to make the encode-utf8-warnings fatal?
#!/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 ( $@ ) {
( my $error_message = $@ ) =~ 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: where does the newline in $error_message after the s/// come from?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我是否明白你的主要问题...
use warnings FATAL =>; 'utf8';
已经很短了;我认为你不可能找到更短的东西。至于子问题,默认情况下,正则表达式中的
.
将匹配除换行符之外的任何字符,因此替换不会删除最终的换行符:prints
To get < code>. 要匹配换行符,请将
/s
修饰符添加到正则表达式中:
I'm not sure I follow your main question...
use warnings FATAL => 'utf8';
is pretty short already; I don't think you're likely to find anything shorter.As for the subquestion,
.
in a regex will, by default, match any character except a newline, so that substitution doesn't remove the final newline:prints
To get
.
to match newlines, add the/s
modifier to your regex:prints