Perl Tutorial

Fundamentals

Input and Output

Control Flow

Arrays and Lists

Hash

Scalars

Strings

Object Oriented Programming in Perl

Subroutines

Regular Expressions

File Handling

Context Sensitivity

CGI Programming

Misc

CGI Programming in Perl

CGI (Common Gateway Interface) is a standard for web servers to interface with executable programs installed on a server that generate web pages dynamically. Perl has historically been a popular language for CGI programming, especially in the early days of the web.

This tutorial will provide an overview of CGI programming using Perl. By the end, you should be able to create a basic web form that interacts with a Perl script on a server.

1. Setting Up:

Ensure that your server has Perl installed. Also, ensure that the server is configured to recognize and run Perl scripts as CGI programs. This usually means placing them in a directory called cgi-bin or similar.

2. First CGI Program:

Here's a simple Perl script that outputs an HTML page:

#!/usr/bin/perl

print "Content-type: text/html\n\n";
print "<html><head><title>Test CGI</title></head>";
print "<body>";
print "<h1>Hello, World!</h1>";
print "</body></html>";
  • Make sure the script has execute permissions.
  • Upload this script to the cgi-bin directory on your server.

When you access this script via a browser, it should display a "Hello, World!" webpage.

3. Processing Web Forms:

Suppose you have a form on your website:

<form action="/cgi-bin/form_processing.pl" method="post">
    Name: <input type="text" name="username">
    <input type="submit" value="Submit">
</form>

Here's a Perl CGI script (form_processing.pl) to process this form:

#!/usr/bin/perl

use CGI;

my $query = new CGI;
my $name = $query->param('username');

print "Content-type: text/html\n\n";
print "<html><head><title>Form Processing</title></head>";
print "<body>";
print "<h1>Hello, $name!</h1>";
print "</body></html>";

When the form is submitted, the script should greet the user by the name they entered.

4. Avoiding Common Mistakes:

  • Set permissions: Ensure that your Perl script has execute permissions.

  • Shebang Line: Make sure the first line of your script points to the correct location of Perl.

  • Print Content-Type: Always print the Content-type header as the first thing in your script. It tells the browser what sort of data to expect.

5. Security Considerations:

CGI scripts can be vulnerable to various attacks:

  • Input validation: Always validate and sanitize user input. Never trust data coming from web forms.

  • Taint mode: Run your Perl scripts in taint mode (perl -T) to ensure no unsanitized data is used in unsafe operations.

  • Error reporting: Be careful about revealing too much information in error messages. It might be exploited by attackers.

6. Further Reading:

While CGI in Perl can serve web requests, it's considered a bit old-fashioned now. Modern Perl web applications are more likely to use frameworks like Dancer, Mojolicious, or Catalyst. These provide routing, templating, and other utilities that make building web applications easier and more secure.

Summary:

CGI programming in Perl provides a way to create dynamic web pages and applications. It was more prevalent in the early days of the web, but the principles remain relevant even as newer frameworks and technologies emerge. Always be cautious about security, validate inputs, and consider modern frameworks for more extensive applications.

  1. Creating CGI scripts with Perl:

    • Description: Write a simple CGI script in Perl for web interaction.
    • Code:
      #!/usr/bin/perl
      use strict;
      use warnings;
      use CGI;
      
      my $cgi = CGI->new;
      print $cgi->header, $cgi->start_html('Hello CGI'), $cgi->h1('Hello CGI'), $cgi->end_html;
      
  2. Processing form data in Perl CGI:

    • Description: Receive and process form data in a Perl CGI script.
    • Code:
      my $name = $cgi->param('name');
      print $cgi->p("Hello, $name!");
      
  3. Perl CGI environment variables:

    • Description: Access CGI environment variables in Perl.
    • Code:
      my $remote_address = $cgi->remote_addr();
      print $cgi->p("Your IP address is: $remote_address");
      
  4. CGI input validation and handling in Perl:

    • Description: Validate and handle input from CGI parameters.
    • Code:
      my $input_value = $cgi->param('input_field');
      if ($input_value =~ /^[A-Za-z]+$/) {
          print $cgi->p("Valid input: $input_value");
      } else {
          print $cgi->p("Invalid input. Please enter letters only.");
      }
      
  5. Perl CGI output and content generation:

    • Description: Generate dynamic content in Perl CGI.
    • Code:
      print $cgi->p("Current time: " . localtime);