Home: Perl Programming Help: Beginner:
Premature end of script headers



MDTech.us_MAN
Novice

Feb 24, 2013, 7:08 PM


Views: 5502
Premature end of script headers

Hello,

I am getting the following error:

Code
[mdtech.us.ms] [Mon Feb 25 02:30:32 2013] [error] Premature end of script headers: index.cgi 
Error 500: Script Execution Failure


In the following code: http://pastebin.com/V9NdNJMK

Maxwell.

--

Please visit my website, http://www.MDTech.us.


(This post was edited by MDTech.us_MAN on Feb 24, 2013, 7:09 PM)


Kenosis
User

Feb 24, 2013, 7:31 PM


Views: 5497
Re: [MDTech.us_MAN] Premature end of script headers

Perhaps the follow will help: I keep getting "Error 500, Premature end of script headers" when running a Perl script.

Just in case, be sure your script's permissions are set to mode 0755, e.g., chmod 0755.


MDTech.us_MAN
Novice

Feb 24, 2013, 8:15 PM


Views: 5496
Re: [Kenosis] Premature end of script headers


In Reply To
Perhaps the follow will help: I keep getting "Error 500, Premature end of script headers" when running a Perl script.

Just in case, be sure your script's permissions are set to mode 0755, e.g., chmod 0755.


Nope, I checked everything. Still doesn't work.

Frown


FishMonger
Veteran / Moderator

Feb 24, 2013, 9:14 PM


Views: 5487
Re: [MDTech.us_MAN] Premature end of script headers

Your script generated an error before sending the http header.

This is usually caused by incorrect permission setting, incorrect apache config for the scriptalias directive or incorrect shebang line.

How to get over 'premature end of script headers' in Apache?
http://www.perlmonks.org/?node_id=24447


(This post was edited by FishMonger on Feb 24, 2013, 9:16 PM)


Kenosis
User

Feb 24, 2013, 9:14 PM


Views: 5486
Re: [MDTech.us_MAN] Premature end of script headers

...I checked everything.

Even the syntax?

perl -c script.pl
Can't find string terminator "HEAD111" anywhere before EOF at temp3.pl line 8.

For the here document, try:

Code
print <<HEADER; 

...

HEADER


Do the same for the footer. Also, you use some modules at the top of your script:

Code
use strict; 
use CGI;


Then later:

Code
use CGI; 
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);


Place all the modules you use at the top of your script. Also, always:

Code
use strict; 
use warnings;


It's evident later in your script that you're attempting to parse form data. The CGI module you're using handles this, so use its methods. The Fool's Guide to CGI.pm, the Perl module for CGI scripting can assist you to better understand the features of CGI.pm.


(This post was edited by Kenosis on Feb 24, 2013, 9:18 PM)


FishMonger
Veteran / Moderator

Feb 24, 2013, 9:28 PM


Views: 5482
Re: [MDTech.us_MAN] Premature end of script headers

