perl 模块从未导入的包中调用方法
今天我惊讶地发现我的一个 perl 模块使用了另一个模块,但没有导入它。
但是,当我意识到这从未引起任何问题时,我感到震惊!< /b>
代码类似于:
package This;
# no import for OTHER !!
sub new {
... implementation ...
my $something = OTHER->new(@arguments); # no error!
... more implementation ...
}
那么什么给出? This
的导入中没有一个 import OTHER
——这是我做的第一件事检查过。
是否如果导入This
的代码也导入了OTHER
,那么OTHER
也可用于This
?
问题澄清:
X
、Y
和Z
是模块。- 在 X 中:
使用 Y;
- 在 X 中:
使用 Z;
Y
不使用
Z
Y
是否可以访问Z
中的函数和方法>(反之亦然)?
I was appalled to discover today that one of my perl modules uses another module, but doesn't import it.
However, I was shocked when I realized that this has never caused any problems!
The code is something like:
package This;
# no import for OTHER !!
sub new {
... implementation ...
my $something = OTHER->new(@arguments); # no error!
... more implementation ...
}
So what gives? None of This
's imports import OTHER
-- that was the first thing I checked.
Could it be that if the code that imports This
also imports OTHER
, OTHER
is available to This
?
Clarification of question:
X
,Y
, andZ
are modules.- in X:
use Y;
- in X:
use Z;
Y
does notuse
Z
Does Y
have access to the functions and methods in Z
(and vice versa)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您对“导入”一词的使用有点误导。
是的,或多或少。
使用
模块只是将其加载到当前脚本中;所有use
模块都加载到同一个脚本中,因此可以互相看到(前提是它们以正确的顺序use
)。但真正的导入——模块实际上导出的东西,以便它们被复制到
使用
模块的命名空间中-仅被复制到use
模块的命名空间中。例如,考虑这个 Perl 脚本:
它
使用
Time::HiRes
模块,并告诉它导出Time::HiRes::time
> 进入当前命名空间(主命名空间),覆盖内置时间
。 (Time::HiRes::time
类似于time
,但它具有亚秒级分辨率;它会给出类似1323048440.80571
的内容,而不是只是1323048440
。)因此包Foo
可以看到Time::HiRes
以及其中的任何内容Time::HiRes
,如其时间
;但它必须通过显式编写Time::HiRes::time
来明确指定。(注意:为了简化描述,我将上面的内容写为单个脚本,但是当在
Foo.pm
中定义Foo
并使用加载时,也会发生同样的情况使用 Foo;
。)Your use of the term "import" is a bit misleading.
Yes, more or less.
use
ing a module just loads it into the current script; alluse
d modules are loaded into the same script, so can see each other (provided they'reuse
d in the right order).But true imports — the things that a module actually exports, so that they're copied into the
use
ing module's namespace — will only be copied into theuse
ing module's namespace.For example, consider this Perl script:
It
use
s theTime::HiRes
module, and tells it to exportTime::HiRes::time
into the current namespace (the main namespace), overwriting the built-intime
. (Time::HiRes::time
is liketime
, but it has sub-second resolution; it'll give something like1323048440.80571
instead of just1323048440
.) So packageFoo
can seeTime::HiRes
, and anything withinTime::HiRes
, such as itstime
; but it has to explicitly specify that, by explicitly writingTime::HiRes::time
.(Note: I wrote the above as a single script, for simplicity of description, but the same thing happens when
Foo
is defined inFoo.pm
and loaded usinguse Foo;
.)是的,至少当您使用间接包和对象语法时。从像
Perl 这样的调用可以解析完全限定的子例程名称(在上述两种情况下都是
&OtherPackage::method
)并从任何地方调用子例程。Yes, at least when you use indirect package and object syntax. From calls like
Perl can resolve a fully qualified subroutine name (
&OtherPackage::method
in both of the above cases) and invoke the subroutine from anywhere.