为什么变量用“our”声明?跨文件可见?

发布于 2024-09-17 08:58:20 字数 234 浏览 6 评论 0原文

来自“我们的”perldoc

our 与 my 具有相同的作用域规则,但不一定创建变量。

这意味着用 our 声明的变量不应该跨文件可见,因为 file 是最大的词法范围。但事实并非如此。为什么?

From the "our" perldoc:

our has the same scoping rules as my, but does not necessarily create a variable.

This means that variables declared with our should not be visible across files, because file is the largest lexical scope. But this is not true. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦断已成空 2024-09-24 08:58:20

您可以考虑our为包全局变量创建一个词法范围的别名。包全局变量可以从任何地方访问;这就是它们全球化的原因。但是 our 创建的名称仅在 our 声明的词法范围内可见。

package A;
use strict;
{
  our $var; # $var is now a legal name for $A::var
  $var = 42; # LEGAL
}

say $var; # ILLEGAL: "global symbol $var requires explicit package name"
say $A::var; # LEGAL (always)

{
  our $var; # This is the same $var as before, back in scope
  $var *= 2; # LEGAL
  say $var; # 84
}

You can consider our to create a lexically-scoped alias to a package global variable. Package globals are accessible from everywhere; that's what makes them global. But the name created by our is only visible within the lexical scope of the our declaration.

package A;
use strict;
{
  our $var; # $var is now a legal name for $A::var
  $var = 42; # LEGAL
}

say $var; # ILLEGAL: "global symbol $var requires explicit package name"
say $A::var; # LEGAL (always)

{
  our $var; # This is the same $var as before, back in scope
  $var *= 2; # LEGAL
  say $var; # 84
}
允世 2024-09-24 08:58:20

您已经有了一个很好的答案,但这也许也会有帮助。

our 声明结合了 myuse vars 的各个方面。它的功能与 use vars 类似,因为它声明了包变量;但是,以这种方式声明的变量具有词法作用域,并且不能在声明它们的作用域之外访问(除非您使用变量的完全限定名称)。此外,用 our 声明的变量在其整个词法范围内都是可见的,甚至跨包边界

这是我不久前添加到 Perl 笔记中的一张表。例如,请参阅 这个答案

              Scope/     Package
              Namespace  Variable    Private    New
---------------------------------------------------
my            Lexical    No          Yes        Yes
our           Lexical    Yes         No         No
use vars      Package    Yes         No         No

You have a good answer already, but perhaps this will be helpful as well.

The our declaration combines aspects of my and use vars. It functions similarly to use vars in that it declares package variables; however, variables declared in this way are lexically scoped and cannot be accessed outside the scope in which they were declared (unless you use the fully qualified name of the variable). In addition, a variable declared with our is visible across its entire lexical scope, even across package boundaries.

Here's a table that I added to my Perl notes a while back. For an example, see this SO answer.

              Scope/     Package
              Namespace  Variable    Private    New
---------------------------------------------------
my            Lexical    No          Yes        Yes
our           Lexical    Yes         No         No
use vars      Package    Yes         No         No
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文