Skip to content

Instantly share code, notes, and snippets.

@kentaro
Created December 9, 2010 16:22
Show Gist options
  • Save kentaro/734917 to your computer and use it in GitHub Desktop.
Save kentaro/734917 to your computer and use it in GitHub Desktop.
#!/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