Perl | CGI Programming

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

#!"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;

chevron_right


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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.