perl 模块从未导入的包中调用方法

发布于 2024-12-19 15:02:16 字数 906 浏览 0 评论 0原文

今天我惊讶地发现我的一个 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


问题澄清:

  • XYZ 是模块。
  • 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, and Z are modules.
  • in X: use Y;
  • in X: use Z;
  • Y does not use Z

Does Y have access to the functions and methods in Z (and vice versa)?

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

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

发布评论

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

评论(2

只有影子陪我不离不弃 2024-12-26 15:02:16

您对“导入”一词的使用有点误导。

Y 是否可以在不导入 Z 的情况下访问它(反之亦然)?

是的,或多或少。 使用模块只是将其加载到当前脚本中;所有use模块都加载到同一个脚本中,因此可以互相看到(前提是它们以正确的顺序use)。

但真正的导入——模块实际上导出的东西,以便它们被复制到使用模块的命名空间中-被复制到use模块的命名空间中。

例如,考虑这个 Perl 脚本:

use Time::HiRes 'time';

package Foo;

sub time1() { return time(); } # calls the built-in time() function
sub time2() { return Time::HiRes::time(); }


package main;

print Foo::time1(), "\n"; # prints (e.g.) 1323048440
print Foo::time2(), "\n"; # prints (e.g.) 1323048440.80571
print time(), "\n";       # prints (e.g.) 1323048440.8061

使用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.

Does Y have access to Z without importing it (and vice versa)?

Yes, more or less. useing a module just loads it into the current script; all used modules are loaded into the same script, so can see each other (provided they're used in the right order).

But true imports — the things that a module actually exports, so that they're copied into the useing module's namespace — will only be copied into the useing module's namespace.

For example, consider this Perl script:

use Time::HiRes 'time';

package Foo;

sub time1() { return time(); } # calls the built-in time() function
sub time2() { return Time::HiRes::time(); }


package main;

print Foo::time1(), "\n"; # prints (e.g.) 1323048440
print Foo::time2(), "\n"; # prints (e.g.) 1323048440.80571
print time(), "\n";       # prints (e.g.) 1323048440.8061

It uses the Time::HiRes module, and tells it to export Time::HiRes::time into the current namespace (the main namespace), overwriting the built-in time. (Time::HiRes::time is like time, but it has sub-second resolution; it'll give something like 1323048440.80571 instead of just 1323048440.) So package Foo can see Time::HiRes, and anything within Time::HiRes, such as its time; but it has to explicitly specify that, by explicitly writing Time::HiRes::time.

(Note: I wrote the above as a single script, for simplicity of description, but the same thing happens when Foo is defined in Foo.pm and loaded using use Foo;.)

挥剑断情 2024-12-26 15:02:16

是的,至少当您使用间接包和对象语法时。从像

OtherPackage->method()
$anOtherPackageObject->method()

Perl 这样的调用可以解析完全限定的子例程名称(在上述两种情况下都是&OtherPackage::method)并从任何地方调用子例程。

Yes, at least when you use indirect package and object syntax. From calls like

OtherPackage->method()
$anOtherPackageObject->method()

Perl can resolve a fully qualified subroutine name (&OtherPackage::method in both of the above cases) and invoke the subroutine from anywhere.

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