Sie sind auf Seite 1von 2

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.
$tut = new TutorialConfig;

(You don't need to use parentheses here, unless your object's new method takes any extra parameters. But if you feel more comfortable writing $tut = new TutorialConfig();, it'll work just as well.) When you run this code, you'll see:
TutorialConfig is successfully loaded! We just created the variable ... and now it's a TutorialConfig object!

Now that we have a class and we can create objects with it, let's make our class do something!

Das könnte Ihnen auch gefallen