Web Perl   «Prev  Next»

Lesson 7CGI and the forms interface
ObjectiveExplore CGI as an interface to the Web server.

CGI and Forms Interface

Perl-CGI (Common Gateway Interface) acts as an interface between Perl scripts and a web server, such as Apache, allowing dynamic content generation and web-based interactivity. Here's how it works:
  1. Request Handling
    • When a client (browser) sends an HTTP request to the Apache server for a resource that invokes a CGI script, the server identifies the request based on the script's location (typically in a cgi-bin directory or through a configuration).
    • The Apache web server invokes the CGI script and passes information about the request to the script.
  2. Environment Variables
    • Perl-CGI scripts communicate with Apache through environment variables, which the server populates. These include:
      • REQUEST_METHOD (e.g., GET, POST)
      • QUERY_STRING (data passed via the URL)
      • CONTENT_TYPE and CONTENT_LENGTH (for POST requests)
      • HTTP_* (headers sent by the client, like HTTP_USER_AGENT or HTTP_ACCEPT).
    • The script accesses these variables using Perl’s %ENV hash.
  3. Standard Input and Output
    • Input: For POST requests, the web server passes the body of the request to the CGI script via standard input (STDIN). The script reads this to process user-submitted data.
    • Output: The script outputs its response via standard output (STDOUT), starting with HTTP headers (e.g., Content-Type: text/html) followed by the response body (e.g., HTML or JSON content).
  4. Dynamic Content Generation
    • Perl-CGI scripts use Perl’s text manipulation capabilities to generate dynamic HTML, query databases, handle form submissions, or process other business logic.
    • These scripts can integrate with other systems, libraries, or APIs to create robust web applications.
  5. Apache Configuration
    • Apache must be configured to recognize and execute Perl-CGI scripts. This typically involves enabling the mod_cgi module and specifying the location of executable CGI scripts:
      <Directory "/var/www/cgi-bin">
          Options +ExecCGI
          AddHandler cgi-script .cgi .pl
      </Directory>
                      
    • Apache forks a new process for each CGI request, invoking the Perl interpreter to execute the script.
  6. Advantages and Limitations
    • Advantages:
      • Simplicity: Perl-CGI scripts are straightforward to write and deploy for small-scale web applications.
      • Integration: Easily integrates with databases and other backend systems.
    • Limitations:
      • Scalability: Forking a new process for each request can be resource-intensive, especially under high traffic.
      • Performance: Not as fast as modern web application frameworks due to process overhead.
  7. Modern Usage
    • While Perl-CGI was widely used in early web development, modern Perl applications often use frameworks like Catalyst or Mojolicious for better performance and scalability. However, Perl-CGI remains useful for lightweight or legacy applications.

In summary, Perl-CGI serves as a bridge between the Apache web server and Perl scripts, enabling dynamic, server-side processing and content generation in response to client requests.


Early on in the development of the Web, developers saw the need to interact with servers from their browsers.
The forms interface appeared as a Mosaic extension to HTML in 1993, and was formally added to the HTML Plus specification (the original working name for HTML 2.0) sometime in early 1994.
The forms interface was designed specifically to take advantage of features in the URL specification and the HTTP/1.0 specification.
These specifications each allow methods of getting data back from the client to the server. CGI (Common Gateway Interface) is the standardized interface for passing data between the Web server and a program running on the same host. It was originally designed to allow a program to process the data submitted by HTML forms.
In time, it became obvious to many of us that the CGI specification could be used for other things besides forms.
In fact, the form elements themselves could be integrated into a variety of applications like:
  1. Guestbooks
  2. Discussion groups
  3. Chat applications
  4. Online maps

SEMrush Software