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

发布于 2024-10-19 02:45:27 字数 1697 浏览 7 评论 0原文

为什么带有“$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 技术交流群。

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

发布评论

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

评论(1

幻想少年梦 2024-10-26 02:45:27

这是因为警告也不例外。但是你可以通过以下方式使警告抛出异常

use warnings FATAL => 'all';

It is because warning is not an exception. But you can make warnings throw exception by

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