Home arrow Perl Programming arrow Page 4 - Perl 101 (part 8) - Putting It To The Test

The Code... - Perl

In the concluding article in the Perl 101 series, everythingyou've learnt so far is put to the test when you develop some real-worldCGI applications - a counter, a guest book and a form mailer.

  1. Perl 101 (part 8) - Putting It To The Test
  2. Adding Things Up
  3. Visitors Welcome!
  4. The Code...
  5. ...And The Explanation
  6. Going Backwards
  7. Fortune Cookies
  8. You Have Mail!
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 4
October 02, 2000

print this article


Here's the other half of the puzzle - the CGI script that takes care of the actual storage of form data.
#! /usr/bin/perl
# submit.cgi - accepts guestbook data and writes to file
# define a variable that accepts a value from the form
# assign values to the variable depending on form METHOD
if ($ENV{'REQUEST_METHOD'} eq "GET") {
} else {
$in = <STDIN>;
# fix URL-encoded strings
$in =~ s/\+/ /g;
# all variables are passed to the script as name-value pairs separated by &
# split the input string on the basis of &
@detail = split (/&/, $in);
# display data entered by user again
print "Content-Type: text/html\n\n";
print "<html><body>";
print "<center>";
print "<table cellspacing=5 cellpadding=5 width=600 bgcolor=#D6D6D6>";
print "<tr><td align=center colspan=2 width=600><font face=Verdana
size=2>Thank you for entering the following details in the
# each name-value pair is stored as an element of an array
# now take each element of the array and split to form a hash
# on the basis of the = symbol
# using the "foreach" loop, we split each element of the array into a
"temporary" hash
foreach $details(@detail)
%details = split (/=/, $details);
# now extract the name-value pair from the temporary hash to display to the
# some forward thinking here:
# since values need to be stored in a text file, create a variable called
# and differentiate the different elements of each guestbook entry by the #
# this comes in useful later, wait and see!
while (($name, $value) = each %details)
print "<tr><td align=right width=300><font face=Verdana size=2>Your
$name:</font></td><td align=left width=300><font face=Verdana size=2>
$entry .= $value . "#";
# end the display with a link that allows the user to view other guestbook
print "<tr><td align=center colspan=2 width=600><font face=Verdana
size=1>Click <a href= view.cgi>here</a> to view other
print "</body></html>";
# make things simple by ensuring that every entry in the file is on a
single line
# by terminating the entry with a newline
$entry = $entry."\n";
# this is where the file write actually happens
# remember to open the file in "append" mode to avoid losing previous data
# make sure that you have permission to write to this file
open (GBOOK, ">> guestbook.txt");
print GBOOK "$entry";
close (GBOOK);
# whew!

Now, how about an explanation?

This article copyright Melonfire 2000. All rights reserved.

>>> More Perl Programming Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: