Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Personal perldoc webserver

by Tanktalus (Canon)
on Feb 28, 2013 at 00:49 UTC ( #1020979=CUFP: print w/replies, xml ) Need Help??

Most of the time I want to read the doc for a module, I either bring up konqueror where I have a shortcut "mod:" that sends me to "\{@}", or I have to run "perldoc" on the module in my console because either I need the specific version of the module I'm using, and that's not always the latest, or, more likely, I need the doc to a module in our own repository (i.e., our code).

Having given up on that, I whipped together the following. Now I can go to "http://localhost:8877/Module::Name" and get the perldoc in HTML form in my browser for this particular level. As a bonus, the links (L<...>) between modules works as well, which didn't quite happen for free.

TODO list includes being able to dynamically switch between multiple installed perls for the @INC that I'll be using, as well as multiple code streams (getting the doc for My::Foo in the current release, the previous release, etc.), probably by allowing some sort of version identifier in the URL (http://localhost:8877/v3/My::Foo), and getting the perldoc -f flag :)

In the hopes it gives someone else a starting point...

# App::PerldocServer use autopackage; use strict; use warnings FATAL => 'all'; use 5.16.2; use EV; use AnyEvent::HTTPD; use File::Basename qw(basename dirname); use Pod::Simple::HTML; use URI::Escape qw(uri_unescape); sub new { bless {} } sub run { my $self = shift; my $httpd = AnyEvent::HTTPD->new( port => 8877 ); $httpd->reg_cb( '' => sub { my ($httpd, $req) = @_; my $path = $req->url()->path(); my $m = uri_unescape(basename($path)); $httpd->stop_request(); my $f = $self->find_module($m); my $html; if ($f) { local $Pod::Simple::HTML::Perldoc_URL_Prefi +x = ('http://localhost:8877' . dirname($path) . '/') =~ s[/+$][/]r; my $p = Pod::Simple::HTML->new(); $p->output_string(\$html); $p->parse_file($f); } else { $html = "<html><body><h1>Unknown module: $m +</h1></body></html>"; } $req->respond( { content => [ 'text/html', $html ] } ); }, ); EV::loop; } sub find_module { my $self = shift; my $m = shift; my @paths; push @paths, File::Spec->catdir($ENV{CC_ROOT},'lib'), if $ENV{CC_ROOT}; push @paths, @INC; $m =~ s[::][/]g; for my $p (@paths) { for my $e (qw(pod pm)) { my $fullpath = "$p/$m.$e"; return $fullpath if -r $fullpath; } } return undef; } 1;
And the main script is my as-always-so-short:
#!/usr/bin/perl use rlib '../lib'; use App::PerldocServer; my $app = App::PerldocServer->new(); exit $app->run();

Update: removed reference to parent class - I don't think it's needed here.

Replies are listed 'Best First'.
Re: Personal perldoc webserver
by Your Mother (Chancellor) on Feb 28, 2013 at 06:24 UTC
    perl -Mojo -e 'plugin "PODRenderer"; a()->start' daemon

    Then try http://localhost:3000/perldoc/Mojo, for example. :P

      Very cool.
      Never knew it was that simple!
Re: Personal perldoc webserver
by Anonymous Monk on Feb 28, 2013 at 04:49 UTC
Re: Personal perldoc webserver
by perl-diddler (Hermit) on Mar 14, 2013 at 05:45 UTC
    Where does one find:

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://1020979]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2017-01-22 21:00 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (190 votes). Check out past polls.