我們如何識別屬於給定分佈的所有模塊。我們如何識別屬於給定分佈的所有模塊
例如XML :: LibXML發行版提供了一組以下模塊 https://metacpan.org/release/XML-LibXML
我們如何通過cpan/ppm或通過任何標準的每個包來獲取此列表。
其實我們正在爲我們用Perl編寫的代碼編寫一個單元測試框架。要驗證模塊,我們需要一種方法來查找給定模塊名稱的分佈名稱。
我們如何識別屬於給定分佈的所有模塊。我們如何識別屬於給定分佈的所有模塊
例如XML :: LibXML發行版提供了一組以下模塊 https://metacpan.org/release/XML-LibXML
我們如何通過cpan/ppm或通過任何標準的每個包來獲取此列表。
其實我們正在爲我們用Perl編寫的代碼編寫一個單元測試框架。要驗證模塊,我們需要一種方法來查找給定模塊名稱的分佈名稱。
MetaCPAN API使用JSON Web服務(http://api.metacpan.org)爲此問題提供瞭解決方案。
這很容易在http://explorer.metacpan.org/
在命令行或通過網絡形式使用curl
如果你知道你正在尋找, 釋放的名稱,你可以做這樣的查詢來嘗試不同的查詢獲得模塊名稱的列表:
/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" } }
]
}
}
你也可以用"distribution": "XML-LibXML"
替代"release": "XML-LibXML-1.95"
。
如果你開始與一個模塊名稱,首先需要確定的名稱發佈,試試這個:
/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 模塊,雖然我沒有使用它自己。
由於這只是一個Web請求,您可以使用LWP或任何其他HTTP模塊。
你可能ALO要檢查出 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;
如需更多幫助,請訪問#metacpan
上irc.perl.org
, 或https://github.com/CPAN-API/cpan-api/wiki檢查出維基。
如果你多說一點你正在做的事情和/或試圖達到目的,你可能會找到其他方法來做到這一點。
這是來自Tim Bunce的一篇詳細的文章,他希望以更大的規模來完成這項工作,從而使他創建Dist :: Surveyor:http://blog.timbunce.org/2011/11/16/whats-實際安裝在那個庫/ – 2012-03-14 16:56:29
感謝蘭迪的回覆。 – rpg 2012-03-19 06:36:17