0% found this document useful (0 votes)
75 views

A Program That Illustrates The Use of The Matching Operator

The document provides information about Common Gateway Interface (CGI) including: 1. CGI allows external programs to interface with web servers like HTTP servers and the current CGI specification is CGI/1.1. 2. When a browser requests a URL, the web server sends back the file or runs a CGI program which outputs dynamic content. 3. CGI programs are executed by the web server and kept in the /cgi-bin directory with extensions like .cgi. An example "Hello World" CGI program in Perl is provided. 4. Important CGI concepts covered include HTTP headers, environment variables passed to CGI programs, and a sample program to view all environment variables.

Uploaded by

sreenimol
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

A Program That Illustrates The Use of The Matching Operator

The document provides information about Common Gateway Interface (CGI) including: 1. CGI allows external programs to interface with web servers like HTTP servers and the current CGI specification is CGI/1.1. 2. When a browser requests a URL, the web server sends back the file or runs a CGI program which outputs dynamic content. 3. CGI programs are executed by the web server and kept in the /cgi-bin directory with extensions like .cgi. An example "Hello World" CGI program in Perl is provided. 4. Important CGI concepts covered include HTTP headers, environment variables passed to CGI programs, and a sample program to view all environment variables.

Uploaded by

sreenimol
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

1

1. A program that illustrates the use of the matching operator.


#!/usr/local/bin/perl
print ("Ask me a question politely:\n");
$question = <STDIN>;
if ($question =~ /plz/) {
print ("Thank you for being polite!\n");
} else {
print ("That was not very polite!\n");
}
2. A word-count program that handles multiple spaces between words.
#!/usr/local/bin/perl
$wordcount = 0;
$line = <STDIN>;
while ($line ne "") {
chop ($line);
@words = split(/ +/, $line);
$wordcount += @words;
$line = <STDIN>;
}
print ("Total number of words: $wordcount\n");
3 . A program that counts the number of input lines containing the word the.
#!/usr/local/bin/perl
$thecount = 0;
print ("Enter the input here:\n");
$line = <STDIN>;
while ($line ne "") {

2
if ($line =~ /\bthe\b/) {
$thecount += 1;
}
$line = <STDIN>;
}
print ("Number of lines containing 'the': $thecount\n");

What is CGI ?
The Common Gateway Interface, or CGI, is a set of standards that define how information is
exchanged between the web server and a custom script.
The CGI specs are currently maintained by the NCSA and NCSA defines CGI is as follows:
The Common Gateway Interface, or CGI, is a standard for external gateway programs to
interface with information servers such as HTTP servers.
The current version is CGI/1.1 and CGI/1.2 is under progress.
Web Browsing
To understand the concept of CGI, lets see what happens when we click a hyper link to browse a
particular web page or URL.
Your browser contacts the HTTP web server and demand for the URL ie. filename.
Web Server will parse the URL and will look for the filename in if it finds that file then
sends back to the browser otherwise sends an error message indicating that you have
requested a wrong file.
Web browser takes response from web server and displays either the received file or error
message.
However, it is possible to set up the HTTP server so that whenever a file in a certain directory is
requested that file is not sent back; instead it is executed as a program, and whatever that program
outputs is sent back for your browser to display. This function is called the Common Gateway
Interface or CGI and the programs are called CGI scripts. These CGI programs can be a PERL
Script, Shell Script, C or C++ program etc.
CGI Architecture Diagram

Web Server Support & Configuration


Before you proceed with CGI Programming, make sure that your Web Server supports CGI and it is
configured to handle CGI Programs. All the CGI Programs be executed by the HTTP server are kept
in a pre-configured directory. This directory is called CGI Directory and by convention it is named
as /cgi-bin. By convention PERL CGI files will have extention as .cgi.
First CGI Program
Here is a simple link which is linked to a CGI script called hello.cgi. This file is being kept in /cgibin/ directory and it has following content. Before running your CGI program make sure you have
chage mode of file using chmod 755 hello.cgi UNIX command.
#!/usr/bin/perl
print "Content-type:text/html\r\n\r\n";
print '<html>';
print '<head>';

4
print '<title>Hello Word - First CGI Program</title>';
print '</head>';
print '<body>';
print '<h2>Hello Word! This is my first CGI program</h2>';
print '</body>';
print '</html>';
1;
If you click hello.cgi then this produces following output:
Hello Word! This is my first CGI program

