![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
Speaking in HTMLThe Web server expects everything sent to it to be in one of several formats, all of them conforming to the MIME standard, which we will discuss at the end of this skill. You want your Hello World program to write to a Web page, so the first thing to do is to tell the server that it will be expected to draw one.
Major reconstruction is required for your first hello.pl to work, so lets just start a whole new program file. Youre no longer printing to the screen, but to the Web server, so the print statements need to produce valid HTML code. Crank up your text editing software and type this code. It will be dissected a little later, after youve run it: #!c:/perl/bin/perl.exe # hellowww.pl # A Perl program that draws a Web page. print "Content-type: text/html", "\n\n"; # MIME header. print "<HTML>", "\n"; print "<HEAD>"; print "<TITLE>Perl meets the World Wide Web</TITLE>", "\n"; print "</HEAD>", "\n"; print "<BODY>", "\"; print "<H1>Hello, World!</H1>", "\n"; print "</BODY>", "\n"; print "</HTML>", "\n"; # End hellowww.pl Save the file as hellowww.pl in the directory in which your Web server expects to see CGI programs. Remember, the correct directory will be listed in one of the .conf files on UNIX or in the Registry on Windows NT, probably Inet/Pub/scripts. If youre using the Sambar server and you let it install itself, it will be sambar/cgi-bin.
Running Your New Perl ProgramYour hellowww.pl program should now be installed on your Web site as a CGI program. You will run it as you would any other Web doo-dad, by bringing up a browser and connecting to the site. In this case you will go directly to the Perl script by typing the URL for your site followed by the base name of the CGI script directory. In other words, if your Web site is at www.doo-dads.com and the CGI script directory is cgi-bin, the full URL would be www.doo-dads.com/ cgi-bin/hellowww.pl.
Enter the URL in your Web browser. Figure 2.6 shows the result under Netscape 3.01.
Congratulations! Youve just written and run your first CGI program. hellowww.pl ExplainedSeveral additions to the original hello.pl script and some judicious system configuration enabled you to paint a Web page from Perl. Briefly, you made these changes:
The MIME HeaderAs you have learned, when going through CGI, a Perl program is actually addressing the Web server directly. But you cant just ship data to the server and expect it to know what to do. In our hellowww.pl example, the server knew what to do because of the first executable line in the program: print "Content-type: text/html", "\n\n"; # MIME header. Note the comment at the end of the line: # MIME header. It sums it all up. This is the MIME header, the information the server needs. With it, the server can proceed with the rest of the data your program sends to it. The rest of the data may be utterly bollixed up, in which case the server will complain loudly (if you have a sound card) and visibly. However, you have at least established the preliminariesthe content of the data is text and the type of the text is HTML. The Web server now expects that it will receive properly formatted HTML code and that it will send the code to the browser that has connected with it. The browser will dutifully process the HTML commands and draw the results on-screen. You know enough about Perl by now to know what \n is, but why are there two of them in this line?
This is another way of communicating with the server about your intentions. In the case of a simple HTML document, the MIME header can be communicated in just one line. There are cases, however, where that isnt enough. Two line-enders tell the server that the header, or information, portion of the communication is complete and that anything following should be treated as data.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |