如何在 Perl 中将某些特定的鲤鱼更改为黄花鱼?

发布于 2024-09-01 17:50:21 字数 889 浏览 4 评论 0原文

我试图捕捉鲤鱼警告:

 carp "$start is > $end" if (warnings::enabled()); ) 

使用 eval {} 但它没有工作,所以我查看了 eval 文档,发现 eval 仅捕获语法错误、运行时错误或执行的 die 语句。

我怎样才能捕捉到鲤鱼警告?

#!/usr/bin/env perl
use warnings;
use strict;
use 5.012;
use List::Util qw(max min);
use Number::Range;

my @array;
my $max = 20;
print "Input (max $max): ";
my $in = <>;

$in =~ s/\s+//g;
$in =~ s/(?<=\d)-/../g;

eval {
    my $range = new Number::Range( $in );
    @array = sort { $a <=> $b } $range->range;
};
if ( $@ =~ /\d+ is > \d+/ ) { die $@ }; # catch the carp-warning doesn't work 

die "Input greater than $max not allowed $!" if defined $max and max( @array ) > $max;
die "Input '0' or less not allowed $!" if min( @array ) < 1;
say "@array";

I tried to catch a carp-warning:

 carp "$start is > $end" if (warnings::enabled()); ) 

with eval {} but it didn't work, so I looked in the eval documentation and I discovered, that eval catches only syntax-errors, run-time-errors or executed die-statements.

How could I catch a carp warning?

#!/usr/bin/env perl
use warnings;
use strict;
use 5.012;
use List::Util qw(max min);
use Number::Range;

my @array;
my $max = 20;
print "Input (max $max): ";
my $in = <>;

$in =~ s/\s+//g;
$in =~ s/(?<=\d)-/../g;

eval {
    my $range = new Number::Range( $in );
    @array = sort { $a <=> $b } $range->range;
};
if ( $@ =~ /\d+ is > \d+/ ) { die $@ }; # catch the carp-warning doesn't work 

die "Input greater than $max not allowed $!" if defined $max and max( @array ) > $max;
die "Input '0' or less not allowed $!" if min( @array ) < 1;
say "@array";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

七月上 2024-09-08 17:50:21

根据您的评论,我的理解是您希望将 carp 设为致命警告。

如果可以接受将目标包中的所有 carp 警告变成致命错误,那么您可以对 carp 进行猴子补丁。

Carping包:

package Foo;
use Carp;

sub annoying_sub {
    carp "Whine whine whine";
}

主程序:

use Foo;

*Foo::carp = \&Foo::croak;

Foo::annoying_sub();

如果想将monkey补丁限制在动态范围内,可以使用local

use Foo;

Foo::annoying_sub();  # non-fatal

{   local *Foo::carp = \&Foo::croak;
    Foo::annoying_sub();  # Fatal
}

Based on your comments, my understanding is that you would like to make carp into a fatal warning.

If it is acceptable to make all carp warnings in your target package into fatal errors you can monkey-patch carp.

Carping Package:

package Foo;
use Carp;

sub annoying_sub {
    carp "Whine whine whine";
}

Main program:

use Foo;

*Foo::carp = \&Foo::croak;

Foo::annoying_sub();

If you want to limit the monkey patch to a dynamic scope, you can use local:

use Foo;

Foo::annoying_sub();  # non-fatal

{   local *Foo::carp = \&Foo::croak;
    Foo::annoying_sub();  # Fatal
}
我不在是我 2024-09-08 17:50:21

carp 不会死,只是打印一个警告,所以 eval 或其他什么都没有什么可捕获的。但是,您可以在本地覆盖警告处理程序,以防止警告发送到 stderr:

#!/usr/bin/env perl

use warnings;
use strict;

use Carp;

carp "Oh noes!";

{
    local $SIG{__WARN__} = sub {
        my ($warning) = @_;

        # Replace some warnings:
        if($warning =~ /replaceme/) {
            print STDERR "My new warning.\n";
        }
        else {
            print STDERR $warning;
        }

        # Or do nothing to silence the warning.
    };

    carp "Wh00t!";
    carp "replaceme";
}

carp "Arrgh!";

输出:

Oh noes! at foo.pl line 8
Wh00t! at foo.pl line 25
My new warning.
Arrgh! at foo.pl line 29

在几乎所有情况下,您应该更愿意修复鲤鱼的原因。

carp does not die but just prints a warning, so there's nothing to catch with eval or whatever. You can, however, overwrite the warn handler locally to prevent the warning from being sent to stderr:

#!/usr/bin/env perl

use warnings;
use strict;

use Carp;

carp "Oh noes!";

{
    local $SIG{__WARN__} = sub {
        my ($warning) = @_;

        # Replace some warnings:
        if($warning =~ /replaceme/) {
            print STDERR "My new warning.\n";
        }
        else {
            print STDERR $warning;
        }

        # Or do nothing to silence the warning.
    };

    carp "Wh00t!";
    carp "replaceme";
}

carp "Arrgh!";

Output:

Oh noes! at foo.pl line 8
Wh00t! at foo.pl line 25
My new warning.
Arrgh! at foo.pl line 29

In almost all cases you should prefer fixing the cause of the carp instead.

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