我们如何识别属于给定分布的所有模块.
例如,XML :: LibXML发行版提供了一组以下模块 https://metacpan.org/release/XML-LibXML
我们如何通过cpan/ppm或每个包的任何标准获得此列表.
实际上我们正在为用Perl编写的代码编写一个单元测试框架.要验证模块,我们需要一种方法来查找给定模块名称的分发名称.
小智 6
MetaCPAN API通过JSON Web服务(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" } }
]
}
}
Run Code Online (Sandbox Code Playgroud)
您也可以替换"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" } }
]
}
}
Run Code Online (Sandbox Code Playgroud)
该查询语法是ElasticSearch DSL,因为api使用ElasticSearch来索引数据.
要从perl进行查询,有一个MetaCPAN :: API 模块,虽然我自己没有使用它.
由于它只是一个Web请求,您可以使用LWP或任何其他HTTP模块.
您可能还想查看 ElasticSearch和 ElasticSearch :: SearchBuilder 模块,它们提供了一个更完整的perl接口来查询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;
Run Code Online (Sandbox Code Playgroud)
如需更多帮助,请访问#metacpan上irc.perl.org,或检查出在维基https://github.com/CPAN-API/cpan-api/wiki.
如果你再解释一下你正在做什么和/或试图实现,你可能会找到其他方法来做到这一点.
| 归档时间: |
|
| 查看次数: |
174 次 |
| 最近记录: |