Launch CPAN search in browser from Windows command shell
xdg
created: 2006-04-06 19:15:51

My development is split between Windows XP and Linux. Even on Windows, I do most of my programming from the command-line. Recently, I thought it would be nice to launch a browser with a query to [http://search.cpan.org] straight from the command line:

> searchcpan Class::InsideOut

I also adapted the pseudo-URL style from perlmonks:

> searchcpan mod://Storable

> searchcpan dist://Test-Simple

> searchcpan author://dagolden

To get this working, I installed the following simple code as C:\perl\bin\searchcpan.pl and then ran pl2bat searchcpan.pl in that directory.

# searchcpan.pl
use strict;
use warnings;

my %modes = (
    "mod://" => "module",
    "dist://" => "dist",
    "author://" => "author",
);

my $url = "http://search.cpan.org/search?query=QUERY;mode=MODE";
my $target = shift;

if ( ! $target ) { 
    $url =~ s{/search\?.+}{};
}
else {
    my ($mode) = grep { $target =~ qr/\A$_/ } keys %modes;

    if ( defined $mode ) {
        $target =~ s{\A$mode}{};
        $url =~ s{MODE}{$modes{$mode}};
        $url =~ s{QUERY}{$target};
    }
    else {
        $url =~ s{QUERY}{$target};
        $url =~ s{MODE}{all};
    }
}

system('C:\windows\system32\cmd.exe', "/C start $url" );

-xdg

Code written by xdg and posted on PerlMonks is [http://creativecommons.org/licenses/publicdomain|public domain]. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Launch CPAN search in browser from Windows command shell
created: 2006-04-07 03:13:12

Slightly off topic, but maybe interesting to somebody....

There is a script out there somewhere that uses AS "perlscript" and IE to make the AS docs interactively searchable. I dont recall much more than that, but it was a nifty tool, and a good example of how you can use IE/web as a presentation engine for Perl. No web server needed, just AS and IE.

---
$world=~s/war/peace/g

Re: Launch CPAN search in browser from Windows command shell
created: 2006-04-07 05:00:19

I agree that's a neat idea, launching a browser window to display search results. I wonder if you could provide an alternative launch mechanism that would work with *nix as well, dependent on $^O. Reusing an existing browser instance and possibly opening in a new tab would be nice :).

Also, some other ideas spring to mind, like a dependency checker that opens browser windows for all the modules that you need, but don't yet have installed.

--

Oh Lord, won’t you burn me a Knoppix CD ?
My friends all rate Windows, I must disagree.
Your powers of persuasion will set them all free,
So oh Lord, won’t you burn me a Knoppix CD ?
(Missquoting Janis Joplin)

Re: Launch CPAN search in browser from Windows command shell
created: 2006-04-07 07:46:07
In case you want to try and do some ^O switching, I've used this on linux. It might get you over a tricky quote syntax hurdle.
my $linkurl = 'http://zentara.net';
my $command = "mozilla -remote \"openURL($linkurl)\"";
if(fork() == 0){ exec ($command) }

I'm not really a human, but I play one on earth. flash japh
Re^2: Launch CPAN search in browser from Windows command shell
xdg
created: 2006-04-07 09:30:49

Yeah, the cheater bit I used for Windows is the start command that dispatches the URL to the user-configured browser. I suspect there's an equivalent in Gnome/KDE (though I don't remember offhand). Unfortunately, without hard-coding a browser, I suspect this is both OS and X-environment specific on Unix. And I have no idea about MacOSX, though I suspect there's a similar "launch-url" command somewhere.

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re^3: Launch CPAN search in browser from Windows command shell
created: 2006-04-07 11:17:00
My Debian desktop has the following alternatives setup:
www-browser (/usr/bin/w3m)

mozilla (
 +    1        /usr/bin/mozilla-suite
*     2        /usr/bin/mozilla-firefox
)

x-www-browser (
      1        /usr/bin/mozilla
      2        /usr/bin/epiphany
 +    3        /usr/bin/konqueror
*     4        /usr/bin/mozilla-firefox
)
So either mozilla or x-www-browser seem like sensible "launch-url" commands.
Re: Launch CPAN search in browser from Windows command shell
created: 2006-04-08 18:56:55
For portability you can use HTML::Display

MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
** The third rule of perl club is a statement of fact: pod is sexy.

perlmonks.org content © perlmonks.org and demerphq, PodMaster, rhesa, rinceWind, xdg, zentara

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03