summaryrefslogtreecommitdiffstats
path: root/bin/macros/perl-cpan
blob: 0cd9bcd19b40a87817f9bd7b0b7516379f08a636 (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
#!/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";

unless (-d 'src') {
    mkdir 'src' or die "$0: mkdir src: $!"
}

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' => "src/$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 src/$file";
if ($? != 0) {
   printf STDERR "$0: failed to run perl-dist%s\n", ($! ? " ($!)" : q{});
   exit 1;
}