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.
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.
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.
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 :
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.
- Perl | Socket Programming
- UNIVERSAL Package in Perl Programming
- Perl - Attributes in Object Oriented Programming
- Object Oriented Programming (OOPs) in Perl
- Plain Old Text Documentation in Perl Programming
- Perl | Basic Syntax of a Perl Program
- Perl Tutorial - Learn Perl With Examples
- Perl vs C/C++
- Perl | Classes in OOP
- Perl | CGI Security
- Use of print() and say() in Perl
- Coercions in Perl
- Perl | given-when Statement
- What is the difference between Perl and PHP ?
- while loop in Perl
- Julia vs Perl
- Perl vs Python
- Perl vs Java
- Perl | oct() Function
- Perl | log() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.