![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
URL Encoding with GETThe QUERY_STRING environment variable is the method of storage for the information passed from an HTML form to a CGI program through GET. When you use the GET method to pass the information, it is just tacked on to the end of the URL: perl.bat?LastName=Jones&FirstName=John&Address=123+Any+Street The arguments to the CGI application are separated from the application name by a question mark (?) and the URL is built in name=value pairs, with each pair separated from the others by an ampersand (&). Notice, too, that all of the spaces in the URL have been replaced with plus signs (+). These three characters, plus the percent sign that will flag any encoded characters and the equal sign that separates the name=value pairs, are what youll need to deal with in your Perl-CGI script. To get a little practice with these concepts and to learn some new concepts in Perl, youll write a program to read the QUERY_STRING, decode it, and print all of its names and values in an HTML document. Heres the program: #!/perl/bin/perl # geturl.pl # # A little Perl script to read, decode and print the names # and values passed to it from an HTML form through CGI. # Get HTML header, ender, define the page title. require "/pub/scripts/perl-cgi/html.pl"; # Full path. $Title = "Get Information From A URL"; # Get the query string. $QueryString = $ENV{'QUERY_STRING'}; # Use split to make an array of name-value pairs broken at # the ampersand character. @NameValuePairs = split (/&/, $QueryString); # Put up an HTML header, page title and a rule. &HTML_Header ($Title); print "<BODY>\n"; print "<H1>$Title</H1>\n"; print "<HR>\n"; # Split each of the name-value pairs and print them on the page. foreach $NameValue (@NameValuePairs) { ($Name, $Value) = split (/=/, $NameValue); print "Name = $Name, value = $Value<BR>\n"; } # End the HTML document. &HTML_Ender; # End geturl.pl Store it as geturl.pl in a directory from which you can run Perl scripts over the Web server. Now, change the ACTION= string in form1.html to the correct path to geturl.pl. Also, move form1.html into a directory on your Web server. Youll be calling it up through your Web site this time, rather than as a simple file in the browser. Start your browser and connect with the Web site with the correct URL to form1.html. Fill in the form with information similar to what has been entered in Figure 5.7. But this time use some characters that will be encoded by the browser when theyre shipped through CGI.
When you click the Send Information button, the result should be similar to what is illustrated in Figure 5.8.
Decoding a Query StringYou no doubt noticed that the information displayed by geturl.pl is full of strange encodings and separators. Youll take care of those shortly. For now, lets examine how you broke a single URL into series of name and value pairs. The hero in this program is split, another workhorse Perl function that you will use again and again in your applications. split is specified in this way: split (/PATTERN/, STRING, LIMIT); where PATTERN is some delimiter or point of separation, STRING is the string to split and the optional LIMIT tells split to do no more than LIMIT separations. split returns an array of strings broken apart at the PATTERN, which is eliminated in the array. PATTERN is always put between the forward slash (/) characters, but it can be left out. This call to split @Array = split (//, $String); would fill @Array with the contents of $String broken out at any instance of white space, which is spaces, tabs, and line-enders. PATTERN also can be a regular expression, which well cover shortly. Other than split, there is very little in geturl.pl that you havent seen before. You used the QUERY_STRING environment variable to obtain the URL submitted by form1.html; you broke the query string into individual name=value pairs by using split to separate it on the ampersand character; and you split up the pairs into their component parts by specifying the equal sign as the PATTERN. However, the strings still have all of those URL-encoded characters in them, and it looks as if it will be a tedious job to take them out, doesnt it? Well, lets see.
|
![]() |
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. |