问题描述
我对 Perl 中的 local/our
范围几乎没有怀疑.我阅读了很多文档,但我仍然很困惑.以下是困惑
什么是
local
作用域?我读到的是 -> 本地复制全局变量的值,更改值,用户将使用它,并且在块之外它将保留全局值
Confusion ->
my
做同样的事情.我看到的唯一好处是像$package::var
这样的一些变量不能用我的范围声明,但可以用本地范围声明.本地还有什么什么是全局"变量?
读取的是 -> 它的范围在包内.基本上我们将全局变量放在 @EXPORT
数组中并使用它或附加命名空间以在其他包中使用.
怀疑 -> 同样,如果我们仅在 main 中声明具有 my
范围的变量,那么我们就可以在整个包中访问该变量.是对的吗?是否可以在 @EXPORT
数组中添加 my
作用域变量并在其他包中使用它?
我认为全局变量是用 our
关键字声明的.还有其他方法吗?
这个问题可能看起来重复,但我很困惑.
在作用域方面,Perl 中有两种变量.
- 词法变量是词法范围的,这意味着它们只在当前词法范围内可见.
- 包变量是全局范围的,这意味着它们对解释器中的所有代码都是可见的.
以下是创建变量的方法.
my
创建一个词法变量.our
创建一个词法变量,它是当前包中同名变量的别名.换句话说,our $foo;
和alias my $foo = $The::Current::Package::foo;
是一样的.- 全局变量是在使用时创建的.
local
不创建任何变量.它只是简单地备份一个变量,直到当前的词法作用域被销毁.
my
做同样的事情.
没有.local
不会改变变量的作用域.虽然词法变量只在词法范围内可见,但局部化的全局变量在整个解释器中仍然可见.
$x = 123;子 foo { 打印$x
";}{ 本地 $x = 456;富();#456富();#123
$x = 123;子 foo { 打印$x
";}{ 我的 $x = 456;富();#123富();#123
还有什么local
local
主要用于逼近 my
的功能,用于无法在词法上声明的变量.
(过去,这是所有变量.从 5.6 开始,只有标点变量不能在词法上声明.)
什么是全局"变量?
全局可见的变量,即解释器中的任何代码.
是否可以在@EXPORT 数组中添加我的作用域变量并在其他包中使用它?
没有.@EXPORT
被导出器使用.导出器除了全局符号之外什么都找不到(因为文件是在新的词法范围内编译的),所以 @EXPORT
必须只包含全局符号.
I am having few doubts about the local/our
scope in Perl. I read a lot of documentation, but I am still in confusion is there. Following are the confusions
What is
local
scope?what I read is -> local copies the value of global variable, change the value, user will use it and outside the block it will retain the global value
Confusion ->
my
does the same thing. Only benefit I see is that some variables like$package::var
cannot be declared with my scope but can be declared with local scope. What else for localWhat is "global" variable?
What is read is -> Its scope is within the package. Basically we put the global variable in @EXPORT
array and use it or append the namespace with it to use in other packages.
doubt -> Again if we declare variable with my
scope in main only then we can access the variable throughout the package. Is that right? Is it possible to add the my
scoped variables in @EXPORT
array and use it in another packages?
I think global variables are declared with our
keyword. Is there any other way to do so?
This question may look repetitive, but I am confused.
In terms of scoping, there are two kinds of variables in Perl.
- Lexical variables are lexically scoped, which means they are only visible in the current lexical scope.
- Package variables are globally scoped, which means they are visible by all code in the interpreter.
Here are ways to create variable.
my
creates a lexical variable.our
creates a lexical variable that is aliased to the variable of the same name in the current package. In other words,our $foo;
is the same asalias my $foo = $The::Current::Package::foo;
.- Global variables are created on use.
local
doesn't create any variables. It simply backs up a variable until the current lexical scope is destroyed.
No. local
does not change the scope of a variable. While a lexical variable is only visible in a lexical scope, a localized global variable is still visible across the entire interpreter.
$x = 123;
sub foo { print "$x
"; }
{ local $x = 456; foo(); } # 456
foo(); # 123
$x = 123;
sub foo { print "$x
"; }
{ my $x = 456; foo(); } # 123
foo(); # 123
local
is primarily used to approximate the functionality of my
for variables that cannot otherwise be declared lexically.
(Historically, that was all variables. Since 5.6, only punctuation variables cannot be declared lexically.)
A variable that can seen globally, i.e. by any code in the interpreter.
No. @EXPORT
is used by Exporter. Exporter would not be able to find anything but global symbols (since files are compiled in fresh lexical scopes), so @EXPORT
must only contain global symbols.
这篇关于perl 中的局部变量和全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!