![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
The Power of Regular ExpressionsChange the foreach loop in geturl.pl to read this way: # Split, decode each of the name-value pairs and print them on the page. foreach $NameValue (@NameValuePairs) { ($Name, $Value) = split (/=/, $NameValue); $Value =∼ tr/+/ /; $Value =∼ s/%([\dA-Fa-f][\dA-Fa-f])/ pack ("C", hex ($1))/eg; print "Name = $Name, value = $Value<BR>\n"; } If youve never worked with regular expressions, the two new lines in geturl.pl probably are among the weirdest things youve ever seen. However, install the new program, fill out form1.html in your Web browser, and submit it, using all the bizarre characters you want. Your result will be what you typed in, as illustrated in Figure 5.9.
Where did all the garbage go? If you had any doubts about the power of Perl, this little trick should dispel them. If you have been intimidated by the perplexing and strange conventions of regular expressions, you now should feel inspired to learn them. You have accomplished in two lines of code what probably would have taken an entire program to do in C or C++. In Perl, you just have to learn the lingo. Theyre easy to avoid, these Perl regular expressions. They generally look like something only Martians would understand. However, you now have seen what you can do with a simple, though quite turgid, regular expression. You simply have no choice but to learn more about them! Lets begin by examining the two new lines in geturl.pl. Translations, SubstitutionsThe first new line is fairly simple, though utterly meaningless to the untrained eye: $Value =∼ tr/+/ /; A couple of new Perl concepts surface in this line:
You will use the =∼ operator frequentlyalways, in fact, when you want to change characters in a string into other characters. The specification for tr is tr /SEARCH_LIST/REPLACE_LIST/ where SEARCH_LIST is the characters for which you want to search, and REPLACE_LIST is what their new values will be.
The line from geturl.pl that utilizes tr: $Value =∼ tr/+/ /; has the + character as its SEARCH_LIST and a space as its REPLACE_LIST. Therefore, it will go through the $Value string and replace every occurrence of the plus character (+) with a space. This is handy in URLs, where all spaces are designated by plus signs. The second new line in geturl.pl is trickier to understand. $Value =∼ s/%([\dA-Fa-f][\dA-Fa-f])/ pack ("C", hex ($1))/eg; Its a little easier to understand if it is explained in the sequence of events that it kicks off. First of all, what does this code do? This is the program line that turns URL-encoded characters back into printable characters. Remember the %nn convention, in which special characters are encoded with a percent sign followed by their hexadecimal ASCII values? This is where the encoded values revert to real characters. Lets step through the program line:
|
![]() |
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. |