Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Launch CPAN search in browser from Windows command shell

by xdg (Monsignor)
on Apr 06, 2006 at 23:15 UTC ( [id://541763]=CUFP: print w/replies, xml ) Need Help??

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 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.

Replies are listed 'Best First'.
Re: Launch CPAN search in browser from Windows command shell
by demerphq (Chancellor) on Apr 07, 2006 at 07:13 UTC

    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
by PodMaster (Abbot) on Apr 08, 2006 at 22:56 UTC
    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.

Re: Launch CPAN search in browser from Windows command shell
by zentara (Archbishop) on Apr 07, 2006 at 11:46 UTC
    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) }

    UPDATE.. here is a little better script that will do local files too. There is a way to select between new-tabs and new-windows, but I didn't make a commandline switch for it. There are also some special syntax differences for firefox, which I don't use. So all, in all, if you could incorporate all the possibilites for IE,Mozilla, Firefox,etc, and have it auto detect urls or local files, you would have a cool module. Maybe Browser::AutoLoad? :-)

    #!/usr/bin/perl -w use strict; use Cwd; my $pid; my @docs; if(@ARGV){ my $cwd = getcwd; foreach my $arg(@ARGV){ if( $arg =~ /^http:\/\/.*$/ ){ push @docs, $arg } else{ push @docs , "file://$cwd/$arg" } } } else{ #load defaults @docs = qw( file:///home/zentara/1down/1GET http://www.google.com ); } # Look for instance of mozilla. `mozilla -remote "ping()"`; if ( $? ) { # Error, mozilla is not running. if (!defined($pid = fork())) { # Undef branch of fork: die "Cannot fork: $!"; } elsif ($pid == 0) { # Child branch of fork: `mozilla`; # Start new instance.... } else { # Parent branch of fork: do { `mozilla -remote "ping()"`; #sleep 1; select(undef,undef,undef,.5); } while ( $? ); #sleep 1; #give mozilla some time select(undef,undef,undef,.5); load_docs(); $pid=waitpid($pid, 0); # Wait for the pseudo-process # containing the new mozilla instance # to end. } } else { load_docs(); } ############################################################### sub load_docs { my $element; foreach (0..$#docs){ if ( $docs[$_] =~ /~file:\/\// ) { `mozilla -remote "openFile($docs[$_], new-tab)"` ; #`mozilla -remote "openFile($docs[$_], new-window)"` ; } else { `mozilla -remote "openURL($docs[$_], new-tab)"` ; # `mozilla -remote "openURL($docs[$_], new-window)"` ; } } }

    I'm not really a human, but I play one on earth. flash japh

      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.

        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
by rinceWind (Monsignor) on Apr 07, 2006 at 09:00 UTC

    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)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://541763]
Approved by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-19 21:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found