我应该如何组织许多 Perl 模块?
假设我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在我看来,您不太可能直接在主程序中
使用
所有 100 个模块。如果您的程序使用模块 A 中的函数,然后调用模块 B 中的函数,但主程序本身没有引用模块 B 中的任何内容,则该程序应该仅使用 A
。它不应该使用 B
,除非它直接调用模块 B 中的任何内容。另一方面,如果您的主程序确实直接与所有 100 个模块通信,那么它可能太大了。识别程序中不同的功能分组,并将每个组分解为自己的模块。这样做的主要原因是这样可以使代码更易于维护、灵活和可重用,但它也会产生令人高兴的副作用,即减少主程序直接对话的模块数量,从而减少减少任何一处所需的
use
语句的数量。(是的,我确实意识到 100 可能有点夸张,但是,如果您对代码
使用
的模块数量感到不舒服,那么这通常强烈表明有问题的代码试图在一个地方做太多事情,应该分解成一组模块。)It seems unlikely to me that you're
use
ing 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 onlyuse A
. It should notuse 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
use
d 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.)将所有
use
语句放在一个文件中,例如 Mods.pm:并将该文件包含在主脚本中:
Put all the
use
statements in one file, say Mods.pm:and include the file in your main script:
我支持 eugene 的解决方案,但您可以按主题对文件中的
use
语句进行分组,例如:当然,您应该命名模块和 mod-collections 有意义。
I support eugene's solution, but you could group the
use
statements in files by topic, like:And of course you should name the modules and the mod-collections meaningful.
按照 eugene y 的建议,将所有 use 语句放在单独的文件中可能是最好的方法。您可以通过一些元编程来最大程度地减少该模块中的输入:
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: