gpt4 book ai didi

perl - 我们如何识别属于给定分布的所有模块

转载 作者:行者123 更新时间:2023-12-01 05:00:00 25 4
gpt4 key购买 nike

我们如何识别属于给定分布的所有模块。

例如XML::LibXML 发行版提供了一组以下模块 https://metacpan.org/release/XML-LibXML

我们如何通过 cpan/ppm 或通过每个包的任何标准获得此列表。

实际上,我们正在为用 Perl 编写的代码编写单元测试框架。为了验证模块,我们需要一种方法来找到给定模块名称的分发名称。

最佳答案

MetaCPAN API 使用 JSON 网络服务 (http://api.metacpan.org) 提供了解决此问题的方法。

在命令行上使用 curl 或通过位于 http://explorer.metacpan.org/ 的 Web 表单可以轻松尝试不同的查询

如果您知道要搜索的版本的名称,您可以执行这样的查询以获取模块名称列表:

/module/_search
{
"query" : { "match_all" : {} },
"size" : 1000,
"fields" : [ "module.name" ],
"filter" : {
"and": [
{ "term" : { "module.authorized" : true } },
{ "term" : { "module.indexed" : true } },
{ "term" : { "release" : "XML-LibXML-1.95" } },
{ "term" : { "status" : "latest" } }
]
}
}

您也可以将 "release": "XML-LibXML-1.95" 替换为 "distribution": "XML-LibXML"

如果您以模块名称开头并且需要先确定发布名称,请尝试以下操作:

/module/_search
{
"query" : { "match_all" : {} },
"size" : 1000,
"fields" : [ "release", "distribution" ],
"filter" : {
"and": [
{ "term" : { "module.name" : "XML::LibXML" } },
{ "term" : { "status" : "latest" } }
]
}
}

该查询语法是 ElasticSearch DSL,因为该 API 使用 ElasticSearch 来索引数据。

要从 perl 进行查询,有一个 MetaCPAN::API模块,虽然我自己没有使用过它。

因为这只是一个网络请求,您可以使用 LWP或任何其他 HTTP 模块。

你可能也想看看 ElasticSearchElasticSearch::SearchBuilder提供更完整的 perl 接口(interface)来查询 ElasticSearch 数据库的模块。

这是一个使用 LWP 的 perl 完整示例:

use JSON qw( encode_json decode_json );
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $res = $ua->post("http://api.metacpan.org/module/_search",
Content => encode_json({
query => { match_all => {} },
size => 1000,
# limit reponse text to just the module names since that's all we want
fields => ['module.name'],
filter => {
and => [
{ term => { "module.authorized" => 1 } },
{ term => { "module.indexed" => 1 } },
{ term => { "distribution" => "XML-LibXML" } },
{ term => { "status" => "latest" } }
]
}
})
);
my @modules =
# this can be an array (ref) of module names for multiple packages in one file
map { ref $_ ? @$_ : $_ }
# the pieces we want
map { $_->{fields}{'module.name'} }
# search results
@{ decode_json($res->decoded_content)->{hits}{hits} };
print join "\n", sort @modules;

如需更多帮助,请访问 irc.perl.org 上的 #metacpan,或在 https://github.com/CPAN-API/cpan-api/wiki 查看 wiki .

如果您多解释一下您正在做的事情和/或试图实现的目标,您可能会找到其他方法来做到这一点。

关于perl - 我们如何识别属于给定分布的所有模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9696717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com