Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Perl CGl Programming: No experience required.
(Publisher: Sybex, Inc.)
Author(s): Erik Strom
ISBN: 0782121578
Publication Date: 11/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Sorting Associative Arrays

The sort function, like many other Perl constructs, can be anywhere from amazingly simple to stultifyingly complicated.

We’ll start with amazingly simple and introduce two more Perl tools in the process:

  The foreach statement, which sets up a loop to extract members one at a time from a Perl list.
  The keys function, which puts the keys of an associative array into a normal array.

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 it’s 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.


Figure 3.6:  The variables are now sorted alphabetically.

Understanding MIME Types

Now that you have successfully written some environment variables to a Web page—and sorted them—you are ready to tackle MIME types, which tell the Web server what kinds of data it’s 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 client’s 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 Navigator’s 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, it’s usually best to stick to the standard. The list in HTTP_ACCEPT is a good place to start (see Table 3.2).

Table 3.2: MIME Content Types Recognized by HTTP

Type Subtype Description

Application Octet-stream Usually denotes MIME-encoded binary data such as a program file
Audio Basic A sound file
Image Gif, jpeg A GIF- or JPEG-formatted graphical image
Message External-body, partial, rfc-822 A message encapsulated in one of these three formats
Multipart Alternative, digest, mixed, parallel A message containing more than one part with more than one type
Text Plain, html Plain text, such as a text file or HTML document
Video Mpeg A video file

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. Let’s go over them.

Application

With application’s 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 doesn’t know what the file is, it will give it this type.

MIME-Encoded Files

At one time or another, you probably have received a badly formatted e-mail message that ended with hundreds of lines of what looked like gibberish. What you later found out was that someone had tried to send you a message with a program or picture file attached to it.

The gibberish you saw was MIME-encoded, or base-64, data. MIME encoding is a method of translating binary files into plain text, which then can be sent out as a mail message. The presumption is that some kind of de-encoding software is available at the recipient’s end, but if that fails or isn’t present, the gibberish appears.

Technically, what happens is this: The ASCII character set, in which most English text can be represented, contains 128 characters, all of which will fit in a 7-bit binary number. Program and other non-text files contain characters, bytes, that can range from values of 0 all the way to 255, the upper half of which uses a full 8 bits. Many older mail systems couldn’t accept characters larger than 7 bits, so an encoding scheme had to be devised to represent these 8-bit binary data in 7.

MIME encoding works by taking every four bytes and twiddling the bits so they fit into six 7-bit bytes. This is why attached files come out longer than the originals.

The bytes presumably are reverse-twiddled at the receiving end, with the result being what was sent out to begin with.


Previous Table of Contents Next


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.