为什么 LWP::UserAgent 是通过 require LWP::UserAgent 导入的,而不是 use LWP::UserAgent?

发布于 2024-11-17 12:48:22 字数 210 浏览 1 评论 0原文

我对这种语言还很陌生,但我之前一直使用 use 导入特定模块,

为什么 LWP::UserAgent 使用 require执行来自 perldoc LWP::UserAgent 的工作:

require LWP::UserAgent;

I'm pretty new to this language but I've been using use to import a specific module before,

why LWP::UserAgent uses require to do the job as from perldoc LWP::UserAgent:

require LWP::UserAgent;

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

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

发布评论

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

评论(3

萌面超妹 2024-11-24 12:48:22
use LWP::UserAgent;

BEGIN {
    require LWP::UserAgent;
    import LWP::UserAgent;
}

如果 require LWP::UserAgent; 可接受,则表明 import 对 LWP::UserAgent 没有任何作用。也许文档使用 require 的目的是巧妙地暗示这一点?

require LWP::UserAgent;use LWP::UserAgent; 之间的唯一区别在于 require 的执行时间。对于前者,它发生在整个文件编译之后。对于后者,一旦该语句被编译,它就会发生。实际上,面向对象的模块没有太大区别。

就我个人而言,我使用

use LWP::UserAgent qw( );

That's the same 与

BEGIN {
    require LWP::UserAgent;
}

That way,我保证不会导入任何我不想要的东西,并且我使用我用于其他模块的熟悉的 use

use LWP::UserAgent;

is the same as

BEGIN {
    require LWP::UserAgent;
    import LWP::UserAgent;
}

If require LWP::UserAgent; is acceptable, that goes to show that import does nothing for LWP::UserAgent. Maybe the point of the documentation's use of require is to subtly imply this?

The only difference between require LWP::UserAgent; and use LWP::UserAgent; is thus when require is executed. For the former, it happens after the entire file has been compiled. For the latter, it occurs as soon as that statement has been compiled. In practical terms, there's no much difference for object-oriented modules.

Personally, I use

use LWP::UserAgent qw( );

That's the same as

BEGIN {
    require LWP::UserAgent;
}

That way, I'm guaranteed not to import anything I don't want, and I use the familiar use I use for other modules.

白鸥掠海 2024-11-24 12:48:22

require Module::Nameuse 具有相同的效果,仅在运行时,而不是编译时。当您想有条件地需要一个模块时,这有时是有利的。我认为文档没有任何特殊原因说 require 而不是 use

require Module::Name has the same effect as use, only at run-time, not compile-time. This is sometimes advantageous when you want to conditionally require a module. I don't think there's any particular reason for the doc to say require instead of use.

楠木可依 2024-11-24 12:48:22

对于来自谷歌搜索并寻找此解决方案的人:

对于 Message Error: LWP::UserAgent not found at ./apache_accesses line 86。

解决方案:
apt-get 安装 libwww-perl

For people who come from google search and looking this solutions:

For Message Error: LWP::UserAgent not found at ./apache_accesses line 86.

Solution:
apt-get install libwww-perl

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