The MetaCPAN API provides a solution to this problem with a JSON web service (http://api.metacpan.org).
It's easy to try different queries using curl
on the command line or via the web form at http://explorer.metacpan.org/
If you know the name of the release you're searching for,
you can do a query like this to get a list of module names:
/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" } }
]
}
}
You could also substitute "release": "XML-LibXML-1.95"
with "distribution": "XML-LibXML"
.
If you are starting with a module name and need to determine the name of the release first, try this:
/module/_search
{
"query" : { "match_all" : {} },
"size" : 1000,
"fields" : [ "release", "distribution" ],
"filter" : {
"and": [
{ "term" : { "module.name" : "XML::LibXML" } },
{ "term" : { "status" : "latest" } }
]
}
}
That query syntax is the ElasticSearch DSL since the api uses ElasticSearch to index the data.
To do query from perl there is a MetaCPAN::API
module, though I have not used it myself.
Since it's just a web request you can use LWP or any other HTTP module.
You might alo want to check out the
ElasticSearch and
ElasticSearch::SearchBuilder
modules which provide a more full perl interface to querying an ElasticSearch database.
Here's a full example in perl using LWP:
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;
For more help visit #metacpan
on irc.perl.org
,
or check out the wiki at https://github.com/CPAN-API/cpan-api/wiki.
If you explain a little more what you are doing and/or trying to achive you might find other ways to do it.