2012-03-14 48 views

回答

6

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要檢查出 ElasticSearchElasticSearch::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; 

如需更多幫助,請訪問#metacpanirc.perl.org, 或https://github.com/CPAN-API/cpan-api/wiki檢查出維基。

如果你多說一點你正在做的事情和/或試圖達到目的,你可能會找到其他方法來做到這一點。

+0

這是來自Tim Bunce的一篇詳細的文章,他希望以更大的規模來完成這項工作,從而使他創建Dist :: Surveyor:http://blog.timbunce.org/2011/11/16/whats-實際安裝在那個庫/ – 2012-03-14 16:56:29

+0

感謝蘭迪的回覆。 – rpg 2012-03-19 06:36:17

相關問題