gpt4 book ai didi

perl - Try::Tiny:捕获警告的问题

转载 作者:行者123 更新时间:2023-12-03 02:14:48 26 4
gpt4 key购买 nike

为什么带有“$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;
}
}
}

最佳答案

这是因为警告也不异常(exception)。但是您可以通过以下方式使警告抛出异常

use warnings FATAL => 'all';

关于perl - Try::Tiny:捕获警告的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5125876/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com