CGI perldoc with pod2htm
hmerrill
created: 2004-06-14 14:18:13
I'm attempting to create my own 'perldoc' cgi script. My plan is to be able to type in a URL like this:
   http://my.server.com/perldoc.pl?mod=DBI
and have my CGI script search the @INC array for DBI.pm or DBI.pod - when it finds one it would invoke 'pod2html /path/to/DBI.pm' and spew the output to the browser.

Seems simple in concept, but it's not working, and I'm guessing that I'm missing something simple. Here's the code:

#!C:\Perl\bin

use strict;
use CGI;

my $q = new CGI;
my $module = $q->param('mod');
if (not defined($module)) {
	print <
		Perldoc - No module specified!!
		
			

Perldoc Error - No module specified!!
END_HTML exit(1); } ################################################################# my $found = 0; my $abs_filename = ""; foreach my $dir (@INC) { opendir(DH, $dir) or die "Can't open $dir: $!"; while( defined (my $file = readdir DH) ) { if ($file =~ /$module\.(pm|pod)/) { $abs_filename = "$dir/$file"; $found = 1; last; } } } if ($found) { #print $q->header('text/html'), $q->start_html("test"); #print $q->header('text/html'); #print $q->h1(`pod2html $abs_filename`); print "Content-type: text/html\n\n"; `pod2html $abs_filename`; #print `pod2html CGI`; #print $q->header('text/html'), $q->start_html("test"); #print $q->h1("\$abs_filename=[$abs_filename]"); #print $q->end_html; exit 0; } else { print < Perldoc - Module $module *NOT* found!!

Perldoc Error - module $module *NOT* found!!
END_HTML exit(1); }
Note that this is on Windows XP. When I run this with "http://my.server.com/perldoc.pl?mod=DBI", a *blank* page is displayed - no error, no nothing. Any ideas would be appreciated.

TIA.

Re: CGI perldoc with pod2htm
created: 2004-06-14 14:32:18
It looks that `pod2html $abs_filename`; is called in void context, you need to print it. Beside this take a look at Pod::Webserver or Apache::Pod.
Boris
Re^2: CGI perldoc with pod2htm
created: 2004-06-14 15:07:31
I've even tried doing this:
    print "Content-type: text/html\n\n";
    my $html_output = `pod2html $abs_filename`;
    print $html_output;
but that doesn't work either - same thing - blank screen. Other ideas?
Re^3: CGI perldoc with pod2htm
created: 2004-06-14 15:21:22
Perhaps pod2html is not in your path? Try to use it with the full path in front of pod2html.
Boris
Re: CGI perldoc with pod2htm
created: 2004-06-14 15:52:43

You could simplify this by just calling perldoc -oHtml $module. That will take care of all the messiness of finding the pod file for you. I'd do it in a piped open so that there is an Errno to condense all the errors you're testing for.

{
    my $cpid = open my $fh, '-|',
       '/usr/bin/perldoc', '-oHtml', $module;
    defined $cpid or print error_routine($!) and last;
    print while <$fh>;
    close $fh;
}

After Compline,
Zaxo

A CGI to read PODS (was Re: CGI perldoc with pod2htm)
created: 2004-06-14 16:39:02
Here's what I use:

UPDATE I've added code to handle .pod files as well as .pm files. This means you can put Pod::perl or Pod::perlreftut in the query box to see the HTML equivalents of "perldoc perl" etc. You can also see a directory listing of the Pod:: hierarchy (or any hierarchy) by putting a slash after th the hierarchy name (e.g. mod=Pod/ will display a clickable list of pods in the Pod::* hierarchy).

#!perl -w
#
# by Jeff Zucker
#
# may be freely modified and distributred under the same terms as Perl itself
#
use strict;
use CGI;
use CGI::Carp qw/fatalsToBrowser/;
use File::Find;
use Pod::Html;
my $page = new CGI;
my $modname = $page->param('mod') || '';
show_form();
if( $modname =~ m#/# ) { show_dir($modname); }
else { show_pod( $modname ); }

sub show_form {
    print 
        $page->header, 
        $page->start_html(-title=>'POD FINDER'), 
        $page->start_form, 
        $page->b("Module Name : "),
        $page->textfield(-name=>'mod'),
        $page->end_form,
        $page->end_html,
    ;
}

sub show_pod {
    my $modname = shift;
    return unless $modname;
    $modname =~ s#::#/#g;
    my $filename = findmod( $modname );
    pod2html("--quiet","--infile=$filename") if $filename;
}
sub show_dir {
    my $dir = shift;
    $dir =~ s#/$##g;
    my $scriptname = $page->script_name;
    for(@INC){
        my $fdir = "$_/$dir";
        if( -d $fdir ) { 
            opendir(D,$fdir) || die $!;
            my @files = readdir D;
            closedir D;
            for(@files) {
                next unless /(.*)\.(pm|pod)$/i;
                print "$dir\:\:$1
"; } } } return ''; } sub findmod { my $mod = shift; if (-e $mod ) { return $mod; } for(@INC){ my $modname = "$_/$mod.pm"; if( -e $modname ) { return $modname; } $modname = "$_/$mod.pod"; if( -e $modname ) { return $modname; } } return ''; } __END__
Re: A CGI to read PODS (was Re: CGI perldoc with pod2htm)
created: 2004-06-15 15:59:04
Brilliant! ++ to the poster.
Although I would like to offer some very minimal constructive criticism. I'm a bit lazy at times, and depending on the complexity of the code I'm looking at, I'll either try to dissect it for my own edification, or just move on (I know shame on me, I'll never learn anything, etc, etc..). To make a long story short, I would have added a print statement in your show_form subroutine and added a line saying to submit a slash "/" to get a listing of all the POD docs on the system.
Grea code though, I know I'll make use of it.


Very funny Scotty... Now PLEASE beam down my PANTS!
Re: A CGI to read PODS (was Re: CGI perldoc with pod2htm)
created: 2004-06-29 13:18:44
Excellent JZed! The updates fix problems that I was trying to fix myself, but I was unsuccessful. Very nice code.

perlmonks.org content © perlmonks.org and borisz, hmerrill, jZed, spartan, Zaxo

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

v 0.03