The Wayback Machine - https://web.archive.org/web/20240808195332/https://www.geeksforgeeks.org/perl-cgi-programming/
Open In App

Perl | CGI Programming

Last Updated : 01 May, 2019
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In Perl, CGI(Common Gateway Interface) is a protocol for executing scripts via web requests. It is a set of rules and standards that define how the information is exchanged between the web server and custom scripts. Earlier, scripting languages like Perl were used for writing the CGI applications. And, CGI code called by HTTP server was referred to as the CGI script. Later, the growth of the web caused the increase in need of dynamic content through which CGI applications which were written in other languages instead of Perl became more popular and in demand and were referred as scripts only. The specifics of how the script is executed by the server are determined by the server. CGI applications can perform nearly any task. For example, you can access databases, hold telnet sessions, create Web pages on-the-fly and generate graphics, etc. CGI is having a very simple concept, but creating a CGI application requires real programming skills.
 

What is CGI?

Common Gateway Interface (CGI) is nothing more than a protocol which defines the interaction of web servers with some executable programs in order to produce dynamic web pages. Basically, it shows how the web server sends information to the program and the program sends the information back to the web server which in turn can be sent back to the browser. Between web servers and external programs, it is considered as the standard programming interface.
CGI stands for:

Common: Interaction with many different OS.
Gateway: It provides the way to the users in order to gain access to different programs such as picture generator or databases etc.
Interface: It uses a method for interaction with Web server.

CGI programs can send many types of data or media, like documents, images, audio clips, etc. Most Web sites use CGI with fields for input and deals of dynamic content on the Web is done mainly using CGI. It is a method through which a web server can get/send the data from/to databases, documents, and other programs respectively, and then present that data to viewers through the web.
 

CGI Architecture

 

In the above figure, with the help of HTTP (Hyper Text Transfer Protocol) web browser which is running on client machine exchanges information with the web server. Since, CGI program and web server normally run on the same system on which the web server resides, depending on the request from the browser, web server either provides the document from its own document directory or executes a CGI program.

Simple CGI Program:
Here is a simple program to understand the working of CGI Programming in Perl. Here, we will be getting inputs in the HTML program and then run the CGI script for the resulting web page.
Since, HTML determines a number of input fields which we can use for passing the user data to the CGI program. Here, is the HTML form that makes use of these input fields like text field, scroll-down list, check-boxes and a submit button too for submitting the user data to the web server.




<html>
<head>
    <title>GfG Test Example Form</title>
</head>
<body>
    <h1>CGI-Example Form</h1>
    <h3><p>Information Required.</p></h3>
    <form action="/cgi-bin/script.pl" method="Post">
    <table>
        <tr>
        <td>Name:</td>
        <td><input type="text" name="name"><td>
        </tr>
        <tr>
        <td>Gender:</td>
        <td><select name="gender" size="1">
            <option>Female</option>
            <option>Male</option>
            <option>Transgender</option>
        </select></td>
        </tr>
        <tr>
            <td>Profession:</td>
            <td><input type="text" name="profession"><td>
        </tr>
        <tr>
        <td>Sports:</td>
            <td><input type="checkbox" name="sport"
                    value="Cricket">Cricket
                <input type="checkbox" name="sport"
                    value="Hockey">Hockey
                <input type="checkbox" name="sport"
                    value="TableTennis">TableTennis
                <input type="checkbox" name="sport"
                    value="Football">Football</td>
        </tr>
        <tr>
        <td colspan="2"><input type="submit"></td>
        </tr>
    </table>
    </form>
</body>
</html>                    


NOTE: Each of the input element in the HTML Form has a name attribute which is important when we need to access data. And, all the input elements are inside the <form> tag. Along with that, <form> tag also has an “action” attribute in it which determines the CGI script which will be called on submitting the form.

Perl-CGI script for the above form :




#!"c:\xampp\perl\bin\perl.exe"
  
use strict;
use CGI ':standard';
  
my $name = param('name');
my $gender = param('gender');
my $profession = param('profession');
my @sports = param('sport');
  
my $list;
  
if (@sports) 
{
    $list = join ', ', @sports;
} 
else 
{
    $list = 'Null';
}
  
