Created
December 9, 2010 16:22
-
-
Save kentaro/734917 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use opts; | |
use URI; | |
use URI::Escape; | |
use Perl6::Say; | |
use Web::Scraper; | |
opts my $init => { isa => 'Str' }, | |
my $action => { isa => 'Str' }; | |
my $arg = shift or die "$0 (--init=list|--action=open) baz"; | |
{ | |
init => { | |
list => \&search_cpan, | |
}, | |
action => { | |
open => \&open_cpan, | |
}, | |
}->{($init && 'init') || ($action && 'action')}->{$init || $action}->($arg); | |
sub search_cpan { | |
my $word = uri_escape(shift); | |
my $uri = URI->new("http://search.cpan.org/search?m=module&q=$word&s=1&n=100"); | |
my $scraper = scraper { | |
process 'h2.sr > a > b', 'modules[]' => 'TEXT'; | |
}; | |
say $_ for @{$scraper->scrape($uri)->{modules} || []}; | |
} | |
sub open_cpan { | |
my $candidate = shift; | |
$candidate =~ s/::/-/g; | |
my $url = "http://search.cpan.org/dist/$candidate/"; | |
`open $url`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment