girl has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am having a lot of trouble with getting a file to load. When i try it says internal server error. When i go to error logs to check what the problem is, it say premature ending of script headers.

I tried changing the permissions to 755. I think i fixed all the syntax errors.

it still won't work. :(

Here is the page

#!/usr/bin/perl use 5.006; #! /usr/bin/perl &form_parse; $firstnumber = $FORM{'firstnumber'}; $secondnumber = $FORM{'secondnumber'}; $operation = $FORM{'operation'}; print "Content-type: text/html\n\n"; &printconfirmation; ############################################################ # CONFIRMATION PAGE ############################################################ sub printconfirmation { print <<ENDCONFIRMATIONPAGE; <html> <head> <title>Demo Confirmation Page</title> <style> td { background-color: white; font-size: 16pt; } input { font-size: 16pt; background-color: #E7CFCF; color: #FF6633; } select { font-size: 16pt; background-color: #E7CFCF; color: #FF6633; } textarea { font-size: 16pt; background-color: #E7CFCF; color: #FF6633; + } </style> </head> <body bgcolor="white" text="#FF6633" alink="#FF6633" vlink="#FF6633"> <br><p><br> <p><center><h1><b>The answer is: $firstnumber</b> </h1></center> <P ALIGN="center"><p>$answer<br> <p><br><p> </P> <P><br> <BR><P> </body> </html> ENDCONFIRMATIONPAGE } ############################################################ # FORM PARSE ############################################################ sub form_parse { read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs){ ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/\'//g; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/e +g; $FORM{$name} = $value; } }

Replies are listed 'Best First'.
Re: Help!! Premature ending of script headers?
by ~~David~~ (Hermit) on Oct 13, 2011 at 17:41 UTC
    I would recommend doing the following to help in troubleshooting:
    #!/usr/bin/perl use strict; # make sure these are on, you will have to declare all + your variables now use warnings; # use 5.006; use CGI::Carp qw( fatalsToBrowser warningsToBrowser ); # this will le +t you see your script problems $| = 1; # continuously flush the print buffer print "Content-type: text/html\n\n"; # move this to the top so your +web browser will display error messages &form_parse; $firstnumber = $FORM{'firstnumber'}; $secondnumber = $FORM{'secondnumber'}; $operation = $FORM{'operation'}; &printconfirmation;
Re: Help!! Premature ending of script headers?
by Anonymous Monk on Oct 13, 2011 at 22:43 UTC
Re: Help!! Premature ending of script headers?
by aaron_baugher (Curate) on Oct 14, 2011 at 11:29 UTC

    That error means the script exited before it could print out the Content-type header. If you add use warnings and use strict to your script, the reason will probably show up in your log file. Running the script from the command line may also reveal it.

    Incidentally, if this is an exercise in learning how the CGI interface works, that's fine. But if it's an attempt to begin doing practical work, look into the CGI module. Your form_parse routine is lacking in a variety of ways (for instance, a semicolon is a valid separator in place of the ampersand, and they could also be encoded in a couple different ways). It's pretty complicated to write a CGI argument parser that handles all valid URI formats correctly and securely, which is why CGI.pm exists. Some modules are mostly a convenience or a typing-saver, but others are simply necessary for certain work, and I'd put CGI.pm (or one of its descendants, including the many frameworks) in the latter category.

Re: Help!! Premature ending of script headers?
by DanielSpaniel (Scribe) on Oct 13, 2011 at 19:47 UTC

    Just wondering why you have the following at the top of your code (i.e. two shebang lines):

    #!/usr/bin/perl use 5.006; #! /usr/bin/perl

    ... or whether that's just a typo from you entering it on the forum. If those lines are both in your code, then there's a reasonable chance that may be the cause of your problem. (or, at a minimum, it would be a good thing to take care of ... i.e. delete the second one)

      If those lines are both in your code, then there's a reasonable chance that may be the cause of your problem.

      Isn't the second occurrence effectively a comment though?


      Improve your skills with Modern Perl: the free book.

        Good point ... I hadn't thought of that ... doh! ... I guess maybe it would treat it as a commment ...

        Now I'm going to have to try it out for myself and see :-)