如何从Perl中的部分名称空间动态发现包? [英] How do I dynamically discover packages from a partial namespace in perl?

查看:78
本文介绍了如何从Perl中的部分名称空间动态发现包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目录结构如下:

Foo :: Bar :: Baz :: 1 Foo :: Bar :: Baz :: 2等

Foo::Bar::Baz::1 Foo::Bar::Baz::2 etc

我可以从类似的列表中列出软件包吗?

Can I list the packages from something like:

use Foo::Bar::Baz;

谢谢!

更清楚地说明模块是什么.

Made it more clear what the modules are.

推荐答案

请明确一下,您是否正在使用随机Perl代码查看随机包?

Just to be clear, are you looking at random packages in random Perl code?

或者对于Perl 模块,例如"a/b/c/d1.pm"与模块"a :: b :: c :: d1"?

Or for Perl modules, e.g. "a/b/c/d1.pm" with module "a::b::c::d1"?

在任何一种情况下,您都不能使用单个"use"语句来全部加载它们.

In either case, you can not use a single "use" statement to load them all.

您需要做的是使用globFile::Find查找所有适当的文件.

What you need to do is to find all the appropriate files, using either glob or File::Find.

在第一种情况下(模块),您可以通过require加载每个文件,或者将文件名转换为模块名称(s#/#::#g; s#\.pm$##;)并分别在每个模块上调用use来加载它们.

In the first case (modules), you can then load them either by require-ing each file, OR by converting filename into module name (s#/#::#g; s#\.pm$##;) and calling use on each module individually.

对于嵌套在随机Perl文件中的实际软件包,这些软件包可以是:

As far as actual packages nested in random Perl files, those packages can be:

  • 通过为/^package (.*);/

实际上是通过对每个文件执行require $file来加载的.

Actually loaded by executing require $file for each file.

在这种情况下,请注意,a/b/c/1.pl中每个软件包的软件包名称将与"a :: b :: c"相关-例如它们可以由文件作者"p1","a :: p1"或"a :: b :: c :: p1_something"命名.

In this case, please note that the package name for each of those packages in a/b/c/1.pl will NOT need to be related to "a::b::c" - e.g. they CAN be named by the file author "p1", "a::p1" or "a::b::c::p1_something".

这篇关于如何从Perl中的部分名称空间动态发现包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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