PerlCGITutorial

Basics of Perl CGI Programming

=== Introduction ===

This tutorial is designed for someone with basic-moderate experience with perl. It will teach you the basics of using the CGI module to take parameters from a webpage, output content, output errors to the browser, and write secure files to the webserver.

=== Getting a Server ===

The first step in learning how to use Perl for CGI is to get a server that supports Perl CGI. If you have a server that supports CGI already, then you can skip this section. If you are just starting and not sure if you really want to use Perl for CGI, or are just playing around, I suggest you set up a test server on your own computer by getting a webserver package and the perl runtime enviorment appropriate for your system. If you prefer not to have to deal with this, or you are sure you wish to go ahead with Perl CGI, you can either buy hosting from a company, or use a free service. There are a number of free services out there that allow hosting of CGI script, my favorite is Tripod however as long as they support Perl CGI the service is unimportant.

=== Getting Server Preped ===

If the server doesn't have a designated CGI bin and allows you to have scripts executed anywhere on the server, then create a cgi-bin in your web folder on the server. Check to make sure the server has the CGI module, and if it doesn't, ask them to install it. Check to see if the server has the CGI::CARP module, CGI::CARP is not required, but it makes life much easier for CGI programmers and there have been many times I would've ripped my hair out without it. If the server does not have it, ask them if they would install it.

=== The Basics ===

Let's talk about the shebang line. It's required by most if not all webservers, it tells the webserver where to find the interpreter. Just put a # symbol and then a ! then the path to perl. #!/usr/bin/perl and #!/usr/local/bin/perl are the most common shebang lines, however, you should ask your server administrator for the path.

Now for the all important line about modules. Right after the shebang line we should import the modules we need, usually CGI and CGI::CARP, however, sometimes other modules such as DBI and Sendmail will be needed. Loading modules is out of the scope of this tutorial, if you require help on loading modules into your code, I suggest you check Perl.com for resources. For CGI programming it is highly recommended to use the CGI module, which you load by putting
use CGI;
in your program. CGI::Carp is useful for debugging; add this line to your program:
use CGI::Carp qw(fatalsToBrowser);
For security reasons you should NOT use CGI::Carp in programs that are reachable through the Internet.

Once we have the CGI module loaded, we must initialize an instance of it, by using the code
$query=new CGI;
This will make an object that will contain all of the parameters sent to the script, enviormental information, etc.

Now it's time to find out how to retrieve parameters sent to the script. Very simply say
$variable_name=$query->param('paramater_name_here');
You can use that for any parameter passed of any type from hiddden to text to radio, to passwords.

The last topic that you should know for basic CGI is how to create an HTML header. The simple statement
print "Content-type:text/html\n\n";
If you do not have this in your script, you will get an internal server error with the message "Incomplete headers." This is not guaranteed in all cases, however, so you should use
print $query->header()
instead.

=== Securing Files ===

The best way to secure a file is fairly simple, but will require the aid of a server administrator. Simply find the file you wish to secure, and chown it to the user nobody or whatever other user is used to execute cgi scripts from the web. Then chmod that file to 700. This will prevent you and everyone else on the server, with the exception of root, from reading and writting from this file. The only way to access it now is from cgi stored on the server. This is preferable over other methods of protection because it also protects it from other server users, not just web users.

=== Conclusion ===

This concludes this tutorial, I have covered everything that a person new to Perl CGI should know about cgi. I have left out some of the modules and options that you do not need to worry abuot when you first start Perl CGI, but should know when you start seriously writting CGI, I hope to cover these in another tutorial at another time. To conclude this tutorial, I wrote a small script that you can use as you like. It simply displays text that you enter into a text field. If you wish to try it out, save it as test.cgi and be sure to change the shebang line to reflect your server.
  1. !C:/Perl/bin/Perl
  2. above is path to perl
use CGI; use CGI::Carp qw(fatalsToBrowser);
  1. import the cgi module namespace
$query=new CGI;
  1. create a cgi var
$text=$query->param('text_to_post');
  1. get a parameter from the webpage...
unless ($text) { #if text isn't defined... $text="Default"; #set the variable to a default value } print $query->header(); print "<html>"; print "<head><title>Tutorial CGI Script</title></head>"; print "<body bgcolor=black text=white alink=white vlink=white link=white>"; print $text; print "</body></html>";




 

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.