Packaging information


The QUERY_STRING environment variable can only store one string without spaces, but ideally we would like it to store more information. For example, when searching a database across the Web we would like to pass information not only about what string should be searched for, but also what kind of fields should searched, whether the search should be case sensitive, and so on. Also, it would be nice to include spaces in search strings.

As it happens this is all possible by encoding spaces and values of different variables. Suppose we want to send the following information to our CGI script:

star = Eastwood
cert = 15
movie = Pale Rider

Clearly we need to package this information into a single string for the QUERY_STRING environment variable. The way it is done is to put an & between each variable setting and replace each space by a + sign. From the above we get

QUERY_STRING = "star=Eastwood&cert=15&movie=Pale+Rider"

In addition, certain characters are replaced by their value in the %xx hexadecimal form. This can all get rather tedious to decode, but there's a handy Perl subroutine in read-input.pl to ease the strain. The subroutine &read_input takes a query string like the above and returns it as an associative array. In the above example we would simply call the subroutine in our CGI script as follows:

%data = &read_input;
# Now we have
#    $data{'star'} eq "Eastwood"
#    $data{'cert'} eq "15"
#    $data{'movie'} eq "Pale Rider"


Exercise

This exercise is simply to incorporate the &read_input subroutine into your eternal document. Your query strings should look like page=3 or even page=3&from=2, and you should reference the page in an associative array.


Previous Start Next