English 中文(简体)
我和我们的区别? (在同一档案中申报)
原标题:difference between my and our ? (declared in the same file)
  • 时间:2012-05-23 12:27:54
  •  标签:
  • perl

Before writing this program,I thought that our is a package scope variable and my is a file scope variable.But,After did that program,I am get confused.

我的节目是,

#!/usr/bin/perl

use strict;
use warnings;

package one; 
our $val = "sat";
my $new = "hello";
print "ONE:val =>$val 
";
print "ONE:new =>$new 

";

package two;
print "TWO:val =>$val 
";
print "TWO:new =>$new 
";

产出数

ONE:val =>sat 
ONE:new =>hello 

TWO:val =>sat 
TWO:new =>hello 

那么,my our 之间有什么区别? 两者是相同还是有区别?

最佳回答

如你所见, my our 都具有法律效果。

my 创建了一个词汇范围变量。

our 创建了一个软件包变量的词汇范围 < pargenéñem>alias 。

仅仅因为你说 package 并不改变词汇范围,所以您的 $val 仍然是 $one:: val 的别名,即使看到 package two 语句后也是如此。

如果您没有近距离看到卷曲, 您还没有完成您的范围 。 (或者 EOF 或字符串 < code> eval 中的字符串结尾 ) 。

问题回答

my restrict the variables access to the innermost block in which they were declared. If there is no block, they are file-scoped.

our instead associates a simple name with a package variable in the current package , so it is declared at the package level and linked to the package name. our tries to help out by letting you use package variables without adding the package name.

package pack;
our $variable;    # These are the same
$pack::variable;  # These are the same

our 变量与 C s 静态变量类似,但不同之处在于,在函数中以 our 表示的变量如果与变量完全合格的名称一起命名,在函数外仍然可以访问。

但大多数的`强'我`/强'都属于法律范畴,而`强'我们的

简而言之,两种类型之间的区别是:

Global variables

任何代码 任何地方 都可以改变他们的价值观

Lexical variables

变量端的寿命随代码区块的结尾而加上它们, 在它们的值是收集的垃圾之后, 这些变量的寿命被包含在内。 这些变量只能在声明的区块内访问 。


< strong > 要回答您具体的示例问题 : 试图将第二包声明( 第二包) 移到另一个文件, 您将会看到“ strong > my 和 < streng> our ... 之间的区别

必须区分 visuibility 毕生时间

使用 < code> our 或 < code> my 宣布变量的 < em> visibility 是相同的。 您可以在文件首个附加符或结尾之前在声明之后的任何地方使用名称 。

请注意, 这不适用于 完全合格的 变量名称, 这些变量名称不需要声明, 可以在任何地方访问。 无需声明任何我可以指定给软件包变量的东西 。

$pack::three = 3;

在任何软件包中的任意位置使用该软件包。我甚至不必声明 pack 软件包。但如果我写

package pack;
our $three;

我为 $pack 生成了一个缩略的 alias :: 3 ,我可以在相同的词汇范围内使用,就像我可以在同一地方使用一个 my 变量:在文件的附加支架或结尾之前。

这些软件包变量总是可以从程序执行的一开始就可用。就像您总是可以指定给新软件的散列元素一样,它也会一直存在 — 它们的 生命周期 是无穷无尽的。事实上,所有意图和目的的软件包变量 散列元素。

另一方面,用 my 宣布的词汇变量在宣布点被创建,一旦超出范围而没有任何参考文献,则销毁。因此,除非您引用该变量,否则其 生命周期 与它的 可视性 相同。在循环中,一个 my 声明导致为每个执行循环而创建和销毁一个新的变量。

在您的代码中,您为软件包变量 $val 创建了一个别名 $val , 变量 $one: val 和一个词汇变量 $$ new 。 代码块中也不存在, 因此文件末尾都可见。 package two 在这里没有任何效果, 但如果您写了 our $val < /code> , 那么第二个软件包的语句将会更改别名 $val , 以表示 $2: val

我希望这有帮助。





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...