我和我们的Perl有什么不同?

时间:2021-03-27 13:32:25

I know what my is in Perl. It defines a variable that exists only in the scope of the block in which it is defined. What does our do? How does our differ from my?

我知道我的Perl语言是什么。它定义了一个变量,它只存在于定义它的块的范围内。我们做什么呢?我们和我有什么不同?

12 个解决方案

#1


191  

Great question: How does our differ from my and what does our do?

好问题:我们与我的不同之处是什么?

In Summary:

总而言之:

Available since Perl 5, my is a way to declare:

从Perl 5开始,我是一种声明方式:

  • non-package variables, that are
  • non-package变量,,
  • private,
  • 私人的,
  • new,
  • 新的,
  • non-global variables,
  • 非全局变量,
  • separate from any package. So that the variable cannot be accessed in the form of $package_name::variable.
  • 独立于任何包。因此不能以$package_name::变量的形式访问变量。


On the other hand, our variables are:

另一方面,我们的变量是:

  • package variables, and thus automatically
  • 包变量,因此自动。
  • global variables,
  • 全局变量,
  • definitely not private,
  • 绝对不是私人的,
  • nor are they necessarily new; and they
  • 它们也不一定是新的;他们
  • can be accessed outside the package (or lexical scope) with the qualified namespace, as $package_name::variable.
  • 可以使用限定名称空间($package_name::variable)在包(或词法范围)之外访问。


Declaring a variable with our allows you to predeclare variables in order to use them under use strict without getting typo warnings or compile-time errors. Since Perl 5.6, it has replaced the obsolete use vars, which was only file-scoped, and not lexically scoped as is our.

使用我们的允许来声明一个变量是为了在使用时使用它们,而不需要输入错误警告或编译时错误。由于Perl 5.6,它已经取代了过时的使用vars,它只是文件范围的,而不是像我们的那样在词法范围内。

For example, the formal, qualified name for variable $x inside package main is $main::x. Declaring our $x allows you to use the bare $x variable without penalty (i.e., without a resulting error), in the scope of the declaration, when the script uses use strict or use strict "vars". The scope might be one, or two, or more packages, or one small block.