print header,
start_html(-title=>$name),
h1("Hello, $name"),
h3 p('You have Submitted the following Data:'),
h4 table(Tr(td('Name:'),
h4 td($name)),
h4 Tr(td('Gender:'),
h4 td($gender)),
h4 Tr(td('Profession:'),
h4 td($profession)),
h4 Tr(td('Sports:'),
h4 td($list))),
end_html;


CGI.pm: CGI.pm (Use CGI;) is a large and widely used Perl module for programming CGI web applications, which provides a consistent API for receiving and processing user input. We need to write a Perl script in order to return an HTTP response header(Content-type: html/text) and HTML in the body but CGI.pm handles it easily for us (no need to hard-code for HTTP response header).
It has a function named “param” which sends back the details/information about the data which we have passed to the script. It returns the values associated with that name when it is given the name of a parameter. Names of the input elements in the HTML form page are the CGI parameter names.

NOTE: After, submitting the form the web page that we will be getting is shown below. If you notice in the URL box of the web browser you will see that URL has been changed now.



Previous Article
Next Article

Similar Reads

Perl | File Upload in CGI
Perl is general-purpose programming language, specially designed for text manipulation and in present time used for various task including system administration, web development, network programming, GUI development, and in other more area. In Perl, CGI(Common Gateway Interface) is a protocol for executing scripts via web requests or in other words
3 min read
Perl | GET vs POST in CGI
In Perl, Common Gateway Interface (CGI) is nothing more than a protocol which defines the interaction of web servers with some executable programs in order to produce dynamic web pages. Basically, it shows how the web server sends information to the program and the program sends the information back to the web server which in turn can be sent back
4 min read
Perl | Security Issues in CGI
Before diving into the security issues with CGI and Perl, let's know what Perl is. Perl stands for Practical Extraction and Reporting Language. It was built in 1987 by Larry Wall. Its a smart Object oriented language since it takes some useful features from C and BASIC as well. This programming language used to be the master of web programming as i
4 min read
Perl | CGI Security
The CGI stands for the Common Gateway Interface which is a defined protocol for writing dynamic codes on the web. It is also used to execute the scripts online. Perl was accepted as an executable language for HTML pages on the web. Perl is similar to any other CGI language in which, when a code is executed, it creates an interface with the base ope
5 min read
Debugging Perl CGI Scripts
Perl is a cross-platform, open-source computer programming language that is extensively used in both the commercial and private sectors. Perl is popular among Web developers due to its adaptable, ever-evolving text-processing and problem-solving capabilities. Although Perl was initially developed for text editing, its flexibility makes it a versati
5 min read
Perl | Static and Dynamic content in CGI
In the sector of web improvement, Perl has long been a famous choice for growing dynamic content material in CGI (Common Gateway Interface) packages. CGI allows web servers to interact with external packages, allowing the generation of dynamic net pages. In this text, we will explore the standards of static and dynamic content in CGI using Perl, an
3 min read
Perl | Basic Syntax of a Perl Program
Perl is a general purpose, high level interpreted and dynamic programming language. Perl was originally developed for the text processing like extracting the required information from a specified text file and for converting the text file into a different form. Perl supports both the procedural and Object-Oriented programming. Perl is a lot similar
10 min read
Perl | Socket Programming
Socket programming in Perl is a way of connecting two nodes on a network to communicate with each other. Basically, it is a one-way Client and Server setup where a Client connects, sends messages to the server and the server shows them using socket connection. One socket (node) listens on a particular port at an IP, while other socket reaches out t
4 min read
Object Oriented Programming (OOPs) in Perl
Object-oriented programming: As the name suggests, Object-Oriented Programming or OOPs refers to languages that uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc in programming. The main aim of OOP is to bind together the data and the functions that operate on
7 min read
Plain Old Text Documentation in Perl Programming
POD is a markup language used for writing documentation for Perl, Perl programs, and Perl modules. It is simple to use. There are various translators available for converting Pod to different formats such as plain text, HTML, man pages, and more. Pod markup comprises of three kinds of paragraphs : Ordinary Paragraph: For bold, italic, code-style, h
2 min read
Article Tags :
three90RightbarBannerImg