Try::Tiny:捕获警告的问题
为什么带有“$encoded =encode($encoding, $character, Encode::FB_WARN | Encode::LEAVE_SRC);”行的第二个示例没有捕获编码警告?
#!/usr/bin/env perl
use warnings;
use 5.012;
use utf8;
binmode STDOUT, ':encoding(utf-8)';
use Encode qw(encode);
use Try::Tiny;
my $character = '€';
my @leer = ( ' ' ) x 4;
my $t = 11;
for my $encoding ( 'iso-8859-1', 'iso-8859-15', 'cp1252', 'cp850' ) {
my $temp = $character;
my $encoded;
my $warning = 'error';
try {
$encoded = encode( $encoding, $temp, Encode::FB_CROAK );
} catch {
$warning = $1 if m|^\s*(.*\S)\s*at\s/|;
} finally {
if ( @_ ) {
printf "%-${t}s %s:\t%-63s %s\n", $encoding, "encoded", $warning, '--------';
} else {
my @a = unpack '(B8)*', $encoded;
unshift @a, ' ' x 8 while @a < 4;
printf "%-${t}s %s:\t%8s %8s %8s %8s %8s %8s %8s %8s\n", $encoding, "encoded", @leer, @a;
}
}
}
say "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°";
for my $encoding ( 'iso-8859-1', 'iso-8859-15', 'cp1252', 'cp850' ) {
my $encoded;
my $warning = 'error';
try {
$encoded = encode( $encoding, $character, Encode::FB_WARN | Encode::LEAVE_SRC);
} catch {
$warning = $1 if m|^\s*(.*\S)\s*at\s/|;
} finally {
if ( @_ ) {
printf "%-${t}s %s:\t%-63s %s\n", $encoding, "encoded", $warning, '--------';
} else {
my @a = unpack '(B8)*', $encoded;
unshift @a, ' ' x 8 while @a < 4;
printf "%-${t}s %s:\t%8s %8s %8s %8s %8s %8s %8s %8s\n", $encoding, "encoded", @leer, @a;
}
}
}
Why does the second example with the "$encoded = encode( $encoding, $character, Encode::FB_WARN | Encode::LEAVE_SRC);"-line not catch the encode-warnings?
#!/usr/bin/env perl
use warnings;
use 5.012;
use utf8;
binmode STDOUT, ':encoding(utf-8)';
use Encode qw(encode);
use Try::Tiny;
my $character = '€';
my @leer = ( ' ' ) x 4;
my $t = 11;
for my $encoding ( 'iso-8859-1', 'iso-8859-15', 'cp1252', 'cp850' ) {
my $temp = $character;
my $encoded;
my $warning = 'error';
try {
$encoded = encode( $encoding, $temp, Encode::FB_CROAK );
} catch {
$warning = $1 if m|^\s*(.*\S)\s*at\s/|;
} finally {
if ( @_ ) {
printf "%-${t}s %s:\t%-63s %s\n", $encoding, "encoded", $warning, '--------';
} else {
my @a = unpack '(B8)*', $encoded;
unshift @a, ' ' x 8 while @a < 4;
printf "%-${t}s %s:\t%8s %8s %8s %8s %8s %8s %8s %8s\n", $encoding, "encoded", @leer, @a;
}
}
}
say "°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°";
for my $encoding ( 'iso-8859-1', 'iso-8859-15', 'cp1252', 'cp850' ) {
my $encoded;
my $warning = 'error';
try {
$encoded = encode( $encoding, $character, Encode::FB_WARN | Encode::LEAVE_SRC);
} catch {
$warning = $1 if m|^\s*(.*\S)\s*at\s/|;
} finally {
if ( @_ ) {
printf "%-${t}s %s:\t%-63s %s\n", $encoding, "encoded", $warning, '--------';
} else {
my @a = unpack '(B8)*', $encoded;
unshift @a, ' ' x 8 while @a < 4;
printf "%-${t}s %s:\t%8s %8s %8s %8s %8s %8s %8s %8s\n", $encoding, "encoded", @leer, @a;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为警告也不例外。但是你可以通过以下方式使警告抛出异常
It is because warning is not an exception. But you can make warnings throw exception by