perl - Perl 中的 warnings::warn 和 FATAL 类别

标签 perl warnings

我一定是错误地理解了警告文档。我读它的方式,这段代码:

use warnings;
use warnings FATAL => 'all';
warnings::warn('numeric', 'blarg');
print "finished\n";

应该打印 'blarg' 警告并死掉,因为我已经要求所有警告都是致命的。但是,当我运行代码时,我得到:
$> /opt/local/bin/perl x.pl 
blarg at x.pl line 3
finished

有人能帮我理解为什么我不能得到死亡警告吗?

最佳答案

好的。这很丑陋。我有一个半准备的帖子将其解释为 warnings 中的一个错误,然后我意识到它不是,它只是一种非常邪恶的微妙方式warnings作品。

警告开始寻找相关的堆栈帧以从 warnings::warn 中获取警告位。的来电者的来电者。这个想法是您正在编写一些模块并使用 warnings::warnwarnings::warnif在您的函数中,是否打印(或致命)警告取决于 use warnings在使用您的模块的代码中设置范围。没有提供从 caller(1) 开始的选项而不是 caller(2) ,所以你想要的效果是不可能的。

一个有效的代码示例(并演示了这个接口(interface)是如何被编写它的人使用的):

package Foo;
require warnings;

sub bail {
  warnings::warnif('numeric', "You fool! You divided by zero!");
}

package main;
use warnings FATAL => all;

Foo::bail();
print "Will never be reached\n";

而且你不能仅仅通过添加另一个级别的子程序来破坏它的工作方式,因为它从第一个调用者那里获取标志,该调用者与 warn 的调用者在不同的包中。/warnif/enable/ETC。

关于perl - Perl 中的 warnings::warn 和 FATAL 类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1592397/

相关文章:

c++ - 关于不一致的dll链接

qt - 如何在 Qt 中禁用某些控制台警告

windows - 仅当鼠标悬停时滚动

perl - 内存高效的统计分布模块

perl - 使用 DBIx::Class 的多对多访问器

PHP:在没有警告的情况下打印 undefined variable

elixir - 我们如何解决 Elixir 中的冲突行为警告

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

excel - 在perl中读写excel文件

perl - 如何捕获对 %ENV 的更改?