summaryrefslogtreecommitdiffstats
path: root/module-to-dist.pl
blob: 8783bf8f45040b3d14c6e62c620b0b0d7f7c5331 (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/-/::/;
    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;
}