Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 60
The Melonfire Community - Trog
Logo         Community
  Trog


Copyright notice:

This article is copyright Melonfire,
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 119
2018. All rights reserved.

All source code, brand names, trademarks and other content contained herein is proprietary to Melonfire,
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 121
2018. All rights reserved.

Source code within this article is provided with NO WARRANTY WHATSOEVER. It is meant for illustrative purposes only, and is NOT recommended for use in production environments.

Copyright infringement is a violation of law.

Printed from http://www.melonfire.com/community/columns/trog/article.php?id=22



Perl 101 (part 8): Putting It To The Test
Put your Perl skills to the test with some real-world CGI applications.


Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 150

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 152

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 167

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 168

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 170
Reach Out And Touch Someone

Last time out, we discussed how Perl can be used to write simple CGI applications that reside on the server and do useful things like receiving form data. This time, we're going to take it a little further, by showing you how to make Perl do some really useful things.

Over the next few pages, we're going to show you how to track the number of visitors your page receives with a simple counter, store their comments on your Web development efforts in a basic guestbook, and write a simple mailer that can email the contents of a feedback form to the site webmaster. So keep reading - this is your chance to get to know your site visitors better!


Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 150

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 152

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 167

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 168

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 170


Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 150

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 152

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 167

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 168

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 170


Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 150

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 152

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 167

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 168

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 170
The Code...

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
$in;

# assign values to the variable depending on form METHOD

if ($ENV{'REQUEST_METHOD'} eq "GET") {

   $in = $ENV{'QUERY_STRING'};
} 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 guestbook.</font></td></tr>\n";

# 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 user

# some forward thinking here:
# since values need to be stored in a text file, create a variable called $entry
# and differentiate the different elements of each guestbook entry by the # symbol
# 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> $value</font></td></tr>\n";   
        $entry .= $value . "#";
    }
}

# end the display with a link that allows the user to view other guestbook entries
print "<tr><td align=center colspan=2 width=600><font face=Verdana size=1>Click <a href= view.cgi>here</a> to view other entries.</font></td></tr></table>";

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?


Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 150

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 152

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 167

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 168

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 170
...And The Explanation

Let's start from the top. We've first defined a variable, $in, that accepts a value from the form. As explained in our previous example, all the form variables are passed to the script as name-value pairs. However, in this case, we are passing more than one name-value pair, separated from each other with an ampersand.

Therefore, it becomes necessary to split the input string against the & to get the individual name-value pairs. Each name-value pair is stored as an element of an array. When we begin printing the data entered (for verification), it becomes necessary to again split each element of the array into individual "names" and "values" against the = symbol. Splitting things up this way also makes it simple to write the different items to a text file.

Using the "foreach" loop, we split each element of the array, as described above, into a temporary hash variable. We can then extract the name-value pair from the temporary hash and display it to the user.

Since we have to also store the values in a text file, we've created a variable named $entry, formatted it in such a way that it contains all three values entered by the user (separated by a # symbol), and dumped it into a text file. Remember to open the file in "append" mode!


Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 150

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 152

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 167

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 168

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 170
Going Backwards

So that takes care of data entry. But how about the link that allows the user to read previous guestbook entries? Well, here's the code for the CGI script that reads the file and displays all previous entries to the user.


#! /usr/bin/perl

# view.cgi - display guestbook entries

# open the file and read contents into an array

open (GBOOK, "guestbook.txt");
@entries = <GBOOK>;
close (GBOOK);

print "Content-Type: text/html\n\n";
print "<html><body>";

print "<center>";
print "<table cellspacing=5 cellpadding=5 width=600 >";

print "<tr><td align=center width=600><font face=Verdana size=2>View other comments.</font></td></tr>\n";

# at this point, each entry in the file is stored as a single element of the array

foreach $entry(@entries)
{

# use chomp() to delete the newline character from the end of each line

    chomp $entry;

# split each array element against the # delimiter into a new temporary array
# now, the first element of the temp array contains the name
# the second, the email address and the third, the comment

    @singles = split (/#/, $entry);

# display it   
    print "<tr><td width=600>";
             
    print "<table cellspacing=5 cellpadding=0 width=600 bgcolor=#D6D6D6><tr ><td align=right width=300 ><font face=Verdana size=2>Name:</font></td><td align=left width=300><font face=Verdana size=2>$singles[0]</font></td></tr>\n";   

    print "<tr><td align=right width=300><font face=Verdana size=2>Email address:</font></td><td align=left width=300><font face=Verdana size=2>$singles[1]</font></td></tr>\n";   

    print "<tr><td align=right width=300 valign=top><font face=Verdana size=2>Comments:</font></td><td align=left width=300><font face=Verdana size=2>$singles[2]</font></td></tr></table>\n";   
  
    print "</td></tr>";
}

print "</body></html>";


Now, when it's time to display the contents of the guestbook, we do things in reverse: first read the file into an array, split each element against the # delimiter we added earlier, and display it in a neatly formatted table. Simple, huh?


Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 150

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 152

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 167

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 168

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 170
Fortune Cookies

Perl also allows you to run external commands, and display the output of those commands on your Web page. Consider the following simple fortune cookie generator, which uses the "fortune" program to give you a random quote each time you reload the page.


#!/usr/bin/perl

# fortune.cgi - get a random quote

# get a quote - change your path appropriately
$quote = `/usr/games/fortune`;

# print it in a page
print "Content-Type: text/html\n\n";
print <<EOF;
<html>
<head>
<basefont face=Arial>
</head>
<body>
And your quote is:
<br>
$quote
</body>
</html>
EOF


In this case, we've executed a command by enclosing it in single quotes, and sent the output to the variable $quote. Next, we've used Perl to output an HTML page containing the quote - this page is the one you'll see when you visit the site through your browser. Each time you refresh it, you'll see a new quote.

Note the slightly different manner in which we've structured the print() statement here. The << marker indicates to Perl that what comes next is a multi-line block of text, and is to be printed as is right up to the marker (the marker in this case is the string "EOF"). In Perl-lingo, this is known as a "here document", and it comes in very handy when you need to output a chunk of HTML code.


Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 150

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 152

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 167

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 168

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/print.php on line 170


Copyright notice:

This article is copyright Melonfire,
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 193
2018. All rights reserved.

All source code, brand names, trademarks and other content contained herein and proprietary to Melonfire,
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 195
2018. All rights reserved.

Source code within this article is provided with NO WARRANTY WHATSOEVER. It is meant for illustrative purposes only, and is NOT recommended for use in production environments.

Copyright infringement is a violation of law.

Printed from http://www.melonfire.com/community/columns/trog/article.php?id=22



Copyright © 1998-
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/print.php on line 211
2018 Melonfire. All rights reserved
Terms and Conditions | Feedback