我应该如何组织许多 Perl 模块?

发布于 2024-09-01 01:26:54 字数 106 浏览 4 评论 0原文

假设我在 12 个目录中有 100 个 Perl 模块。但是,查看主要 Perl 脚本,它看起来像 100 use p1 ;使用 p2 ; 等。解决此问题的最佳方法是什么?

Consider that I have 100 Perl modules in 12 directories. But, looking into the main Perl script, it looks like 100 use p1 ; use p2 ; etc. What is the to best way to solve this issue?

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

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

发布评论

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

评论(4

青衫儰鉨ミ守葔 2024-09-08 01:26:54

在我看来,您不太可能直接在主程序中使用所有 100 个模块。如果您的程序使用模块 A 中的函数,然后调用模块 B 中的函数,但主程序本身没有引用模块 B 中的任何内容,则该程序应该仅使用 A。它不应该使用 B,除非它直接调用模块 B 中的任何内容。

另一方面,如果您的主程序确实直接与所有 100 个模块通信,那么它可能太大了。识别程序中不同的功能分组,并将每个组分解为自己的模块。这样做的主要原因是这样可以使代码更易于维护、灵活和可重用,但它也会产生令人高兴的副作用,即减少主程序直接对话的模块数量,从而减少减少任何一处所需的 use 语句的数量。

(是的,我确实意识到 100 可能有点夸张,但是,如果您对代码使用的模块数量感到不舒服,那么这通常强烈表明有问题的代码试图在一个地方做太多事情,应该分解成一组模块。)

It seems unlikely to me that you're useing all 100 modules directly in your main program. If your program uses a function in module A which then calls a function from module B, but the main program itself doesn't reference anything in module B, then the program should only use A. It should not use B unless it directly calls anything from module B.

If, on the other hand, your main program really does talk directly to all 100 modules, then it's probably just plain too big. Identify different functional groupings within the program and break each of those groups out into its own module. The main reason for doing this is so that it will result in code that is more maintainable, flexible, and reusable, but it will also have the happy side-effect of reducing the number of modules that the main program talks to directly, thus cutting down on the number of use statements required in any one place.

(And, yes, I do realize that 100 was probably an exaggeration, but, if you're getting uncomfortable about the number of modules being used by your code, then that's usually a strong indication that the code in question is trying to do too much in one place and should be broken down into a collection of modules.)

兮颜 2024-09-08 01:26:54

将所有 use 语句放在一个文件中,例如 Mods.pm:

package Mods;

use Mod1;
use Mod2;
...

并将该文件包含在主脚本中:

use Mods;

Put all the use statements in one file, say Mods.pm:

package Mods;

use Mod1;
use Mod2;
...

and include the file in your main script:

use Mods;
鹿! 2024-09-08 01:26:54

我支持 eugene 的解决方案,但您可以按主题对文件中的 use 语句进行分组,例如:

package Math;

use ModMatrix;
use ModFourier;
...

当然,您应该命名模块和 mod-collections 有意义。

I support eugene's solution, but you could group the use statements in files by topic, like:

package Math;

use ModMatrix;
use ModFourier;
...

And of course you should name the modules and the mod-collections meaningful.

风尘浪孓 2024-09-08 01:26:54

按照 eugene y 的建议,将所有 use 语句放在单独的文件中可能是最好的方法。您可以通过一些元编程来最大程度地减少该模块中的输入:

package Mods;
require Exporter;
our @ISA = 'Exporter';

my @packages = qw/Mod1 Mod2 Mod3 .... /;  
     # or  map {"Mod$_"} 1 .. 100  if your modules are actually named that way

for (@packages) {
    eval "require $_" or die $@;  # 'use' means "require pkg; pkg->import()" 
    $_->import();                 # at compile time
}

our @EXPORT = grep {*{$Mods::{$_}}{CODE}} keys %Mods::; # grab imported subs
#or @EXPORT_OK

Putting all of the use statements in a separate file as eugene y suggested is probably the best approach. you can minimize the typing in that module with a bit of meta programming:

package Mods;
require Exporter;
our @ISA = 'Exporter';

my @packages = qw/Mod1 Mod2 Mod3 .... /;  
     # or  map {"Mod$_"} 1 .. 100  if your modules are actually named that way

for (@packages) {
    eval "require $_" or die $@;  # 'use' means "require pkg; pkg->import()" 
    $_->import();                 # at compile time
}

our @EXPORT = grep {*{$Mods::{$_}}{CODE}} keys %Mods::; # grab imported subs
#or @EXPORT_OK
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文