summaryrefslogtreecommitdiffstats
path: root/bin/macros/perl-cpan
blob: a28d85260a75f835eaeeee9453a3ddc8fa41347a (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
#!/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";
print "! perl-dist $file\n";