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 (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.
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.
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>";
cgi-bin
directory on your server.When you access this script via a browser, it should display a "Hello, World!" webpage.
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.
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.
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.
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.
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.
Creating CGI scripts with Perl:
#!/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;
Processing form data in Perl CGI:
my $name = $cgi->param('name'); print $cgi->p("Hello, $name!");
Perl CGI environment variables:
my $remote_address = $cgi->remote_addr(); print $cgi->p("Your IP address is: $remote_address");
CGI input validation and handling in Perl:
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."); }
Perl CGI output and content generation:
print $cgi->p("Current time: " . localtime);