Home Index

Shutting down a Windows NT/2000
 
Date: March  01 2001
Subject: Online Programmer Ezine, Volume 2, No.3
Article: Start with Perl CGI now! Part I - What's CGI and Hello world!

 

In issue 1-2 of ezine we had an article about cgi programming.
In that article we saw a small cgi program using C programming
language.

In this issue we will start a series of small articles on cgi
programming in Perl. For those new to cgi programming we start
again from zero. These articles are based on small examples 
so you can test them, change them and learn more than the 
small example.

When you call a cgi program over internet by referring to its 
address in your browser (either directly or indirectly by a 
html page) a web server program receives your request. If you 
have requested an html file or an image file etc. it easily 
sends back the file.

In other side if you have requested a cgi program, web server 
does not send cgi program file but runs it and sends back the 
result of running the cgi as a html web page.

So actually a cgi program will create an html web page for 
web server and this is what web server sends back to browser.

If running a cgi does not create a valid html or return an 
error web server will send an error message to the browser.

In this issue we will write a very simple cgi program with 
Perl language. We will first see the program and then will 
describe each line.


Example 1 - hello.pl:

#!/usr/bin/perl

print "Content-type: text/html\n\n";

print "<HTML>\n";
print "<HEAD><TITLE>Perl Hello World !</TITLE></HEAD>\n";
print "<CENTER><H1>Hello World!</H1></CENTER>\n";
print "</HTML>";


As we told later, this program will output a valid html file. 
Web server will get the result and send it to browser. Below
is the actual html file that browser will receive.


<HTML>
<HEAD><TITLE>Perl Hello World !</TITLE></HEAD>
<CENTER><H1>Hello World!</H1></CENTER>
</HTML>

And now description of program.

1- In first line we see the path to Perl interpreter 
executable file on server. This is very important on Unix 
type servers. If this path does not point to the correct 
place of Perl interpreter then our cgi program will not run.

If you are using a NT server with Active state's Perl 
(R) then you will not need the first line. Active State 
Perl will be used automatically by web server to run 
the program.

2- Second line output will create a header that is needed 
by web server to accept output of our program as an html 
file. You will add this line to your programs exactly as 
this line if your program must create an html file.

Pay attention that two "\n" or new line marks are necessary
for the header.

Print command is for sending a text to standard output. If 
you run a Perl program from command line the output of print
command will be sent to your console. If you run it inside 
a web server ,web server will use the results.

3- In next 4 lines we actually create our html file, as we 
want it on the web server. "\n" marks (new line) will force 
program to output results in a new line after it.

So we will see have above html code in our browser.

In more complicated programs such as database programs we 
will construct the output page from results of longer 
processes.

In next issues we will see more complicated examples.

If you have any question about Perl and cgi programming you 
can ask it in our programming forums.

http://op.htmsoft.com/forums



Home Index
© 2000 OnlineProgrammer (HtmSoft.com) . All Rights Reserved