perl – 我可以只在一个子程序中“使用警告”吗?

前端之家收集整理的这篇文章主要介绍了perl – 我可以只在一个子程序中“使用警告”吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在研究一个大约4000行的Perl CGI脚本.我们的编码风格通常包括使用严格和使用警告,但在这个特定的(相当旧的)文件中,“使用警告”被注释掉,注释表明启用警告会泛滥Apache日志.

现在我打算将一些代码分成一个新的子程序.我想至少在那里使用警告.如何安全地限制使用警告对一个子程序的影响?只是将use子句放在子程序中就能完成这项工作吗?

解决方法

是的,使用警告将在您编写它的范围内.

在子内写入使用警告只会影响给定例程(或块).

示例代码

sub foo {
  use warnings;
  print  my $a; 
}

{
  use warnings;
  print  my $b;
}

foo;

print my $c;

产量

Use of uninitialized value $b in print at foo.pl line 8.
Use of uninitialized value $a in print at foo.pl line 3.

请注意,没有关于使用print my $c的警告.

文件说什么?

> perldoc.perllexwarn

This pragma works just like the strict pragma. This means that the scope of the warning pragma is limited to the enclosing block. It also means that the pragma setting will not leak across files (via use,require or do). This allows authors to independently define the degree of warning checks that will be applied to their module.

猜你在找的Perl相关文章