#!/sw/bin/perl
#############################
# By: Ventz Petkov          #
# Date: 6-22-05             #
# CPAN Name/URL/Info Finder #
#############################
use warnings;
use strict;

use WWW::Mechanize;

if (@ARGV != 1) {
    print "\t\tUsage: ./searchcpan.pl query\n";
    print "\t\tEx: ./searchcpan.pl mp3\n"; exit();
}

my $mech = WWW::Mechanize->new();
my $url = 'http://search.cpan.org';

$mech->agent_alias( 'Mac Safari' );
$mech->get( $url );


$mech->submit_form  (
    form_name => 'f',
    fields    => { query  => $ARGV[0], }, 
);

my @alllinks = $mech->links;
for (@alllinks) {
    my $text = $_->text;
    my $link = $_->url;
    next unless($text =~ /(.*)::(.*)/); 
    print "$text\n";
    print "\thttp://search.cpan.org$link\n";
    $mech->get("http://search.cpan.org$link");
    if($mech->content =~ /<p>(.*)::(.*) (&#45;)* (.*)<\/p>/) {
        print "\t$4\n\n";
    }
}
