使模块警告致命的最简单方法是什么?

发布于 2024-10-19 21:18:58 字数 1482 浏览 3 评论 0原文

通过“掌握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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明天过后 2024-10-26 21:18:58

我不确定我是否明白你的主要问题... use warnings FATAL =>; 'utf8'; 已经很短了;我认为你不可能找到更短的东西。

至于子问题,默认情况下,正则表达式中的 . 将匹配除换行符之外的任何字符,因此替换不会删除最终的换行符:

$ perl -e '$foo = "foo bar baz\n"; $foo =~ s/bar.*$//; print $foo . "---\n";'

prints

foo
---

To get < code>. 要匹配换行符,请将 /s 修饰符添加到正则表达式中

perl -e '$foo = "foo bar baz\n"; $foo =~ s/bar.*$//s; print $foo . "---\n";'

foo ---

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:

$ perl -e '$foo = "foo bar baz\n"; $foo =~ s/bar.*$//; print $foo . "---\n";'

prints

foo
---

To get . to match newlines, add the /s modifier to your regex:

perl -e '$foo = "foo bar baz\n"; $foo =~ s/bar.*$//s; print $foo . "---\n";'

prints

foo ---
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文