在Perl中,使用和加载模块有什么区别? [英] In Perl, what is the difference between use and require for loading a module?

查看:81
本文介绍了在Perl中,使用和加载模块有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

use My::Modulerequire My::Module有什么区别?

推荐答案

use 函数:

use ModuleName;

等效于使用 require 函数的以下代码:

BEGIN {
    require ModuleName;
    ModuleName->import;
}

die 尝试.然后调用模块的import函数. import函数可以执行各种操作,但是通常会将函数加载到use d函数的名称空间中(通常使用

The BEGIN block causes this code to run as soon as the parser sees it. The require loads the module or dies trying. And then the import function of the module is called. The import function may do all sorts of things, but it is common for it to load functions into the namespace that used it (often with the Exporter module).

重要的是要注意在这种情况下将不会调用导入:

It is important to note that import will not be called in this case:

use ModuleName ();

在这种情况下,它等同于

In that case, it is equivalent to

BEGIN {
    require ModuleName;
}

这篇关于在Perl中,使用和加载模块有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