A Hello World Program

We need to write a program which, when placed in the cgi-bin directory of the server, executes. This is the famous "hello world" program common to almost all introductory texts.
#!/usr/local/bin/perl -w
use strict;

print "Content-Type: text/html\n\n";#this sends html text, needs 
#print "Content-Type: text/plain\n\n";#this sends plain text, need \n # use the second form, and reload the URL to see what happens print ("<h1>Here I am<//h1>\n"); print ("<HTML>"); print "<body>"; print "This is the first line."; print "This is the second line."; print "This doesn't work in html mode,\n, SEE!"; print "This is the third line."; print "<h1>This is a heading</h1>"; print "This doesn't work in plain mode,<br>,SEE!<br>"; print "This is the fourth line."; print "<font color=red size=+2> Here's some big red text<br></font>"; print "<h1>This is another heading</h1>"; print "</html>";
and here is the link to see how this code works.
Once the above text has been placed in the cgi-bin directory, you needs to change its ``permissions'' so that the WWW can access the program. To do this you issues a statement such as
chmod a+x hello_world.pl
if, indeed, the name of the program is ``hello_world.pl''. If you issues a statement:
ls -al hello_world.pl
you will see that hello_world is executable, i.e., that there are 3 x's showing.
-rwxr-xr-x
This makes the program executable by the owner, his group, and the world. Now, when you address this program via
http://www.your.server.address.whatever/~your_account/cgi-bin/hello_world.pl
this program will ``execute'' on the server, and print in your local browser.
All the print statements starting with "Here I am" are yours to write as you wish, assuming that you know html code (using the greater than and less than symbols). Alternatively, you can send plain text, using the carriage return symbol, just by changing the header. We will always be sending html code here.
What the Internet is providing is a scheme for declaring the user's browser as the target for the STDOUT print command, i.e., the default Perl printer. (Later we will show how the STDIN is assigned to the browser's input fields.) In order to display material, all we need do is "print" it while STDOUT is assigned to the WWW, and voila, what appears on a browser screen is what our Perl program wrote!

A program to sense the environment of the server

Now we introduce a program written by Terrence Jordan:
#!/usr/local/bin/perl 


#from Terrence Jordan
# July, 1997, a super programmer.
# he has asked me to cite him in the following manner:
# Terence Jordan mailto:tatewake@mindless.com
# Inspired Software, http://inspired.netstreet.net/
#
#
#
#
#
#This subroutine and such takes all ENVironmental variables
# and "spits 'em back at you".
sub cgiparse{
	if ($ENV{'REQUEST_METHOD'} eq "POST"){
		read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
	}
	else {
		$buffer = $ENV{'QUERY_STRING'};
	}
	local(@query_strings) = split("&",$buffer);
	foreach $q(@query_strings){
		$q =~ s/\+/ /g;
		($attr,$val) = split("=", $q);
		$val =~ s/%/\n/g;
		local($tmpval);
		foreach (split("\n",$val)){
			if(m:%(\w\w):){
				local($binval) = hex($1);
				if(($binval>0)&&($binval<256)){
					local($htmlval) = pack("C",$binval);
					s/%$1/$htmlval/;
				}
			}
			$tmpval .= $_;
		}
		$parseit{$attr} = $tmpval;
	}
	%parseit;
}
print "Content_Type: text/plain\n\n";
print "\n";
&cgiparse();
#THIS IS THE KEY STATEMENT COMING UP
print map {"\n"}keys %ENV;

print "\n\n\n";
and here is the link to see how this code works.
Because of the kind of work we will be doing, we will be using CGI.pm, the Lincoln Stein package which makes programming the WWW such a breeze. We wish to obtain the same information as above using CGI.pm as an introduction.
#!/usr/local/bin/perl -w
use CGI;
$query = new CGI;
print $query->header;

print "<hr>";
print "The script's name = ",$query->script_name(),"<br>";
print "The script's referer = ",$query->referer(),"<br>";
print "The script's remote_host = ",$query->remote_host(),"<br>";
print "The script's request_method = ",$query->request_method(),"<br>";
print "The script's server_name = ",$query->server_name(),"<br>";
print "The script's server_port = ",$query->server_port(),"<br>";
print "The script's user_agent = ",$query->user_agent(),"<br>";
print "<hr>";
print $query->end_html;

Click here to see how this code works.
You will have to look at the CGI.pm documentation to understand all of this, but we will clarify a bit of it:
  1. use CGI; is a required statement to guarantee that the program will have access to the library in question.
  2. $query = new CGI; creates what is in essence our own private version of the CGI.pm library, which we refer to using the variable $query in a special way, vide infra.
  3. print $query->header; writes to STDOUT the required header information which we needed to do explicitly ourselves prior to using CGI.pm.
  4. print $query->end_html; closes the entire script
In between the start_html and end_html statements, we print various self evident identifiers, chosen from the documentation's list of HTTP variables. Notice that we print html tags, and they have the desired effect!

Last revision/editing, September 30, 1997