Perl的主要包 – 块语法 – pragma和BEGIN / END块
我看到了这个问题:
Is there any difference between “standard” and block package declaration?并考虑了主要的包装.当我编写脚本时,例如:
---- begin of the file --- #!/usr/bin/perl #probably removed by shell? my $var; #defined from now up to the end of file ... ---- end of the file ---- 这自动进入主包,所以我理解正确的下一个发生. ---- begin of the file --- { #<-- 1st line package main; my $var; #variable transformed to block scope - "up to the end of block" ... } # <-- last line ---- end of the file ---- 这相当于 ---- begin of the file --- package main { #1st line my $var; #variable block scope ... } #last line ---- end of the file ---- 问题1:以上是对的吗?主包发生了什么? 现在是BEGIN / END块和pragma.如果我理解的话,在编译阶段处理.所以有: ---- begin of the file --- #!/usr/bin/perl use strict; #file scope use warnings; #file scope my $var; #defined from now up to the end of file BEGIN { say $var; #the $var is not known here - but it is declared } ... ---- end of the file ---- 声明$var,但在这里 ---- begin of the file --- #!/usr/bin/perl use strict; #file scope use warnings; #file scope BEGIN { say $var; #the $var is not known here - but "requires explicit package name" error } my $var; #defined from now up to the end of file ... ---- end of the file ---- $var未声明. 那么如何将上述内容翻译成“默认主包”? 它始终是: ---- begin of the file --- { package main; use strict; #block scope ??? use warnings; #block scope ??? my $var; #defined from now up to the end of block BEGIN { #NESTED??? say $var; #the $var is not known here - but declared } ... } ---- end of the file ---- 相当于 ---- begin of the file --- package main { use strict; #block scope use warnings; #block scope my $var; #defined from now up to the end of block BEGIN { #NESTED block say $var; } ... } ---- end of the file ---- 问题是 – 在这里_ANY使用类似的东西: ---- begin of the file --- use strict; #always should be at the START OF THE FILE - NOT IN BLOCKS? use warnings; #not NESTED BEGIN { } package main { my $var; } 所以问题是: >在BLOCK语法的上下文中,如何处理pragma,BEGIN / END / CHECK块和主包? 和最后一个代码: ---- begin of the file --- use strict; #always should be at the START OF THE FILE - NOT IN BLOCKS? use warnings; my $var; #not NESTED BEGIN { } package main { } 我的$var如何进入主包?所以这被翻译成: ---- begin of the file --- use strict; #always should be at the START OF THE FILE - NOT IN BLOCKS? use warnings; #not NESTED BEGIN { } package main { my $var; #### GETS HERE???? } 对不起文字墙… 解决方法
使用my声明变量时,它不在任何包中.完全没有.块范围与任何包都严格不同.该变量在最内层封闭块的右括号(})之前有效,但没有包限定.如果你写了$main :: var或$:: var,它将是不同的变量.
use warnings; use strict; package main { my $var = 'this'; } $var; # error,$var was not declared in this scope say $main::var; # says nothing 还有两种方法可以声明变量: >使用vars qw($var)使$var引用包中的当前包中的变量. 块包声明是一个块,并将其内容放在包中.而无块包声明将以下内容放在另一个包中,但当前块范围仍在继续. 另一个缺失的是你写的时候 use warnings; use strict; package main { # ... } 你有效地写了 package main { use warnings; use strict; package main { # ... } } 因为包装是相同的,所以是相同的 package main { use warnings; use strict; { # ... } } 换句话说,包在文件开头是主要的,并且隐式块作用域(文件范围)是打开的.当您重新进入主程序包时,它没有任何效果,如果它与程序段关联,它的行为就像任何程序段一样. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |