加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 23:31:01 所属栏目:大数据 来源:网络整理
导读:我正在研究一个大约4000行的Perl CGI脚本.我们的编码风格通常包括使用严格和使用警告,但在这个特定的(相当旧的)文件中,“使用警告”被注释掉,注释表明启用警告会泛滥Apache日志. 现在我打算将一些代码分成一个新的子程序.我想至少在那里使用警告.如何安全地
我正在研究一个大约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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读