我知道my在Perl中是什么。它定义了一个只存在于定义它的块范围内的变量。我们该怎么做?
我们的和我的有什么不同?
我知道my在Perl中是什么。它定义了一个只存在于定义它的块范围内的变量。我们该怎么做?
我们的和我的有什么不同?
当前回答
我们的和我的有什么不同,我们做什么?
总而言之:
my是一种声明非包变量的方法,它们是:
私人 新 非全局 与任何包分开,这样变量就不能以$package_name::variable的形式访问。
另一方面,我们的变量是包变量,因此自动:
全局变量 绝对不是私人的 不一定是新的 可以在包(或词法范围)之外使用 限定的命名空间,如$package_name::variable。
使用our声明变量允许您预先声明变量,以便在use strict下使用它们,而不会得到拼写警告或编译时错误。从Perl 5.6开始,它取代了过时的vars, vars只是文件作用域,而不像我们的那样是词汇作用域。
例如,main包中变量$x的正式限定名是$main::x。声明我们的$x允许你在声明的范围内,当脚本使用use strict或使用strict“vars”时,使用纯$x变量而没有惩罚(即,没有导致错误)。范围可以是一个、两个或多个包,或者一个小块。
其他回答
一个例子:
use strict;
for (1 .. 2){
# Both variables are lexically scoped to the block.
our ($o); # Belongs to 'main' package.
my ($m); # Does not belong to a package.
# The variables differ with respect to newness.
$o ++;
$m ++;
print __PACKAGE__, " >> o=$o m=$m\n"; # $m is always 1.
# The package has changed, but we still have direct,
# unqualified access to both variables, because the
# lexical scope has not changed.
package Fubb;
print __PACKAGE__, " >> o=$o m=$m\n";
}
# The our() and my() variables differ with respect to privacy.
# We can still access the variable declared with our(), provided
# that we fully qualify its name, but the variable declared
# with my() is unavailable.
print __PACKAGE__, " >> main::o=$main::o\n"; # 2
print __PACKAGE__, " >> main::m=$main::m\n"; # Undefined.
# Attempts to access the variables directly won't compile.
# print __PACKAGE__, " >> o=$o\n";
# print __PACKAGE__, " >> m=$m\n";
# Variables declared with use vars() are like those declared
# with our(): belong to a package; not private; and not new.
# However, their scoping is package-based rather than lexical.
for (1 .. 9){
use vars qw($uv);
$uv ++;
}
# Even though we are outside the lexical scope where the
# use vars() variable was declared, we have direct access
# because the package has not changed.
print __PACKAGE__, " >> uv=$uv\n";
# And we can access it from another package.
package Bubb;
print __PACKAGE__, " >> main::uv=$main::uv\n";
My用于局部变量,而our用于全局变量。
更多内容请参阅Perl中的变量作用域:基础知识。
perldoc对我们的。
与我的方法不同,我的方法既为变量分配存储空间,又将一个简单的名称与该存储空间关联起来,以便在当前作用域内使用,而我们的方法则将一个简单的名称与当前包中的一个包变量关联起来,以便在当前作用域内使用。换句话说,our具有与my相同的作用域规则,但不一定创建变量。
来自cartman和Olafur的PerlMonks和PerlDoc链接是一个很好的参考-下面是我的总结:
我的变量在词法上是在一个由{}定义的块中,如果不在{}s中,则在同一个文件中。它们不能从同一词法作用域/块之外定义的包/子例程中访问。
变量的作用域在包/文件内,可以从任何使用或要求包/文件名称冲突通过预挂适当的名称空间来解决的代码中访问。
只是为了完善它,局部变量是“动态”范围的,与我的变量不同的是,它们也可以从同一块中调用的子例程中访问。
让我们思考一下解释器到底是什么:它是一段在内存中存储值的代码,并让它所解释的程序中的指令通过指令中指定的名称访问这些值。因此,解释器的主要工作是制定规则,规定我们应该如何在这些指令中使用名称来访问解释器存储的值。
在遇到“my”时,解释器创建一个词法变量:解释器只能在执行块时访问一个命名值,而且只能从该语法块中访问。在遇到“our”时,解释器会为包变量创建一个词法别名:它将一个名称绑定到具有相同名称的包变量的值上,从那时起解释器就应该将其作为词法变量的名称处理,直到块完成。
The effect is that you can then pretend that you're using a lexical variable and bypass the rules of 'use strict' on full qualification of package variables. Since the interpreter automatically creates package variables when they are first used, the side effect of using "our" may also be that the interpreter creates a package variable as well. In this case, two things are created: a package variable, which the interpreter can access from everywhere, provided it's properly designated as requested by 'use strict' (prepended with the name of its package and two colons), and its lexical alias.
来源:
http://perldoc.perl.org/functions/our.html http://perldoc.perl.org/perlsub.html Private-Variables-via-my ()