Open In App
Related Articles

Perl | CGI Programming

Like Article
Save Article
Report issue
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.
    <title>GfG Test Example Form</title>
    <h1>CGI-Example Form</h1>
    <h3><p>Information Required.</p></h3>
    <form action="/cgi-bin/" method="Post">
        <td><input type="text" name="name"><td>
        <td><select name="gender" size="1">
            <td><input type="text" name="profession"><td>
            <td><input type="checkbox" name="sport"
                <input type="checkbox" name="sport"
                <input type="checkbox" name="sport"
                <input type="checkbox" name="sport"
        <td colspan="2"><input type="submit"></td>

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 :
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;
    $list = 'Null';
print header,
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))),

            (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 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.

Last Updated : 01 May, 2019
Like Article
Save Article
Share your thoughts in the comments
Similar Reads