加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl – 如何创建一个为用户导入许多模块的模块?

发布时间:2020-12-16 06:24:13 所属栏目:大数据 来源:网络整理
导读:我有一个相当复杂的数据结构,我已经在Perl中实现了.这已被分解为大约20个班级.基本上,只要您想使用其中一个类,就需要使用所有这些类. 现在,如果有人想要使用这种数据结构,他们需要做类似的事情: use Component::Root;use Component::Foo;use Component::Bar
我有一个相当复杂的数据结构,我已经在Perl中实现了.这已被分解为大约20个班级.基本上,只要您想使用其中一个类,就需要使用所有这些类.

现在,如果有人想要使用这种数据结构,他们需要做类似的事情:

use Component::Root;
use Component::Foo;
use Component::Bar;
use Component::Baz;
use Component::Flib;
use Component::Zen;
use Component::Zen::Foo;
use Component::Zen::Bar;
use Component::Zen::Baz;
... # 15 more of these...
use Component::Last;

能够操纵它的所有部分.如何编写一个为用户执行此操作的模块,因此他们所要做的就是

use Component;

导入所有其他模块?

在这种特殊情况下,模块都是类,没有导出.

解决方法

如果这些只是类(即它们在使用它们时不导出任何函数或变量),那么真正重要的是它们已被加载.

只需创建Component.pm:

package Component;

our $VERSION = '1.00';

use Component::Root;
use Component::Foo;
use Component::Bar;
use Component::Baz;
use Component::Flib;
use Component::Zen;
use Component::Zen::Foo;
use Component::Zen::Bar;
use Component::Zen::Baz;
... # 15 more of these...
use Component::Last;

1; # Package return value

您不需要Exporter或类似的东西.

但是,将这些use语句放入根节点的类或创建数据结构的模块中可能更有意义,而不是使用只有use语句的模块.也就是说,人们会想说:

use Component::Root;
my $root = Component::Root->new(...);

要么

use Component qw(build_structure);
my $root = build_structure(...);

取决于您的数据结构通常如何创建.人们写作可能有点令人困惑:

use Component;
my $root = Component::Root->new(...);

但它实际上取决于你的API是什么样的.如果有许多人可能正在调用new,那么使用Component可能就好了.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读