我看到了这个问题:“标准”和块包声明之间有什么区别吗?和思考main package
。当我编写脚本时,例如:
---- 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 ----
这会自动进入main
包中,所以据我了解,接下来会发生。
---- 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 ----
问题一:上面说的对吗?主包会发生这种情况吗?
现在是BEGIN/END
块和编译指示。如果我理解正确,则在编译阶段进行处理。所以有:
---- 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 语法的上下文中如何处理
pragmas
,BEGIN/END/CHECK
块和?main package
- 当将“文件范围”更改为“块范围”时 - 或者如果它没有更改,“标准主包”到“主包 {block}”的等效翻译是什么
最后一个代码:
---- 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 {
}
如何my $var
进入main package?
所以这在某种程度上被翻译为:
---- 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????
}
对不起,文字墙...