[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl-WebCGI] Cracked-up CGI Output



Hi. I have written a cgi that is designed to turn a manual into a HTML
version. The "manuals" are text files and have the following structure:

TITLE
etc.
------------------------------------------------------------------------

Contents:

1. Chapter 1
2. Chapter 2
3. Chapter 3

------------------------------------------------------------------------

1. Chapter 1
------------------------------------------------------------------------

chapter 1. blah blah

------------------------------------------------------------------------

2. Chapter 2
------------------------------------------------------------------------

blah blah blah

------------------------------------------------------------------------

3. Chapter 3
------------------------------------------------------------------------

blah blah blah

This script works fine when run in MacPerl, but when I turn it into a
CGI it hangs with the CGI application (manual2html.cgi) waiting for a
response from MacPerl. I have found this seems to only happen when the
returned web page is large. If I comment out the loop, or place a return
statement at the end of the loop so only the first section is printed,
it works. Some of the text files are very large (>150K). Attached is a
copy of the program. I cannot find any problem with it otherwise.

--
COM              Quentin
   CLUB          Smith
       Software  Chief programmer
-----BEGIN GEEK CODE BLOCK----
Version: 3.12
GCS d s:+ !a C++++ U*+++ P++++ L+ E-- W+++ N++ o+ K- w--- O? M++ V? PS
PE- Y-- PGP++ t+ 5 X R tv++ b+++ D I- D? G! e h! r
-----END GEEK CODE BLOCK-----

--

#!perl
$| = 1; # Force no buffering.
use CGI;
$q = new CGI;

print $q->header;

$DEBUG=($q->param("debug") || 0);

$file = ($q->param("file") || "Macintosh HD:Web Pages:mans:64.txt");
$gname = ( $DEBUG ? "Manual 64" : $q->param("title"));
open IN, $file or die "Couldn't open $file: $!";
$slash = $/;
undef $/;
$text = <IN>;
$/ = $slash;
close IN;
$text =~ s/\n\r?/\n/g;
@sects =
split(/------------------------------------------------------------------------/,
$text);
$title = shift @sects;
$contents = shift @sects;
%sects = @sects;

print <<EOF;
<HTML>
<HEAD>
<TITLE>$gname</TITLE>
</HEAD>
<BODY BGCOLOR="white">
<PRE>
EOF

print $title;
print "</PRE>\n";
$contents =~ s/Contents://;
$contents = trimEnds($contents);


print "<H2>Contents</H2>\n";
foreach (split /\n/, $contents) {
    /^(\d+)\./;
    print "<LI><A HREF=\"#$1\">$_</A><BR>\n";
}

foreach $item (sort {$a <=> $b} keys %sects) { # Print the sections
    $itemname = trimEnds($item);
    $itemname =~ /^(\d+)\./;
    $sectnum = $1;
    print "<H2><A NAME=\"$sectnum\">$itemname</A></H2>\n";
    print brs($sects{$item});
    #return; # Un-commenting this works, but it only prints one section.

}


print <<EOF;
</BODY>
</HTML>
EOF


sub list {
    my $text = shift;
    $text =~ s/\n/\n<LI>/g;
    return "<LI>" . $text;
}

sub trimEnds {
    $t = shift;
    while ((substr $t, 0, 1) =~ /\s/) {
 $t = substr $t, 1;
    }
    while ((substr $t, -1, 1) =~ /\s/) {
 $t = substr $t, 0, -1;
    }
    return $t;
}
sub brs {
    my $text = shift;
    $text =~ s/\n/<BR>\n/g;
    return $text . "<BR>";
}



==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-webcgi-request@macperl.org