例如,在package main中变量$x的正式、限定名是$main::x。声明我们的$x允许您使用裸$x变量而不受惩罚(即:在声明的范围内,当脚本使用严格或使用严格的“vars”时,在声明的范围内。范围可能是一个、两个或多个包,或一个小块。

#2


58  

The PerlMonks and PerlDoc links from cartman and Olafur are a great reference - below is my crack at a summary:

来自cartman和Olafur的PerlMonks和PerlDoc是一个很好的参考,下面是我的一个总结:

my variables are lexically scoped within a single block defined by {} or within the same file if not in {}s. They are not accessible from packages/subroutines defined outside of the same lexical scope / block.

我的变量在词法作用域范围内,在一个单独的块中,如果没有在{}中,则在同一个文件中定义。它们不能从定义在同一个词法范围/块之外的包/子例程中访问。

our variables are scoped within a package/file and accessible from any code that use or require that package/file - name conflicts are resolved between packages by prepending the appropriate namespace.

我们的变量限定在包/文件中,并且可以从任何使用或要求包/文件名称冲突的代码中访问,并在适当的名称空间之前通过包来解决。

Just to round it out, local variables are "dynamically" scoped, differing from my variables in that they are also accessible from subroutines called within the same block.

为了解决这个问题,局部变量是“动态”作用域的,不同于我的变量,它们也可以从同一个块中调用的子例程中访问。

#3


43  

An example:

一个例子:

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";

#4


12  

Coping with Scoping is a good overview of Perl scoping rules. It's old enough that our is not discussed in the body of the text. It is addressed in the Notes section at the end.

应对Scoping是对Perl范围规则的一个很好的概述。这已经足够大了,我们在正文中没有讨论。它在最后的注释部分中被处理。

The article talks about package variables and dynamic scope and how that differs from lexical variables and lexical scope.

本文讨论了包变量和动态范围以及它与词法变量和词法作用域的区别。

#5


6  

my is used for local variables, where as our is used for global variables. More reading over Variable Scoping in Perl: the basics .

我的是用于局部变量,在这里我们用于全局变量。Perl中关于变量范围的更多阅读:基础知识。

#6


4  

It's an old question, but I ever met some pitfalls about lexical declarations in Perl that messed me up, which are also related to this question, so I just add my summary here:

这是个老问题,但我在Perl中遇到了一些关于词汇声明的陷阱,这些错误都和这个问题有关,所以我只在这里加上我的总结:

1. definition or declaration?

1。定义或声明?

local $var = 42; 
print "var: $var\n"; 

The output is var: 42. However we couldn't tell if local $var = 42; is a definition or declaration. But how about this:

输出是var: 42。但是,我们无法判断本地$var = 42;是一个定义或声明。但这个怎么样:

use strict;
use warnings;

local $var = 42;
print "var: $var\n";

The second program will throw an error:

第二个程序会抛出一个错误:

Global symbol "$var" requires explicit package name.

$var is not defined, which means local $var; is just a declaration! Before using local to declare a variable, make sure that it is defined as a global variable previously.

$var没有定义,这意味着本地$var;只是一个宣言!在使用local声明变量之前,请确保之前将其定义为全局变量。

But why this won't fail?

但为什么这不会失败呢?

use strict;
use warnings;

local $a = 42;
print "var: $a\n";

The output is: var: 42.

输出是:var: 42。

That's because $a, as well as $b, is a global variable pre-defined in Perl. Remember the sort function?

这是因为$a和$b是一个在Perl中预定义的全局变量。还记得排序函数吗?

2. lexical or global?

2。词汇或全球?

I was a C programmer before starting using Perl, so the concept of lexical and global variables seems straightforward to me: just corresponds to auto and external variables in C. But there're small differences:

在开始使用Perl之前,我是一个C程序员,因此,词汇和全局变量的概念对我来说很简单:只对应于C的自动和外部变量,但有一些细微的差别:

In C, an external variable is a variable defined outside any function block. On the other hand, an automatic variable is a variable defined inside a function block. Like this:

在C中,外部变量是在任何函数块外部定义的变量。另一方面,自动变量是在函数块中定义的变量。是这样的:

int global;

int main(void) {
    int local;
}

While in Perl, things are subtle:

在Perl中,事情很微妙:

sub main {
    $var = 42;
}

&main;

print "var: $var\n";

The output is var: 42, $var is a global variable even it's defined in a function block! Actually in Perl, any variable is declared as global by default.

输出是var: 42, $var是一个全局变量,即使它是在函数块中定义的!实际上,在Perl中,默认情况下,任何变量都被声明为全局变量。

The lesson is to always add use strict; use warnings; at the beginning of a Perl program, which will force the programmer to declare the lexical variable explicitly, so that we don't get messed up by some mistakes taken for granted.

教训是总是要增加使用的严格;使用警告;在Perl程序的开始,这将迫使程序员显式地声明lexical变量,这样我们就不会被认为是理所当然的错误弄得一团糟。

#7


3  

The perldoc has a good definition of our.

perldoc对我们的定义有很好的定义。

Unlike my, which both allocates storage for a variable and associates a simple name with that storage for use within the current scope, our associates a simple name with a package variable in the current package, for use within the current scope. In other words, our has the same scoping rules as my, but does not necessarily create a variable.

不像我的,它们都为一个变量分配存储,并将一个简单的名称与当前作用域内的存储关联起来,我们将一个简单的名称与当前包中的包变量关联起来,以便在当前范围内使用。换句话说,我们有与我相同的范围规则,但并不一定创建一个变量。

#8


2  

This is only somewhat related to the question, but I've just discovered a (to me) obscure bit of perl syntax that you can use with "our" (package) variables that you can't use with "my" (local) variables.

这只是与问题相关,但我刚刚发现了一个(对我来说)晦涩的perl语法,您可以使用“我们”(包)变量,您不能使用“my”(本地)变量。

#!/usr/bin/perl

our $foo = "BAR";

print $foo . "\n";
${"foo"} = "BAZ";
print $foo . "\n";

Output:

输出:

BAR
BAZ

This won't work if you change 'our' to 'my'.

如果你把'our'改成'my',这就行不通了。

#9


0  

print "package is: " . __PACKAGE__ . "\n";
our $test = 1;
print "trying to print global var from main package: $test\n";

package Changed;

{
        my $test = 10;
        my $test1 = 11;
        print "trying to print local vars from a closed block: $test, $test1\n";
}

&Check_global;

sub Check_global {
        print "trying to print global var from a function: $test\n";
}
print "package is: " . __PACKAGE__ . "\n";
print "trying to print global var outside the func and from \"Changed\" package:     $test\n";
print "trying to print local var outside the block $test1\n";

Will Output this:

将输出:

package is: main
trying to print global var from main package: 1
trying to print local vars from a closed block: 10, 11
trying to print global var from a function: 1
package is: Changed
trying to print global var outside the func and from "Changed" package: 1
trying to print local var outside the block 

In case using "use strict" will get this failure while attempting to run the script:

如果在尝试运行脚本时使用“使用严格”将会导致失败:

Global symbol "$test1" requires explicit package name at ./check_global.pl line 24.
Execution of ./check_global.pl aborted due to compilation errors.

#10


0  

Just try to use the following program :

试着使用下面的程序:

#!/usr/local/bin/perl
use feature ':5.10';
#use warnings;
package a;
{
my $b = 100;
our $a = 10;


print "$a \n";
print "$b \n";
}

package b;

#my $b = 200;
#our $a = 20 ;

print "in package b value of  my b $a::b \n";
print "in package b value of our a  $a::a \n";

#11


-1  

#!/usr/bin/perl -l

use strict;

# if string below commented out, prints 'lol' , if the string enabled, prints 'eeeeeeeee'
#my $lol = 'eeeeeeeeeee' ;
# no errors or warnings at any case, despite of 'strict'

our $lol = eval {$lol} || 'lol' ;

print $lol;

#12


-1  

Let us think what an interpreter actually is: it's a piece of code that stores values in memory and lets the instructions in a program that it interprets access those values by their names, which are specified inside these instructions. So, the big job of an interpreter is to shape the rules of how we should use the names in those instructions to access the values that the interpreter stores.

让我们想一下解释器实际上是什么:它是一段代码,存储内存中的值,并让程序中的指令通过它们的名称来解释访问这些值,这些值是在这些指令中指定的。因此,解释器的主要工作是确定如何使用这些指令中的名称来访问解释器存储的值的规则。

On encountering "my", the interpreter creates a lexical variable: a named value that the interpreter can access only while it executes a block, and only from within that syntactic block. On encountering "our", the interpreter makes a lexical alias of a package variable: it binds a name, which the interpreter is supposed from then on to process as a lexical variable's name, until the block is finished, to the value of the package variable with the same name.

在遇到“my”时,解释器创建一个词汇变量:解释器只能在执行块时访问的命名值,并且仅从该语法块中访问。在遇到“我们”时,解释器会生成一个包变量的词法别名:它绑定一个名称,从那时起,解释器就被假定为一个词汇变量的名称,直到该块被完成,并将其与同名的包变量的值绑定在一起。

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.

这样做的效果是,你可以假装你在使用一个词汇变量,并且绕过了对包变量完全限定的“使用严格”的规则。由于解释器在第一次使用时自动创建包变量,所以使用“我们”的副作用也可能是解释器创建一个包变量。在这种情况下,创建了两个东西:一个包变量,解释器可以从任何地方访问它,只要它被正确地指定为“使用严格”(使用它的包名和两个冒号),以及它的词法别名。

Sources:

来源:

#1


191  

Great question: How does our differ from my and what does our do?

好问题:我们与我的不同之处是什么?

In Summary:

总而言之:

Available since Perl 5, my is a way to declare:

从Perl 5开始,我是一种声明方式:

  • non-package variables, that are
  • non-package变量,,
  • private,
  • 私人的,
  • new,
  • 新的,
  • non-global variables,
  • 非全局变量,
  • separate from any package. So that the variable cannot be accessed in the form of $package_name::variable.
  • 独立于任何包。因此不能以$package_name::变量的形式访问变量。


On the other hand, our variables are:

另一方面,我们的变量是:

  • package variables, and thus automatically
  • 包变量,因此自动。
  • global variables,
  • 全局变量,
  • definitely not private,
  • 绝对不是私人的,
  • nor are they necessarily new; and they
  • 它们也不一定是新的;他们
  • can be accessed outside the package (or lexical scope) with the qualified namespace, as $package_name::variable.
  • 可以使用限定名称空间($package_name::variable)在包(或词法范围)之外访问。


Declaring a variable with our allows you to predeclare variables in order to use them under use strict without getting typo warnings or compile-time errors. Since Perl 5.6, it has replaced the obsolete use vars, which was only file-scoped, and not lexically scoped as is our.

使用我们的允许来声明一个变量是为了在使用时使用它们,而不需要输入错误警告或编译时错误。由于Perl 5.6,它已经取代了过时的使用vars,它只是文件范围的,而不是像我们的那样在词法范围内。

For example, the formal, qualified name for variable $x inside package main is $main::x. Declaring our $x allows you to use the bare $x variable without penalty (i.e., without a resulting error), in the scope of the declaration, when the script uses use strict or use strict "vars". The scope might be one, or two, or more packages, or one small block.

例如,在package main中变量$x的正式、限定名是$main::x。声明我们的$x允许您使用裸$x变量而不受惩罚(即:在声明的范围内,当脚本使用严格或使用严格的“vars”时,在声明的范围内。范围可能是一个、两个或多个包,或一个小块。

#2


58  

The PerlMonks and PerlDoc links from cartman and Olafur are a great reference - below is my crack at a summary:

来自cartman和Olafur的PerlMonks和PerlDoc是一个很好的参考,下面是我的一个总结:

my variables are lexically scoped within a single block defined by {} or within the same file if not in {}s. They are not accessible from packages/subroutines defined outside of the same lexical scope / block.

我的变量在词法作用域范围内,在一个单独的块中,如果没有在{}中,则在同一个文件中定义。它们不能从定义在同一个词法范围/块之外的包/子例程中访问。

our variables are scoped within a package/file and accessible from any code that use or require that package/file - name conflicts are resolved between packages by prepending the appropriate namespace.

我们的变量限定在包/文件中,并且可以从任何使用或要求包/文件名称冲突的代码中访问,并在适当的名称空间之前通过包来解决。

Just to round it out, local variables are "dynamically" scoped, differing from my variables in that they are also accessible from subroutines called within the same block.

为了解决这个问题,局部变量是“动态”作用域的,不同于我的变量,它们也可以从同一个块中调用的子例程中访问。

#3


43  

An example:

一个例子:

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";

#4


12  

Coping with Scoping is a good overview of Perl scoping rules. It's old enough that our is not discussed in the body of the text. It is addressed in the Notes section at the end.

应对Scoping是对Perl范围规则的一个很好的概述。这已经足够大了,我们在正文中没有讨论。它在最后的注释部分中被处理。

The article talks about package variables and dynamic scope and how that differs from lexical variables and lexical scope.

本文讨论了包变量和动态范围以及它与词法变量和词法作用域的区别。

#5


6  

my is used for local variables, where as our is used for global variables. More reading over Variable Scoping in Perl: the basics .

我的是用于局部变量,在这里我们用于全局变量。Perl中关于变量范围的更多阅读:基础知识。

#6


4  

It's an old question, but I ever met some pitfalls about lexical declarations in Perl that messed me up, which are also related to this question, so I just add my summary here:

这是个老问题,但我在Perl中遇到了一些关于词汇声明的陷阱,这些错误都和这个问题有关,所以我只在这里加上我的总结:

1. definition or declaration?

1。定义或声明?

local $var = 42; 
print "var: $var\n"; 

The output is var: 42. However we couldn't tell if local $var = 42; is a definition or declaration. But how about this:

输出是var: 42。但是,我们无法判断本地$var = 42;是一个定义或声明。但这个怎么样:

use strict;
use warnings;

local $var = 42;
print "var: $var\n";

The second program will throw an error:

第二个程序会抛出一个错误:

Global symbol "$var" requires explicit package name.

$var is not defined, which means local $var; is just a declaration! Before using local to declare a variable, make sure that it is defined as a global variable previously.

$var没有定义,这意味着本地$var;只是一个宣言!在使用local声明变量之前,请确保之前将其定义为全局变量。

But why this won't fail?

但为什么这不会失败呢?

use strict;
use warnings;

local $a = 42;
print "var: $a\n";

The output is: var: 42.

输出是:var: 42。

That's because $a, as well as $b, is a global variable pre-defined in Perl. Remember the sort function?

这是因为$a和$b是一个在Perl中预定义的全局变量。还记得排序函数吗?

2. lexical or global?

2。词汇或全球?

I was a C programmer before starting using Perl, so the concept of lexical and global variables seems straightforward to me: just corresponds to auto and external variables in C. But there're small differences:

在开始使用Perl之前,我是一个C程序员,因此,词汇和全局变量的概念对我来说很简单:只对应于C的自动和外部变量,但有一些细微的差别:

In C, an external variable is a variable defined outside any function block. On the other hand, an automatic variable is a variable defined inside a function block. Like this:

在C中,外部变量是在任何函数块外部定义的变量。另一方面,自动变量是在函数块中定义的变量。是这样的:

int global;

int main(void) {
    int local;
}

While in Perl, things are subtle:

在Perl中,事情很微妙:

sub main {
    $var = 42;
}

&main;

print "var: $var\n";

The output is var: 42, $var is a global variable even it's defined in a function block! Actually in Perl, any variable is declared as global by default.

输出是var: 42, $var是一个全局变量,即使它是在函数块中定义的!实际上,在Perl中,默认情况下,任何变量都被声明为全局变量。

The lesson is to always add use strict; use warnings; at the beginning of a Perl program, which will force the programmer to declare the lexical variable explicitly, so that we don't get messed up by some mistakes taken for granted.

教训是总是要增加使用的严格;使用警告;在Perl程序的开始,这将迫使程序员显式地声明lexical变量,这样我们就不会被认为是理所当然的错误弄得一团糟。

#7


3  

The perldoc has a good definition of our.

perldoc对我们的定义有很好的定义。

Unlike my, which both allocates storage for a variable and associates a simple name with that storage for use within the current scope, our associates a simple name with a package variable in the current package, for use within the current scope. In other words, our has the same scoping rules as my, but does not necessarily create a variable.

不像我的,它们都为一个变量分配存储,并将一个简单的名称与当前作用域内的存储关联起来,我们将一个简单的名称与当前包中的包变量关联起来,以便在当前范围内使用。换句话说,我们有与我相同的范围规则,但并不一定创建一个变量。

#8


2  

This is only somewhat related to the question, but I've just discovered a (to me) obscure bit of perl syntax that you can use with "our" (package) variables that you can't use with "my" (local) variables.

这只是与问题相关,但我刚刚发现了一个(对我来说)晦涩的perl语法,您可以使用“我们”(包)变量,您不能使用“my”(本地)变量。

#!/usr/bin/perl

our $foo = "BAR";

print $foo . "\n";
${"foo"} = "BAZ";
print $foo . "\n";

Output:

输出:

BAR
BAZ

This won't work if you change 'our' to 'my'.

如果你把'our'改成'my',这就行不通了。

#9


0  

print "package is: " . __PACKAGE__ . "\n";
our $test = 1;
print "trying to print global var from main package: $test\n";

package Changed;

{
        my $test = 10;
        my $test1 = 11;
        print "trying to print local vars from a closed block: $test, $test1\n";
}

&Check_global;

sub Check_global {
        print "trying to print global var from a function: $test\n";
}
print "package is: " . __PACKAGE__ . "\n";
print "trying to print global var outside the func and from \"Changed\" package:     $test\n";
print "trying to print local var outside the block $test1\n";

Will Output this:

将输出:

package is: main
trying to print global var from main package: 1
trying to print local vars from a closed block: 10, 11
trying to print global var from a function: 1
package is: Changed
trying to print global var outside the func and from "Changed" package: 1
trying to print local var outside the block 

In case using "use strict" will get this failure while attempting to run the script:

如果在尝试运行脚本时使用“使用严格”将会导致失败:

Global symbol "$test1" requires explicit package name at ./check_global.pl line 24.
Execution of ./check_global.pl aborted due to compilation errors.

#10


0  

Just try to use the following program :

试着使用下面的程序:

#!/usr/local/bin/perl
use feature ':5.10';
#use warnings;
package a;
{
my $b = 100;
our $a = 10;


print "$a \n";
print "$b \n";
}

package b;

#my $b = 200;
#our $a = 20 ;

print "in package b value of  my b $a::b \n";
print "in package b value of our a  $a::a \n";

#11


-1  

#!/usr/bin/perl -l

use strict;

# if string below commented out, prints 'lol' , if the string enabled, prints 'eeeeeeeee'
#my $lol = 'eeeeeeeeeee' ;
# no errors or warnings at any case, despite of 'strict'

our $lol = eval {$lol} || 'lol' ;

print $lol;

#12


-1  

Let us think what an interpreter actually is: it's a piece of code that stores values in memory and lets the instructions in a program that it interprets access those values by their names, which are specified inside these instructions. So, the big job of an interpreter is to shape the rules of how we should use the names in those instructions to access the values that the interpreter stores.

让我们想一下解释器实际上是什么:它是一段代码,存储内存中的值,并让程序中的指令通过它们的名称来解释访问这些值,这些值是在这些指令中指定的。因此,解释器的主要工作是确定如何使用这些指令中的名称来访问解释器存储的值的规则。

On encountering "my", the interpreter creates a lexical variable: a named value that the interpreter can access only while it executes a block, and only from within that syntactic block. On encountering "our", the interpreter makes a lexical alias of a package variable: it binds a name, which the interpreter is supposed from then on to process as a lexical variable's name, until the block is finished, to the value of the package variable with the same name.

在遇到“my”时,解释器创建一个词汇变量:解释器只能在执行块时访问的命名值,并且仅从该语法块中访问。在遇到“我们”时,解释器会生成一个包变量的词法别名:它绑定一个名称,从那时起,解释器就被假定为一个词汇变量的名称,直到该块被完成,并将其与同名的包变量的值绑定在一起。

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.

这样做的效果是,你可以假装你在使用一个词汇变量,并且绕过了对包变量完全限定的“使用严格”的规则。由于解释器在第一次使用时自动创建包变量,所以使用“我们”的副作用也可能是解释器创建一个包变量。在这种情况下,创建了两个东西:一个包变量,解释器可以从任何地方访问它,只要它被正确地指定为“使用严格”(使用它的包名和两个冒号),以及它的词法别名。

Sources:

来源: