summaryrefslogtreecommitdiffstats
path: root/bin/macros/perl-cpan
blob: cc87c4d1d1c0a3bf31a324ddc31d1ddb7c984fd3 (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
#!/usr/bin/env perl

use warnings 'FATAL' => 'all';
use strict;

use LWP::UserAgent;
use IO::Handle; # for autoflush

my $dist = shift or die "Usage: $0 [CPAN dist name]\n";
my $url  = "http://search.cpan.org/dist/$dist";
my $ua   = LWP::UserAgent->new();
my $resp = $ua->get($url);

die "$0: GET $url failed: ", $resp->status_line, "\n"
    unless $resp->is_success;

$resp = $resp->content;
my ($href) = $resp =~ m{\[<a href="([^"]+)">Download</a>\]}
    or die "$0: no download link found at $url\n";

my $file = $href;
$file =~ s{\A.*/}{};
$href = "http://search.cpan.org" . $href;

STDERR->autoflush(1);
print STDERR "Downloading $file... ";
$resp = $ua->get($href, ':content_file' => $file);
die "$0: download of $file failed: ", $resp->status_line
    unless $resp->is_success;
print STDERR "OK\n";

print "+ url $url\n";
print "+ source $href\n";
system "perl-dist $file";
if ($? != 0) {
   printf STDERR "$0: failed to run perl-dist%s\n", ($! ? " ($!)" : q{});
   exit 1;
}