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 "https://metacpan.org/module/\{@}", 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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.