通过“掌握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;
}
子问题:s ///之后$ error_message中的换行符来自哪里?
答案 0 :(得分: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 ---