为什么要使用警告;走到最后?

时间:2016-07-28 14:00:11

标签: perl warnings

我含糊地回忆起<xsl:apply-templates>编译指示应该在加载warnings的模块列表中排在最后。我还隐约记得它与模块注册自己的警告类别有关,但我无法重现任何问题。有人可以指向相关文章或显示use编译指示的位置有所不同的示例吗?

1 个答案:

答案 0 :(得分:11)

这可能就是你所指的。无论哪种方式都需要注意,我将其作为错误提交

我/ Warnings.pm

package My::Warnings;

use warnings::register;

sub test {
    warnings::warnif 'This is my warning';
}

1;

main.pl

use strict;
use feature 'switch';

use warnings 'all';

use My::Warnings;

print undef;

given (1) { }

My::Warnings::test();

正如所料,这将输出

given is experimental at E:\Perl\source\main.pl line 10.
Use of uninitialized value in print at E:\Perl\source\main.pl line 8.
This is my warning at E:\Perl\source\main.pl line 12.

但是,如果任何警告类别被禁用,它也将禁用自定义类别。喜欢这个

use strict;
use feature 'switch';

use warnings 'all';
no warnings 'experimental';

use My::Warnings;

print undef;

given (1) { }

My::Warnings::test();

这只输出

Use of uninitialized value in print at E:\Perl\source\main.pl line 9.

并且似乎有必要在 use My::Warnings之后启用警告以使其执行

use strict;
use feature 'switch';

use My::Warnings;

use warnings 'all';
no warnings 'experimental';

print undef;

given (1) { }

My::Warnings::test();

可生产

Use of uninitialized value in print at E:\Perl\source\main.pl line 9.
This is my warning at E:\Perl\source\main.pl line 13.


更新

更重要的是,重新启用关闭自定义警告的类别会让他们无法使用

像这样的东西

use strict;
use feature 'switch';

use warnings 'all';
no warnings 'experimental';
use warnings 'experimental';

use My::Warnings;

print undef;

given (1) { }

My::Warnings::test();

仅打印

given is experimental at E:\Perl\source\main.pl line 12.
Use of uninitialized value in print at E:\Perl\source\main.pl line 10.