Why are you outputing the header twice (once via the CGI module's header() function and once manually)?

Why are you loading the CGI module twice?

Why are you manually parsing the form submission (which has been outdated/depreciated for at least 10 years)?

D:\test>perl -c MDTech.us_MAN.pl
Can't find string terminator "HEAD111" anywhere before EOF at MDTech.us_MAN.pl line 8.


FishMonger
Veteran / Moderator

Feb 24, 2013, 9:37 PM


Views: 5478
Re: [MDTech.us_MAN] Premature end of script headers


Quote

Code
if (jQuery) { 
if (jQuery.browser.msie) window.onload = initFlyouts;
else jQuery(initFlyouts)
}else{
if (Prototype.Browser.IE) window.onload = initFlyouts;
else document.observe('dom:loaded', initFlyouts);
}



I see several syntax issues, don't you?

Why are you printing meta tags in the body section?

You really should put your css and javascript code into separate external files.


MDTech.us_MAN
Novice

Feb 25, 2013, 5:55 AM


Views: 5469
Re: [FishMonger] Premature end of script headers


Quote
Why are you manually parsing the form submission (which has been outdated/depreciated for at least 10 years)?


What is the better way?



In Reply To

Quote

Code
if (jQuery) { 
if (jQuery.browser.msie) window.onload = initFlyouts;
else jQuery(initFlyouts)
}else{
if (Prototype.Browser.IE) window.onload = initFlyouts;
else document.observe('dom:loaded', initFlyouts);
}



I see several syntax issues, don't you?

Why are you printing meta tags in the body section?

You really should put your css and javascript code into separate external files.


I actually just used the code from the weebly.com designer, since I'm not a HTML pro yet. I've been programming more in Visual Basic.


(This post was edited by MDTech.us_MAN on Feb 25, 2013, 6:36 AM)


FishMonger
Veteran / Moderator

Feb 25, 2013, 8:19 AM


Views: 5457
Re: [MDTech.us_MAN] Premature end of script headers

The better way to parse the form submission is to use the methods provided by the CGI module.

For example, this one line replaces lines 100 - 113 of your script.

Code
my %FORM = Vars;


Personally, I prefer the OO methods, so I'd normally it write as:

Code
my $cgi = CGI->new; 
my %form = $cgi->Vars;


The Can't find string terminator "HEAD111" issue is due to having the ; semicolon of the heredoc in the wrong place. It should be at the start, not the end of the heredoc.


Code
print <<HEAD111; 
....
....
....
HEAD111



FishMonger
Veteran / Moderator

Feb 25, 2013, 8:32 AM


Views: 5456
Re: [Kenosis] Premature end of script headers

Kenosis,

Have you looked over the info in that "Fool's Guide" link you gave?

The code examples are very poorly written, particularly the bad and incomplete html. That's not a resource that I'd be proud to promote.


Kenosis
User

Feb 25, 2013, 10:09 AM


Views: 5450
Re: [FishMonger] Premature end of script headers

I think you make an excellent point. I've had that resource on my shelf far too long. Time for some house cleaning...

It's the GCI.pm 'snippets' there that are primarily beneficial--certainly not the overall style. The Perl examples are not in good form, either--no strictures or lexically-scoped variables.

If you have a better CGI.pm resource to share that you think is well done, I'd appreciate it.

Thanks for your comment...


FishMonger
Veteran / Moderator

Feb 25, 2013, 4:34 PM


Views: 5443
Re: [Kenosis] Premature end of script headers

IMO, the best resource for the CGI module would be the module's documentation.
http://search.cpan.org/~markstos/CGI.pm-3.63/lib/CGI.pm

All other resources are built off that source.


Kenosis
User

Feb 25, 2013, 5:38 PM


Views: 5437
Re: [FishMonger] Premature end of script headers

Well, yes, this module's documentation is always an option. Am just not too sure how 'accessible' it is to the OP. This is a Beginners' forum...


(This post was edited by Kenosis on Feb 25, 2013, 5:40 PM)


FishMonger
Veteran / Moderator

Feb 25, 2013, 6:28 PM


Views: 5435
Re: [Kenosis] Premature end of script headers


Quote
Am just not too sure how 'accessible' it is to the OP. This is a Beginners' forum...


I'm not sure what you mean since the OP is using perl and the documentation comes as part of the base install and is accessible via the perldoc tool.


Code
D:\>perldoc CGI 
NAME
CGI - Handle Common Gateway Interface requests and responses

SYNOPSIS
use CGI;

my $q = CGI->new;

# Process an HTTP request
@values = $q->param('form_field');

$fh = $q->upload('file_field');

$riddle = $query->cookie('riddle_name');
%answers = $query->cookie('answers');

# Prepare various HTTP responses
print $q->header();
print $q->header('application/json');

$cookie1 = $q->cookie(-name=>'riddle_name', -value=>"The Sphynx's Question");
$cookie2 = $q->cookie(-name=>'answers', -value=>\%answers);
print $q->header(
-type => 'image/gif',
-expires => '+3d',
-cookie => [$cookie1,$cookie2]
);

print $q->redirect('http://somewhere.else/in/movie/land');

....
....
....


If the OP's installation is missing the docs, then I'd say that installation is broken.

Since the documentation is also online and the OP is obviously online as well, the documentation is easily available in html form via http://search.cpan.org/ and http://perldoc.perl.org/

It's possible that the OP didn't know about those resources and if that's the case, then IMO it's our responsibility to inform the OP.


Kenosis
User

Feb 25, 2013, 9:20 PM


Views: 5429
Re: [FishMonger] Premature end of script headers

I'm not sure what you mean...

s/'accessible'/understandable/

Had absolutely nothing to do with the OP acquiring the documentation. Had everything to do with attempting to be polite...