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:
Newbie question

 



Aaargh
New User

Jan 6, 2011, 8:58 AM

Post #1 of 2 (619 views)
Newbie question Can't Post

Hi all.

I'm new to programming and am having a bit of trouble getting a very simple .pl script to work. It's for a message board, and is as basic as it gets, yet it keeps returning a 500 error. I checked with my host server and they sent me back something I don't understand.

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

Here's my script:

#!/usr/bin/perl
#post_message.pl
$buffer=$ENV{'QUERY_STRING'};
$buffer =~ tr/+/ /;
$buffer =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
$buffer =~ s/<!--(.|\n)*-->/ /g;
$buffer =~ tr/\\|[|]|<|!|"|$|{|}|*|#|'|>|||;|%/ /;
@pairs = split(/&/,$buffer);
foreach $pair(@pairs){
($key,$value)=split(/=/,$pair);
$formdata{$key}.="$value";
}

$yourname=$formdata{'yourname'};
$email=$formdata{'email'};
$subject=$formdata{'subject'};
$message=$formdata{'message'};

open(INFO, ">>message_board.txt"); # Open for appending
print INFO "$yourname|$email|$subject|$message\n";
close (INFO);

open(INFO, "message_board.txt"); # Open db for reading and display
@array=<INFO>;
close (INFO);

print "Content-type:text/html\n\n"; #Follow with blank line

print "<html>\n";
print "<head><title>Message Board</title>\n";
print "<style type="text/css">\n";
print "h2\{font-family: Arial;font-style : normal ;font-size : 12pt; font-weight :bold;text-align :center\}\n";
print "td\{font-family: Arial;font-style : normal ;font-size : 10pt; font-weight :normal \}\n";
print "p\{ font-family: Arial;font-style : normal ;font-size : 10pt; font-weight :normal;text-align :left; width : 40em \}\n";
print "</style>\n";
print "</head>\n";
print "<body>\n";
print "<div align=\"center\">\n";
print "<h2>Message Board</h2>\n";
print "<table width=\"550\" cellpadding=\"5\" cellspacing=\"0\" border=\"1\">\n";

foreach $line (@array){
($yourname,$email,$subject,$message)=split(/\|/,$line);

print "<tr><td>\n";
print "Posted by: $yourname <a href="mailto:$email">Return Message by Email</a><br>\n";
print "Subject:$subject<br>\n";
print "<br>\n";
print "Message:<br>\n";
print "<p>$message</p>\n";
print "</td></tr>\n";
}

print "</table>\n";
print "<br><br>\n";
print "<a href=\"./message_brd.html\">Post a New Message</a>\n";
print "</div>\n";
print "</body>\n";
print "</html>\n";

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

HERE'S WHAT MY HOST SERVER SAYS:

Bareword found where operator expected at ./post_message.pl line 31, near ""<style type="text"
(Missing operator before text?)
String found where operator expected at ./post_message.pl line 31, near "css">\n""
Bareword found where operator expected at ./post_message.pl line 46, near ""Posted by: $yourname <a href="mailto"
(Missing operator before mailto?)
String found where operator expected at ./post_message.pl line 46, near "$email">Return Message by Email</a><br>\n""
(Missing operator before ">Return Message by Email</a><br>\n"?)
syntax error at ./post_message.pl line 31, near ""<style type="text"
syntax error at ./post_message.pl line 46, near ""Posted by: $yourname <a href="mailto"
Execution of ./post_message.pl aborted due to compilation errors.
-----

This appears to be due to embedding double-quotes in a double-quoted string.

----------------------------------------------------------------
Anybody care to decode this for me and explain what I did wrong and how to fix it?

Thanks a million times over.


Zhris
Enthusiast

Jan 6, 2011, 12:35 PM

Post #2 of 2 (613 views)
Re: [Aaargh] Newbie question [In reply to] Can't Post

Hello,

The errors you are receiving are syntax errors. If you want them to appear in the browser as your script compiles, you should use the CGI::Carp module:


Code
use CGI::Carp qw/fatalsToBrowser warningsToBrowser/; 
print "Content-type: text/html\n\n";
warningsToBrowser(1);


There are 2 key syntax errors in your code, and your host correctly pointed out that they were due to double quotes inside a double quoted string, which need to be escaped. Check lines 31 and 46 of your code, and escape (\") every internal double quote like so:


Code
31.  	print "<style type=\"text/css\">\n"; 

46. print "Posted by: $yourname <a href=\"mailto:$email\">Return Message by Email</a><br>\n";


For reference, there is no need to use multiple print statements to print a block of HTML code. Here is how to print a block of HTML code (note you don't need to escape characters, just write HTML code as you normally would. It will end printing when it reaches the closing string (in this case HTML Code)):


Code
print <<"HTML Code"; 
<html>
<head>
<title>$title</title>
</head>
<body>
<div id="mydiv"></div>
</body>
</html>
HTML Code


Let us know if you still have problems.

Chris


(This post was edited by Zhris on Jan 6, 2011, 12:49 PM)

 
 


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

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