我模糊地回顾,警告的pragma应该在我们正在加载使用的模块的列表中最后.我也模糊地记得,它与模块注册自己的警告类别有关,但我无法重现任何问题.有人可以指向相关文章,还是显示一个示例,其中警告标注的位置有何区别?
解决方法
这可能是你所指的.无论哪种方式,都需要注意,我将其作为一个错误提交
我/ 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.
并且似乎有必要在使用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.