Test my first script!

They have: 314 posts

Joined: Nov 1999

Would anyone like to give me some feedback on my first ever perl script? I could so with some advice in what to add to it to make it better I have already thought of a page which says "You wrote blah blah" so that it doesnt just go back to the admin page once sumitted. The code is as follows.

(Just put into a file named news.pl and and chmod to 755).

code:

#!/usr/bin/perl
############################
# Copyright DotComper 2000 #
# First run 25th June 2000 #
# Version 1.1              #
############################

# Please define the variables below
$scripturl = "http://www.url.com/cgi-bin/testingnews/news.pl";
$newspage = "/path to news/news.html";
$nameofnews = "site News";
$nameofsite = "site";
$subfont = "Comic Sans MS";
$memfont = "Comic Sans MS";
$subcolour = "red";
$memcolour = "black";


##################################################################################
# Take the input (subject and message) and un scrabble it (making spaces spaces  #
# instead of code). Also, tell the script that $subject and $message are the     #
# contents of the boxes named subject and message.                               #
##################################################################################
    &parse_form;

  $subject =  $input{'subject'};
  $message = $input{'message'};

  
##################################################################################
# The actual decodifying script													 #
################################################################################## 
sub parse_form {

   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
   if (length($buffer) < 5) {
         $buffer = $ENV{QUERY_STRING};
    }
   @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;

      $input{$name} = $value;
   }
}
  
  
##################################################################################
# Admin screen where users can type in their subject and message and submit it   #
##################################################################################
    print "Content-type: text/html\n\n";
	print "<HTML><HEAD>\n";
	print "<TITLE>$nameofsite News Admin</TITLE></HEAD>\n";
	print "<BODY><p><font color=\"#000000\" face=\"Arial\">You are now logged in and have administrator privelages.</font></p>\n";
	print "<form action=\"$scripturl\" method=\"POST\">";
	print "<p><font color=\"#FF0000\" face=\"Arial\">Subject of news article.<br></font>";
	print "<input type=\"text\" size=\"20\" name=\"subject\"></p><p><font color=\"#FF0000\" face=\"Arial\">Main message.<br></font>";
	print "<textarea name=\"message\" rows=\"13\" cols=\"58\"></textarea></p> <p><input type=\"submit\" name=\"submit\" value=\"Submit\"></p></form>";
	print "<p><font color=\"#000000\" face=\"Arial\">The News Center supports html, JavaScript and other scripting languages which can be imbedded into html pages. To make use of this, type in your code as you would a message. Remember that functions which will need to be in a <head> will not work.<br></font>";
	print "</BODY></HTML>\n";


##################################################################################
# Print the contents of the message and subject fields into the file specified   #
# in the $newspage value. The colour of the subject is $subcolour and the font   #
# is $subfont. The message colour is $memcolour and the font is $memfont.        #
##################################################################################
  open (FILE, ">$newspage") | | "Could not open the file: $!"; {
print FILE "<HTML><HEAD><TITLE>$nameofnews</TITLE></HEAD>\n";
print FILE "<body><font color=$subcolour face=\"$subfont\">$subject<br>\n";
print FILE "<font color=black>$message</p>\n";
print FILE "</body></html>\n";
close (FILE);
}
	

##################################################################################
# Take what is written by the user and display it on the page after the submit   #
# button has been pressed. This will show AFTER the file specified by the value  #
# $newspage has been opened and written to.				 		   	  	  		 #
##################################################################################

	
   [/code]

It is a news script which will take what you write and put it into a html file for you. You then just use SSI to call it into the page{s} where you want it.

Pleasse try it and give feedback.

Cheers

------------------
Thomas Minton
The JavaScript Place
Get paid $20 - $160 an hour for viewing adverts 

They have: 193 posts

Joined: Feb 2000

It's a nice little script. And very nice for a first try. Just wanted you to know, if you use:

code:

print qq| |;
[/code]

instead of

code:
print " ";
[/code]

you won't have to exscape quotation marks like \".

Richard

------------------
         [email protected] 

[email protected]

Everyone here has a website. It's just that not all are worth posting (Mine! Smiling).

They have: 314 posts

Joined: Nov 1999

Thanks for the advice and compliments Richjb!

------------------
Thomas Minton
The JavaScript Place
Get paid $20 - $160 an hour for viewing adverts

They have: 62 posts

Joined: Jun 2000

Looks cool...a news updating script, right? (I'm only semi-familiar with Perl)

------------------
Chris Bowyer
MyCoding.com: Join our Mailing List for Launch Notification!

"I'm not an insomniac, I'm a web designer."

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.