I am getting a premature end of script headers error when running a 
particular script from a browser.  It runs fine the command line, printing 
the proper header and all.

I use the CGI.pm module to print the header, and do not print anything 
prior to the header.  I do not understand why this error occurs.  The code 
is below.

Any suggestions anyone can offer would be appreciated.

#!/usr/local/bin/perl

###############################################################################
# cube_root.cgi
# by Peter Cline 02-10-2002
# Implements Martin Gardner's description of how one can immediately determine
# a cube root given a cube of an integer between 1 and 100
# The program assumes that the number provide is indeed a cube.  Therefore if
# a number which is not a cube is given,the program will return the root of the
# nearest cube.  This is consistent with how a person would perform, unless
# one took the time to memorize all 100 cubes
###############################################################################

use strict;
use CGI qw(:standard);
my $q = new CGI;
my $cubed = $q->param("cubed");                        #get user provided 
cube from cgi parameters

print header;
my @cubes = qw(0 1 8 27 64 125 216 343 512 729 1000);  #here the program 
"memorizes" the first 10 cubes.
                                                        # 0 is included 
because Perl uses 0 indexed arrays

my $root = "";

if ($cubed > 1000000) {                   #check to see if the number is 
bigger then the cube of 100
     print<<HTML;                          #if so, an error screen is printed.
        <html>
        <head>
        <title>Error Page</title>
        </head>
        <body bgcolor="#ffffff">
        <h3>Oops,your number was too large!</h3>
        <p>I'm sorry but I am only able to deal with cubes of numbers between 1
        and 100.  The number you gave me is too large!.  Please try 
again.  If you
        use a number in the proper range, I promise I won't disappoint.</p>
        <form method="POST" action="/cgi-bin/cube_root.cgi">
        If you please, pick an integer between 1 and 100, cube it and put 
the result here:
        <input type="text" name="cubed" size="10" maxlength="10">
        <input type="submit" value="Find the Cube Root!">
        </form>
        <a href="/D.BioInformatics_Spring2002/D.Students/peterc/">Return to 
My Homepage</a>
        </body>
        </html>
HTML
} else {                              #otherwise we calculate the cube

    if ($cubed =~ /([145690])$/) {     #if a cube ends in 0,1,4,5,6 or 9 so 
will its root
       $root = $1;
    } elsif ($cubed =~ /([2378])$/) {  #if a cube ends in 2,3,7 or 8 its 
root ends in 10 - last digit of cube
       $root = 10 - $1;
    }
    $cubed = substr ($cubed,0,length($cubed) - 3);    #create modified 
number by removing the last three digits

    for (1..10) {                                     #find 2 cubes between 
which modified number falls and prepend
                                                      #root of smaller cube 
to the root we are determining
       my $next = $_ + 1;
       if ($cubed > $cubes[$_] and $cubed < $cubes[$next]) {
          $root = $_.$root;
          last;
       }
    }
    print<<HTML;                                         #print 
confirmation page
        <html>
        <head>
        <title>Success Page</title>
        </head>
        <body bgcolor="#ffffff">
        <h3>I have an answer for you</h3>
        <p>The cube root of the number you provided is $root.  If you'd 
like to try again, you may do so below.
        Otherwise, use the link below the form to return to my homepage.</p>
        <form method="POST" action="/cgi-bin/cube_root.cgi">
        If you please, pick an integer between 1 and 100, cube it and put 
the result here:
        <input type="text" name="cubed" size="10" maxlength="10">
        <input type="submit" value="Find the Cube Root!">
        </form>
        <a href="/D.BioInformatics_Spring2002/D.Students/peterc/">Return to 
My Homepage</a>
        </body>
        </html>
HTML

}
Peter Cline
Software Engineer
New York Times Digital 

Reply via email to