为什么要使用警告;最后走?
Why should use warnings; go last?
我依稀记得 warnings
pragma 应该在用 use
加载的 us 模块列表中排在最后。我也依稀记得跟模块注册自己的warning category有关系,但是我无法重现任何问题。有人可以指出相关文章或展示 warnings
pragma 的位置有所不同的示例吗?
这可能就是您所指的。无论哪种方式,这都是需要注意的事情,我将其作为错误提交。 编辑 bug 已在 v5.27.6 中修复。
My/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.
我依稀记得 warnings
pragma 应该在用 use
加载的 us 模块列表中排在最后。我也依稀记得跟模块注册自己的warning category有关系,但是我无法重现任何问题。有人可以指出相关文章或展示 warnings
pragma 的位置有所不同的示例吗?
这可能就是您所指的。无论哪种方式,这都是需要注意的事情,我将其作为错误提交。 编辑 bug 已在 v5.27.6 中修复。
My/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.