处理相互使用的模块的最佳方法是什么?

发布于 2024-09-06 01:48:30 字数 1431 浏览 6 评论 0原文

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

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

发布评论

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

评论(3

鹊巢 2024-09-13 01:48:30

如果您希望拥有相互依赖的模块,实现此目的的一个简单方法是使用具有完全限定名称的后期绑定子例程调用(换句话说,在子调用上使用括号)。原型不起作用,但 Perl 不会关心 Somepackage::mysub() 是否已定义,直到您实际尝试调用它为止。当我编写相互依赖的模块时,我通常将它们保存在同一个文件中,这通过完全避免导入来简化情况。

此外,无需在 BEGIN 块中定义子项,因为 sub name {...}BEGIN {*name = sub {...}} 相同代码>

If you want to have mutually dependant modules, an easy way to make that work is to use late binding subroutine calls with fully qualified names (in other words, use parens on the sub call). Prototypes wont work, but perl wont care that Somepackage::mysub() isnt defined until you actually try to call it. When I am writing mutually dependant modules, I usually keep them in the same file which simplifies the situation by avoiding imports entirely.

Also, there is no need to define a sub in a BEGIN block since sub name {...} is the same as BEGIN {*name = sub {...}}

情绪 2024-09-13 01:48:30

如果两个模块相互依赖,那么设计就有问题。如果我是你,我会考虑重构我的模块。

If two modules are interdependent then there is some problem with the design. If i were you i'd think about refactoring my modules.

千里故人稀 2024-09-13 01:48:30

您可以有两个模块,每个模块都调用另一个模块,因为这些调用是在运行时完成的,到那时“所有内容”都将被加载。然而,(显然)这意味着您需要在不崩溃的情况下进入运行时阶段。一种常见的方法是使用一些其他模块(早于这两个模块中的任何一个),使用它们。删除直接 use 语句的直接结果是,您将丢失这些导入,但是将符号从一个模块导入到另一个模块无论如何都不是一个好主意。

看看您编写的代码,我非常惊讶您提出了如此多的“新”数据处理方法,以至于您觉得有必要将它们放入单独的库中。您是否看过标准库 Hash::UtilList::UtilList::MoreUtils?我建议您使用标准库,以更惯用的 Perlish 方式从库和代码中退出。

You can have two modules that each call into the other, because those calls are done at runtime and by then "everything" will have been loaded. However, (obviously) that means that you need to get to the runtime phase without blowing up. One common approach would be to have some other module, included earlier than either of these two, use them both. As a direct result of removing the direct use statements, you will lose those imports, but importing symbols from one module to another is not such a good idea anyway.

Looking at the code you've written, I'm pretty surprised that you've come up with so many "new" ways of processing data that you feel a need to pull these out into separate libraries. Have you looked at the standard libraries Hash::Util, List::Util, and List::MoreUtils? I would suggest you step back from your libraries and code in a more idiomatic Perlish way, using the standard libraries.

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