Comatose 290 Taboo Programmer Team Colleague

Prelude
Perl is an excellent language, versatile, powerful, and not too difficult to learn. I'm not going to go into detail about Perl, and explain why we have to put #!/usr/bin/perl on the first line, or how using modules work, but I will explain how to check for information from a web page with this tutorial. If this tutorial gets is a little bigger than you expect, I apologize, but I tend to get a bit long winded when it comes to great subjects. So, without any further delay.

Preparation
One thing about Perl CGI's, is making sure that we include CGI.pm. This simplifies a lot of information for us (such as parsing headers, and printing headers), and makes our programming experience with CGI's taste a little better. So, we have to use the CGI module to give us these functions. How do we do that? Real Easy, We Tell Perl We Want To use the standard functions of the CGI Module like so:

#!/usr/bin/perl

use CGI qw(:standard);

Check For Submission
Ok, so we have some functions that help us out. Now what? Well, one thing that we have to do is to see if our CGI (Perl Script) is being called by another page (Through a submit) or if someone decided to get a little too enthusiastic, and stuck our CGI URL directly into their web browser. If Mr. Anxious decided to navigate directly to our CGI, we have a couple of options. We can choose to display a "main page" that is basically the page we want them to fill out and submit to us (wow, our CGI could create the page we want them to submit to us!? Cool!), or (as we will do here), we will create a page that informs the surfer, that they have neglected to give us information from another page. The first step to checking for submission, is to look for a parameter. If there is no parameter, then we need to let them know that we need more information. How do we check for the parameter? We use param.
Param is a nifty little function that when called without values, returns true or false. So, if the return value of param() is true, then some kind of info was passed to us. Otherwise, no information was passed, and we need to alert the user of that.

if (param()) {
     # Information Was Passed To The CGI
     &start_page;   # /* This Line Jumps Down To The start_page Sub */
} else {
     # Information Was NOT Passed To The CGI
     &complain;      # /* This Line Jumps Down To The complain Sub */
}

The Complain Sub
If No Information was passed to the CGI, we need to complain about it, so that the user knows that they need to give us some information before we can do anything. I Prefer to use a little javascript, that redirects them (history.go(-1);) to the page that they just came from, but that's unethical, and kind of wrong. Either way, we now need to create a page that loads when they have reached our site by way of other than passing us paramaters. This sub just makes a little simple page, and here is that sub.

sub complain
{
     print header;
     print <<HTMLPAGE;
	<HTML>
	<HEAD>
		<TITLE>CGI Error: No Data</TITLE>
	</HEAD>
	<BODY BGCOLOR="#000000" TEXT="#FFFFFF">
		<CENTER>
			<H1>I Need More Information</H1>
			<HR><BR>
			<INPUT TYPE="BUTTon" VALUE="Back" onClick="history.go(-1);">
		</CENTER>
	</BODY>
	</HTML>
HTMLPAGE
}

A couple of Perl pieces are worthy to note in this sub. The header function that we use at the very top of the sub. This actually prints the content-header, saving you the hassle of escaping all the quotes and printing the entire header yourself. The other piece, which is a real handy trick, is the line: print <<HTMLPAGE. What's really cool about this, is that everything that falls between print <<HTMLPAGE and the label found at the bottom of the sub HTMLPAGE (you'll notice that HTMLPAGE is all the way to the left of the sub, this is required for it to work properly). Everything between is rendered as a string. This is important, because it saves you TPS (tooth pick syndrom) where you are escaping everything in quotes. This gives you the freedom to build the page, without a thousand print statements, and makes debugging a lot easier on you. Everything is HTML and/or Javascript, and should be familiar to you.

The start_page Sub
Ok, So far we have a CGI, that if you navigate to it, throws a little fit, and gives you a back button. What if they did pass a parameter to us? What do we do now!? Well, for right now, we are going to make a page:

sub start_page
{
     print header;
     print <<HTMLPAGE;
	<HTML>
	<HEAD>
		<TITLE>Thank You For Your Submission</TITLE>
	</HEAD>
	<BODY BGCOLOR="#000000" TEXT="#FFFFFF">
		<CENTER>
			<H1>Thank You!</H1>
			<HR><BR>
			<INPUT TYPE="BUTTON" VALUE="Back" onClick="history.go(-1);">
		</CENTER>
	</BODY>
	</HTML>
HTMLPAGE
}

Right now, This function doesn't do anything different from the complain sub!!!! The only difference is the wording! That's ok. We only need 2 pages for this simple project. One that thanks them for their submission, and one that complains if they didn't submit!

Checking Data Conclusion
Right now, This CGI is very simple. It doesn't do anything special, however, it is a great template or skeleton for building larger, more robust CGI's, with many pages! There have been entire domains coded inside of a single CGI. In the next tutorial, we will go over reading specific data from a web page's form. Here is the template in it's entirety:

#!/usr/bin/perl

use CGI qw(:standard);

if (param()) {
     # Information Was Passed To The CGI
     &show_page;   # /* This Line Jumps Down To The show_page Sub */
} else {
     # Information Was NOT Passed To The CGI
     &complain;      # /* This Line Jumps Down To The complain Sub */
}

sub start_page
{
     print header;
     print <<HTMLPAGE;
	<HTML>
	<HEAD>
		<TITLE>Thank You For Your Submission</TITLE>
	</HEAD>
	<BODY BGCOLOR="#000000" TEXT="#FFFFFF">
		<CENTER>
			<H1>Thank You!</H1>
			<HR><BR>
			<INPUT TYPE="BUTTON" VALUE="Back" onClick="history.go(-1);">
		</CENTER>
	</BODY>
	</HTML>
HTMLPAGE
}

sub complain
{
     print header;
     print <<HTMLPAGE;
	<HTML>
	<HEAD>
		<TITLE>CGI Error: No Data</TITLE>
	</HEAD>
	<BODY BGCOLOR="#000000" TEXT="#FFFFFF">
		<CENTER>
			<H1>I Need More Information</H1>
			<HR><BR>
			<INPUT TYPE="BUTTon" VALUE="Back" onClick="history.go(-1);">
		</CENTER>
	</BODY>
	</HTML>
HTMLPAGE
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.