支架代码部分在使用严格/不严格?

ste*_*iva 6 perl strict

我继承了一些perl代码,当然不使用strict或warnings,并且我继续使用未初始化的变量等.

我想将我正在修改的代码部分括起来:

use warnings;
use strict;

... my code changes and additions ...

no strict;
no warnings;
Run Code Online (Sandbox Code Playgroud)

这似乎有效,但是当我说这些是导入当前"块范围"的编译器指令时,我遇到了破解perldoc使用意味着什么问题.这是否意味着任何范围都可以与一个use strict未配对no strict?是否no strict在全局范围的尾部基本上取消了use strict早期在同一范围内的含义?

yst*_*sth 11

"块范围"意味着从它们到最里面的封闭块的末尾的位置use strict;no strict;影响,所以不,后来no strict不会撤消更早use strict.它只是从代码中的那一点开始为最里面的范围更改它.所以:

{
    use strict;
    # strict in effect
    {
        # strict still in effect
        no strict;
        # strict not in effect
    }
    # strict in effect
    no strict;
    # strict not in effect
    use strict;
    # strict in effect
}
Run Code Online (Sandbox Code Playgroud)

  • "块范围"也称为"词法范围".重要的是要注意词法范围*不*进入子程序调用."词汇",如"你在页面上看到的内容".当你想使用符号引用时,常常会在小块中使用`no strict`.常见的用途是动态创建子程序.`{no strict'refs';*{$ sub_name} = sub {...}}`. (2认同)