![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
Sorting Associative Arrays The sort function, like many other Perl constructs, can be anywhere from amazingly simple to stultifyingly complicated. Well start with amazingly simple and introduce two more Perl tools in the process:
Change the while block in cgienv.pl to this: foreach $EnvironVar (sort (keys (%EnvVarList))) { print $EnvironVar, ": ", $ENV{$EnvironVar}, "<BR>", "\n": } The foreach statement, like while, sets up a loop. However, it is the preferred method for pulling values out of arrays because it explicitly ends the loop when it reaches the end of the array, rather than testing for emptiness, or NULL, as while does. A more straightforward illustration of foreach would be an example of a normal Perl list, such as the following: foreach $member (@list) { do something with $member; } In this example, foreach starts at the beginning of the list, loading $member with the value at index 0, after which the code block between the brackets does whatever its supposed to do with $member. Similarly, in our example $EnvironVar will be loaded with the value extracted between the parentheses each time through the loop. You start with a call to keys with %EnvVarList as its argument. This returns a normal array filled with the keys from the associative array. Given no other instructions, sort simply sorts the array alphabetically. Because you are stepping through the array with foreach, Perl knows to hand you the scalar array members one at a time, until the array is empty. You have left the $Desc part of the associative array out of the picture altogether in this example. It could be included, but that would pose a programming problem of fair-to-middling complexity and we want to keep things simple for now. If you run the rewritten cgienv.pl in your Web browser, you should see something similar to Figure 3.6.
Understanding MIME TypesNow that you have successfully written some environment variables to a Web pageand sorted themyou are ready to tackle MIME types, which tell the Web server what kinds of data its receiving. MIME types are the various file formats that have been standardized under the Multipurpose Internet Mail Extensions, as you learned in Skill 2. Notice that a list of descriptions was printed when cgienv.pl displayed the value in HTTP_ACCEPT. These nominally are the MIME types the clients browser will accept, but the list of types can be misleading. Internally, the browser may be able to handle many more types, as you saw in Skill 2 when you called up Netscape Navigators list of Helpers from the General Preferences in the Options menu (take another look at Figure 2.12). However, many of these types may be extensions to the MIME standard. From the standpoint of your server, its usually best to stick to the standard. The list in HTTP_ACCEPT is a good place to start (see Table 3.2).
The MIME types are used in the content-type: message that you send to the server as a header for an HTML document. You have used text/html as the description so far. However, any of the types in Table 3.2 could be used. Lets go over them. ApplicationWith applications most commonly used subtype, octet-stream, this tells the server that you are sending MIME-encoded binary data, usually a program file or some other data that are not textual. Many browsers use this type as their default; in other words, if the browser doesnt know what the file is, it will give it this type.
|
![]() |
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. |