Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

perl

What is CGI programming in Perl?

Dian Us Suqlain

Common Gateway Interface (CGI) is a protocol or set of standards that executes scripts via web requests and explains how information exchange happens between the web server and a custom script.

CGI stands for:

  • Common - C: controls the interaction between multiple Operating systems (OS).

  • Gateway - G: through the gateway, we can access various programs, such as databases and image generators.

  • Interface - I: uses a well-defined method to interact with a Web server.

CGI application usage

In Perl, creating a CGI application requires professional coding skills. But once we develop an application, we can use it almost anywhere, such as:

  • access various databases
  • hold multiple telnet sessions
  • create interactive Web pages
  • generate graphics
CGI Programming usage

CGI standard application interface

CGI is a standard application interface as it displays how a web server sends information to an external program.

That external program sends the processed data back to the webserver. The information is then delivered back to the browser.

CGI application architecture

With the help of HyperText Transfer Protocol (HTTP) running on a client machine, we can exchange information with the webserver.

As CGI programs and web servers usually run on the same system as the webserver, the webserver provides the document from the document directory or executes a CGI program depending on the request from the browser.

Consider the following illustration that demonstrates the architectural diagram of CGI:

CGI architecture

CGI programming

With the help of CGI programming, we can send data such as texts, images, audio or voice notes, files, documents, etc.

CGI programming isn’t complicated, but a user must know how to debug, execute and structure a CGI code file.

Code

As we have learned what CGI is and how we use it. Let’s bring our learning into reality by executing the code below.

Write these commands in the terminal below to install the required modules for CGI in Perl.

  1. apt-get update: downloads package information from all configured sources.

  2. apt-get install perl. Then press (shift + y) and wait for the packages to get installed. This command installs the latest version of Perl.

  3. apt-get install apache2. Then press (shift + y) and wait for the packages to get installed.

  4. apt-get install sudo: installs commands related to sudo. It allows you to run programs with the security privileges of another user.

  5. sudo apt-get install apache2 libapache2-mod-wsgi python-dev. Then press (shift + y).

  6. a2enmod cgid: a script that enables the specified module within the apache2 configuration.

  7. service apache2 restart: Apache the most commonly used Web server on Linux systems.

  8. cd /usr/lib/cgi-bin: locates and opens the directory named /usr/lib/cgi-bin.

  9. Now, create a Perl CGI by typing this: vi /usr/lib/cgi-bin/test.pl.

In the file, write the following code for a test run:

#!/usr/bin/perl
print "Content-Type: text/html\n\n";
print "<html>";
print "<head>";
print "<title>CGI Perl</title>";
print "</head>";
print "<body>";
print "<h1>This is Perl CGI Test file.</h1>";
print "<p>Common Gateway Interface (CGI) is a protocol or set of standards that executes scripts via web requests and explains how information exchange happens between the web server and a custom script.</p>";
print "<h2>Hello Word! This is my first CGI program</h2>";
print "</body>";
print "</html>";
  1. After typing the above code, press (ctrl + c) two times and write :wq, and hit enter to exit the code file.

  2. chmod 755 /usr/lib/cgi-bin/test.pl: we use chmod command to change the read and write operations of a file. 755 means full permissions for the owner and read and execute permission for others.

  3. Now, open a new browser tab and write your IP address plus the path of Perl file which is /cgi-bin/test.pl.. In this example, the IP address of the webserver is 172.17.0.2.

Terminal 1
Terminal

Explanation

The terminal commands from 1 to 6 install the necessary frameworks, packages, and libraries needed to execute the code file written in point 9 properly.

CGI module runs using an HTML form as written above. This form will get displayed on your local machine when you type in the IP address along with the path of the CGI file.

For example; the above example was executed on a machine having the IP address 172.17.0.2 so, the web address becomes: 172.17.0.2/cgi-bin/test.CGI.

RELATED TAGS

perl

CONTRIBUTOR

Dian Us Suqlain
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring