0% found this document useful (0 votes)
45 views

Beginners Intro To Perl - Part 4: It's CGI Time

This document provides an introduction to CGI (Common Gateway Interface) programming in Perl. It explains that CGI allows a web browser to send information to a server-side program and receive dynamic content in return. It then provides a simple example CGI program in Perl that will display any form data submitted to it. The document advises readers to test the example CGI script and provides tips for troubleshooting common issues like permission errors or the script source code being displayed instead of the output.

Uploaded by

pankajsharma2k3
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Beginners Intro To Perl - Part 4: It's CGI Time

This document provides an introduction to CGI (Common Gateway Interface) programming in Perl. It explains that CGI allows a web browser to send information to a server-side program and receive dynamic content in return. It then provides a simple example CGI program in Perl that will display any form data submitted to it. The document advises readers to test the example CGI script and provides tips for troubleshooting common issues like permission errors or the script source code being displayed instead of the output.

Uploaded by

pankajsharma2k3
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Beginners Intro to Perl - Part 4

It's CGI time

So far, we've talked about Perl as a language for mangling numbers, strings, and files - the original purpose of the language. Now it's time to talk about what Perl does on the Web. In this installment, we're going to talk about CGI programming.
What is CGI?

The Web is based on a client-server model: your browser (the client) making requests to a Web server. Most of these are simple requests for documents or images, which the server delivers to the browser for display. Of course, sometimes you want the server to do more than just dump the contents of a file. You'd like to do something with a server-side program - whether that "something" is using Web-based e-mail, looking up a phone number in a database or ordering a copy of Evil Geniuses in a Nutshell for your favorite techie. This means the browser must be able to send information (an e-mail address, a name to look up, shipping information for a book) to the server, and the server must be able to use that information and return the results to the user. The standard for communication between a user's Web browser and a server-side program running on the Web server is called CGI, or Common Gateway Interface. It is supported by all popular Web server software. To get the most out of this article, you will need to have a server that supports CGI. This may be a server running on your desktop machine or an account with your ISP (though probably not a free Web-page service). If you don't know whether you have CGI capabilities, ask your ISP or a local sysadmin how to set things up. Notice that I haven't described how CGI works; that's because you don't need to know. There's a standard Perl module called CGI.pm that will handle the CGI protocol for you. CGI.pm is part of the core Perl distribution, and any properly installed Perl should have it available. Telling your CGI program that you want to use the CGI module is as simple as this:
use CGI ':standard';

The use CGI ':standard'; statement tells Perl that you want to use the CGI.pm module in your program. This will load the module and make a set of CGI functions available for your code.
A Real CGI Program

Let's write our first real CGI program. Instead of doing something complex, we'll write something that will simply throw back whatever we throw at it. We'll call this script backatcha.cgi:
#!/usr/local/bin/perl

use CGI ':standard'; print header(); print start_html(); for $i (param()) { print "<b>", $i, "</b>: ", param($i), "<br>\n"; } print end_html();

If you've never used HTML, the pair of <b> and </b> tags mean "begin bold" and "end bold", respectively, and the <br> tag means "line break." (A good paper reference to HTML is O'Reilly's HTML & XHTML: The Definitive Guide, and online, I like the Web Design Group.) Install this program on your server and do a test run. (If you don't have a Web server of your own, we've put a copy online for you here.) Here's a short list of what you do to install a CGI program:
1. Make sure the program is placed where your Web server will recognize it as a CGI script. This may be a special cgi-bin directory or making sure the program's filename ends in .pl or .cgi. If you don't know where to place the program, your ISP or sysadmin should. 2. Make sure the program can be run by the server. If you are using a Unix system, you may have to give the Web-server user read and execute permission for the program. It's easiest to give these permissions to everybody by using chmod filename 755. 3. Make a note of the program's URL, which will probably be something like http://server name/cgi-bin/backatcha.cgi) and go to that URL in your browser. (Take a guess what you should do if you don't know what the URL of the program is. Hint: It involves the words "ask," "your" and "ISP.")

If this works, you will see in your browser ... a blank page! Don't worry, this is what is supposed to happen. The backatcha.cgi script throws back what you throw at it, and we haven't thrown anything at it yet. We'll give it something to show us in a moment. If it didn't work, you probably saw either an error message or the source code of the script. We'll try to diagnose these problems in the next section.
Uh-Oh!

If you saw an error message, your Web server had a problem running the CGI program. This may be a problem with the program or the file permissions. First, are you sure the program has the correct file permissions? Did you set the file permissions on your program to 755? If not, do it now. (Windows Web servers will have a different way of doing this.) Try it again; if you see a blank page now, you're good. Second, are you sure the program actually works? (Don't worry, it happens to the best of us.) Change the use CGI line in the program to read:
use CGI ':standard', '-debug';

Now run the program from the command line. You should see the following:
(offline mode: enter name=value pairs on standard input)

This message indicates that you're testing the script. You can now press Ctrl-D to tell the script to continue running without telling it any form items. If Perl reports any errors in the script, you can fix them now. (The -debug option is incredibly useful. Use it whenever you have problems with a CGI program, and ignore it at your peril.) The other common problem is that you're seeing the source code of your program, not the result of running your program. There are two simple problems that can cause this. First, are you sure you're going through your Web server? If you use your browser's "load local file" option (to look at something like /etc/httpd/cgi-bin/backatcha.cgi instead of something like http://localhost/cgi-bin/backatcha.cgi), you aren't even touching the Web server! Your browser is doing what you "wanted" to do: loading the contents of a local file and displaying them. Second, are you sure the Web server knows it's a CGI program? Most Web server software will have a special way of designating a file as a CGI program, whether it's a special cgi-bin directory, the .cgi or .pl extension on a file, or something else. Unless you live up to these expectations, the Web server will think the program is a text file, and serve up your program's source code in plain-text form. Ask your ISP for help. CGI programs are unruly beasts at the best of times; don't worry if it takes a bit of work to make them run properly.

You might also like