summaryrefslogtreecommitdiffstats
path: root/module-to-dist.pl
blob: 9d099d3ab63163d8fd94ac9e3ec9376b2d26f5f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/perl
use warnings;
use strict;
use v5.10;

package Modules;

use HTTP::Tiny qw();

sub cpan_provider
{
    my ($module) = @_;
    my $url = "http://cpanmetadb.plackperl.org/v1.0/package/$module";
    my $http = HTTP::Tiny->new;
    my $resp = $http->get($url);
    return undef unless $resp->{'success'};

    my ($cpanpath) = $resp->{'content'} =~ /^distfile: (.*)$/m
        or return undef;

    my $dist = $cpanpath;
    $dist =~ s{\A.+/}{};    # remove author directory
    $dist =~ s{-[^-]+\z}{}; # remove version and extension
	$dist =~ s/-/::/g;
    return ($dist eq 'perl' ? undef : $dist);
}

package main;

my %seen = ();
my @modules;

if (@ARGV == 0) {
	# no args? read from stdin
	@modules = <>;
} else {
	@modules = @ARGV;
}

for my $module (@modules) {
	chomp($module);
	next if ($module eq "");

	my $dist = Modules::cpan_provider $module;

	next unless $dist; # ignore undef
	next if $seen{$dist}++; # ignore duplicates

	say $dist;
}