#!/usr/bin/perl -w use strict; use URI::Escape; chdir '/tmp'; # if possible my $name = (split '/', $ENV{SCRIPT_NAME} || $0)[-1]; print "Content-type: text/html\n\n"; uri_unescape($ENV{QUERY_STRING} || 'perldoc') =~ /^([\w\s:-]*)$/ or do { print "bad query string: $ENV{QUERY_STRING}"; exit; }; open DOC, "perldoc -u $1|pod2html --header --title='$1'|" or do { print "can't run perldoc: $!"; exit; }; while (<DOC>) { s{ href="/([^."]+)\.html } { local $_ = $1; s|/|::|g; qq(href="$name?$_) }gexi; print; }

Usage:

UPDATE: thanks to tachyon for suggestions
I've chosen deafult argument 'perldoc' instead of error reporting on empty query,
2>/dev/null added,
relative_path used instead of hardcoded 'perldoc.cgi'
fork eliminated

Replies are listed 'Best First'.
Re: perldoc.cgi
by tachyon (Chancellor) on Oct 10, 2004 at 11:45 UTC

    Rather neat. Couple of minor suggestions. The first is mainly because it needs at least one char to search for (or it dies voluminously which 2 > is also designed to fix). If you used a var rather than the hardcode name the script is renamable.

    - uri_unescape($ENV{QUERY_STRING}) =~ /^([\w\s:-]*)$/ + uri_unescape($ENV{QUERY_STRING}) =~ /^([\w :-]+)\z/ - exec "perldoc -u $1|pod2html --header --title='$1'"; + exec "perldoc -u $1|pod2html --header --title='$1' 2>/dev/null";

    You don't really need the fork.....

Re: perldoc.cgi
by tachyon (Chancellor) on Oct 10, 2004 at 22:35 UTC

    You can make the name grab more reliable by using -1 to get the last element.

    - my $name = (split '([^/]+$)', $ENV{'SCRIPT_NAME'} || $0)[1]; + my $name = (split '/', $ENV{'SCRIPT_NAME'} || $0)[-1];

      OK, I agree. Your one is more simple. Thanks

Re: perldoc.cgi
by Anonymous Monk on Oct 10, 2004 at 12:05 UTC
Re: perldoc.cgi
by elwarren (Priest) on Oct 11, 2004 at 15:53 UTC
    That's neat++ I've posted my local docserver in CUFP after seeing your perldoc.cgi this morning. Might be neat to merge them, if you get bored :-)