如何在 Perl 中捕获鲤鱼的输出? [英] How can I catch the output from a carp in Perl?

查看:34
本文介绍了如何在 Perl 中捕获鲤鱼的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Perl 模块,我正在使用 carp 向调用程序抛出一个非致命警告.

I am writing a Perl module, and I am using carp to throw a non-fatal warning back to the calling program.

鲤鱼警告工作正常 - 我正在检查输入参数是否满足特定条件 - 如果不满足条件,则使用 carp 发送警告,并且模块继续使用参数的默认值而不是默认值调用程序通过.警告只是通知正在使用默认参数而不是传入的参数.

The carp warning works fine - I am checking if an input parameter meets a certain condition - if it does not meet the condition, a warning is sent with carp and the module continues on using a default for the parameter instead of the one the calling program passed. The warning is just to notify that a default parameter is being used instead of the passed in parameter.

我的问题是我的测试脚本.我的测试脚本向模块发送了一个错误的参数,我正在尝试捕捉返回的警告消息并确保我收到了正确的警告消息.

My problem is with my test script. My test script is sending in a bad parameter to the module, and I am trying to catch the warning message that comes back and make sure I got the correct warning message.

我的模块看起来像这样:

My module looks something like this:

else {
  carp "value must be numeric - using default value";
}

我的测试脚本如下所示:

and my test script looks like this:

eval {
  #call to my module
};
like (
    $@,
    qr/value must be numeric/,
    "Should abort on non-numeric value"
);

当我运行测试时,我可以在屏幕上看到警告(它必须转到 STDERR),但是 $@ 变量的内容是 '' - 空白.

When I run the test, I can see the warning (it must be going to STDERR) on the screen, but the contents of the $@ variable is '' - blank.

这是我的测试脚本的输出:

Here is the output from my test script:

t/04bad_method_calls....ok 10/12value must be numeric - using default value at ...
#   Failed test 'Should abort on non-numeric value'
#   at t/04bad_method_calls.t line 98.
t/04bad_method_calls....NOK 12
#                   '' doesn't match '(?-xism:value must be numeric)'
# Looks like you failed 1 test of 12.

如果我将鲤鱼改为呱呱声,我的测试脚本就会工作 - 它会捕获错误消息(但我只想警告,而不是中止).

If I change the carp to a croak, my test script works - it catches the error message (but I only want to warn, not abort).

老实说,我对 eval 没有最好的理解——也许这不是捕捉 carp 警告输出的最佳方式.我尝试使用 $SIG{__WARN__},但它也是空的.

To be honest, I don't have the best understanding of eval - maybe that is not the best way to catch the warning output from carp. I tried using $SIG{__WARN__}, but that was empty as well.

有什么办法可以捕获鲤鱼的输出?这不是最大的问题,因为这只是在我的测试脚本中,但我仍然想让我的测试脚本正常工作.

Is there any way to capture the output from carp? It's not the biggest deal since this is just in my test script, but I'd still like to get my test script to work properly.

提前致谢!

推荐答案

从此页面,http://perldoc.perl.org/perlvar.html,看起来您想将本地 $SIG{__WARN__} 设置为一个子例程,该子例程会将警告转换为测试脚本的致命错误.他们给出的例子是:

From this page, http://perldoc.perl.org/perlvar.html, it looks like you want to set the local $SIG{__WARN__} to a subroutine that will turn warnings into fatal errors for your test script. The example they give is:

local $SIG{__WARN__} = sub { die $_[0] };
eval $proggie;

这篇关于如何在 Perl 中捕获鲤鱼的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