This hello.cgi script is a simple PERL script which is writing its output on STDOUT file ie. screen.
There is one important and extra feature available which is first line to be printed Contenttype:text/html\r\n\r\n. This line is sent back to the browser and specifiy the content type to be
displayed on the browser screen. Now you must have undertood basic concept of CGI and you can
write many complicated CGI programs using PERL. This script can interact with any other exertnal
system also to exchange information such as RDBMS.
HTTP Header
The line Content-type:text/html\r\n\r\n is part of HTTP header which is sent to the browser to
understand the content. All the HTTP header will be in the following form
HTTP Field Name: Field Content
For Example
Content-type:text/html\r\n\r\n
There are few other important HTTP headers which you will use frequently in your CGI
Programming.
Header

Description

Content-type: String

A MIME string defining the format of the file being returned. Example
is Content-type:text/html

Expires: Date String

The date the information becomes invalid. This should be used by the
browser to decide when a page needs to be refreshed. A valid date string
should be in the format 01 Jan 1998 12:00:00 GMT.

Location: URL String

The URL that should be returned instead of the URL requested. You can
use this filed to redirect a request to any file.

Last-modified: String

The date of last modification of the resource.

Content-length: String

The length, in bytes, of the data being returned. The browser uses this
value to report the estimated download time for a file.

Set-Cookie: String

Set the cookie passed through the string

5
CGI Environment Variables
All the CGI program will have access to the following environment variables. These variables play
an important role while writing any CGI program.
Variable Name

Description

CONTENT_TYPE

The data type of the content. Used when the client is sending attached
content to the server. For example file upload etc.

CONTENT_LENGTH

The length of the query information. It's available only for POST
requests

HTTP_COOKIE

Return the set cookies in the form of key & value pair.

HTTP_USER_AGENT

The User-Agent request-header field contains information about the


user agent originating the request. Its name of the web browser.

PATH_INFO

The path for the CGI script.

QUERY_STRING

The URL-encoded information that is sent with GET method request.

REMOTE_ADDR

The IP address of the remote host making the request. This can be
useful for logging or for authentication purpose.

REMOTE_HOST

The fully qualified name of the host making the request. If this
information is not available then REMOTE_ADDR can be used to get
IR address.

REQUEST_METHOD

The method used to make the request. The most common methods are
GET and POST.

SCRIPT_FILENAME

The full path to the CGI script.

SCRIPT_NAME

The name of the CGI script.

SERVER_NAME

The server's hostname or IP Address

SERVER_SOFTWARE The name and version of the software the server is running.
Here is small CGI program to list out all the CGI variables. Click this link to see the result Get
Environment
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "<font size=+1>Environment</font>\n";
foreach (sort keys %ENV)
{
print "<b>$_</b>: $ENV{$_}<br>\n";
}
1;
Output
Environment DOCUMENT_ROOT: /var/www/tutorialspoint
GATEWAY_INTERFACE: CGI/1.1

6
HTTP_ACCEPT: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
HTTP_ACCEPT_CHARSET: ISO-8859-1,utf-8;q=0.7,*;q=0.7
HTTP_ACCEPT_ENCODING: gzip, deflate
HTTP_ACCEPT_LANGUAGE: en-us,en;q=0.5
HTTP_CONNECTION: keep-alive
HTTP_COOKIE: __utma=55973678.2136971353.1412656748.1412656748.1412831800.2;
__utmz=55973678.1412831800.2.4.utmccn=(organic)|utmcsr=google|
utmctr=using+perl+for+cgi+programming|utmcmd=organic; __atuvc=21%7C41;
__utmb=55973678; __utmc=55973678
HTTP_HOST: www.tutorialspoint.com
HTTP_KEEP_ALIVE: 115
HTTP_REFERER: http://www.tutorialspoint.com/perl/perl_cgi.htm
HTTP_USER_AGENT: Mozilla/5.0 (X11; Linux i686; rv:2.0) Gecko/20100101 Firefox/4.0
PATH: /sbin:/usr/sbin:/bin:/usr/bin
QUERY_STRING:
REMOTE_ADDR: 117.211.161.156
REMOTE_PORT: 33815
REQUEST_METHOD: GET
REQUEST_URI: /cgi-bin/get_env.cgi
SCRIPT_FILENAME: /var/www/cgi-bin/get_env.cgi
SCRIPT_NAME: /cgi-bin/get_env.cgi
SERVER_ADDR: 66.155.39.108
SERVER_ADMIN: [email protected]
SERVER_NAME: www.tutorialspoint.com
SERVER_PORT: 80
SERVER_PROTOCOL: HTTP/1.1
SERVER_SIGNATURE:
SERVER_SOFTWARE: Apache

You might also like