0

通过“掌握 perl”,我覆盖了“编码”模块的“编码”功能。有没有更短的方法可以使 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/// 之后的换行符是从哪里来的?

4

1 回答 1

3

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

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

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

印刷

foo
---

.匹配换行符,请将/s修饰符添加到您的正则表达式中:

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

印刷

foo ---
于 2011-03-02T09:51:22.920 回答