CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
News Addition

 



young twig
New User

Oct 24, 2003, 2:50 PM

Post #1 of 9 (704 views)
News Addition Can't Post

Bear with me, I'm 13 and just starting this. THis is my first PERL script, but for some reason it won't work. This is how it should work: There's a file called news.html which the PERL script writes to. It is included in the actual news page via SHTML. Simple... or so I thought. Everytime I get this error:

Premature end of script headers.

I've tried the permissions crap a thousand times... What's wrong?

This is the script:




#!/usr/bin/perl
$newslist="/home/young/public_html/news.html";

if (ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; }
FORM{$name} = $value;

open (NEWSLIST,"<< $newslist");

print NEWSLIST "<div class="head">$FORM{date}</div>\n\n";
print NEWSLIST "<div class="news">$FORM{news}</div>\n\n";
print NEWSLIST "<BR><BR>\n\n";
close (NEWSLIST);

&done;
}

sub done {
print "Content-type: text/html\n\n";
print <<"EndPage";


<HTML><HEAD><TITLE>Thepointyouth.com v02.1</TITLE>
<link rel="stylesheet" type="text/css" href="/misc/tpcss.css">
</Head>
<body>

<CENTER>
<BR><BR>
<div class="body">
<BR><BR>
Thank you for adding news. If you made a mistake, email twig to have it fixed...
</body></html>

EndPage

exit(0);
}




Please don't give me another script that will do what I want. I want to know is what is wrong and how to fix it. Thank you.


(This post was edited by young twig on Oct 24, 2003, 2:53 PM)


KevinR
Veteran


Oct 25, 2003, 12:10 AM

Post #2 of 9 (700 views)
Re: [young twig] News Addition [In reply to] Can't Post

syntax errors here:

print NEWSLIST "<div class="head">$FORM{date}</div>\n\n";
print NEWSLIST "<div class="news">$FORM{news}</div>\n\n";

the double-quotes need to be escaped:

print NEWSLIST "<div class=\"head\">$FORM{date}</div>\n\n";
print NEWSLIST "<div class=\"news\">$FORM{news}</div>\n\n";

and make sure there is nothing else on the same line with string terminator:

EndPage

if you fix the two lines above the script will hopefully work.
-------------------------------------------------


davorg
Thaumaturge / Moderator

Oct 25, 2003, 5:05 AM

Post #3 of 9 (697 views)
Re: [young twig] News Addition [In reply to] Can't Post


In Reply To

Code
print NEWSLIST "<div class="head">$FORM{date}</div>\n\n"; 
print NEWSLIST "<div class="news">$FORM{news}</div>\n\n";



You can't put double quotes in a double-quoted string. Try using the qq() operator instead.


Code
print NEWSLIST qq(<div class="head">$FORM{date}</div>\n\n); 
print NEWSLIST qq(<div class="news">$FORM{news}</div>\n\n);


Some tips for tracking down bugs.

1/ The real error message will probably be in the web server error log.
2/ Add "use strict" and "use warnings" in your code to get early warning of problems.
3/ Try running the program from the command line with "perl -c" in order to track down syntax errors.
4/ Don't write your own CGI parameter parser - use CGI.pm instead.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


young twig
New User

Oct 25, 2003, 6:03 PM

Post #4 of 9 (693 views)
Re: [davorg] News Addition [In reply to] Can't Post

I knew it was somehting obvious... I probably copyed and pasted the news code (<div style=...) right into the script. Thanks for the replies. If it works, you just helped me with 3-4 scripts... :)


young twig
New User

Oct 25, 2003, 8:05 PM

Post #5 of 9 (691 views)
Re: [young twig] News Addition [In reply to] Can't Post

Nope. I still get the same thing. 500 error and all the error log says is "Premature end of script headers."


What's the CGI.pm stuff? Any good links?


(This post was edited by young twig on Oct 25, 2003, 8:07 PM)


KevinR
Veteran


Oct 25, 2003, 8:36 PM

Post #6 of 9 (687 views)
Re: [young twig] News Addition [In reply to] Can't Post

I don't believe CGI.pm is going to help with your problem. Sounds like your script is maybe trying to print something to the screen before printing the HTTP header.

But you can learn a bit about the CGI.pm module here:

http://www.perldoc.com/perl5.8.0/lib/CGI.html
-------------------------------------------------


davorg
Thaumaturge / Moderator

Oct 26, 2003, 3:54 AM

Post #7 of 9 (686 views)
Re: [young twig] News Addition [In reply to] Can't Post


In Reply To
Nope. I still get the same thing. 500 error and all the error log says is "Premature end of script headers."


Try point 3 in my previous answer.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


young twig
New User

Oct 26, 2003, 6:34 AM

Post #8 of 9 (685 views)
Re: [davorg] News Addition [In reply to] Can't Post

I didn't really understnd point 3. Could you explain it to me? (sorry for my utter newbie-ness.)


davorg
Thaumaturge / Moderator

Oct 26, 2003, 7:17 AM

Post #9 of 9 (682 views)
Re: [young twig] News Addition [In reply to] Can't Post


In Reply To
I didn't really understnd point 3. Could you explain it to me? (sorry for my utter newbie-ness.)


Perl has a built-in syntax-checknig mode. In this mode, Perl will compile your program and will warn you of any syntax errors that it finds, but won't actually execute the program.

To do this simply run the following from your command line (assuming that your program is in "myprog.pl".

Code
perl -c myprog.pl

You'll then get a list of any errors that Perl finds. You can fix these before going any further.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives