On 5/9/02 1:15 PM, Batchelor, Scott <[EMAIL PROTECTED]> wrote:

> $value =~ s/%([\dA-Fa-f]{2})/pack("C", hex($1))/eg;
> 
> this is saying substitute any alpha, non-alpha characters with the
> hexadecimal string of $1.
> 
> I am not sure what the "%" sign does or the "eg"
> 
> Again, thanks in advance.
> 
> Scott 

Ooh, you're working with a script that parses URL-encoded strings, eh? Use
CGI.pm. It may not be faster, but it's easier. :)

That said:

% = literal '%'

( = start grouping in $1

[\dA-Fa-f] = character class consisting of any number from 0 to 9 (\d), and
any character of either case from a to f (A-Fa-f).

{2} = two of the previous character class.

) = stop grouping into $1

Now, for the replacement:

pack("C", hex($1)) = hex() Interprets $1 (which is holding two 0-9, a-f, or
A-F characters) as a hexadecimal value, and returns its base-10 value. I.e.,
hex('10') returns 16, and hex('FF') returns 255. pack() converts the
returned decimal value of hex() into a character of the corresponding ASCII
value. For instance, if hex() returns "100" (converted from the hexadecimal
value of 64), this results in "d" (the ASCII value of "d" is "100").

Now for the options at the end:

e = Evaluate the replacement string. This means, instead of taking
everything in the replacement string literally, evaluate it as a perl
expression and perform a replacement with the RESULT of that expression.
Very handy for things like this.

g = Global replacement. Replace everything that matches the pattern in
$value, not just the first one.

The net effect is that a URL-encoded string like "%61%20%62%20%63" is
decoded to "a b c".

Might I suggest buying the Camel Book? ("Programming Perl", by Larry Wall,
Tom Christiansen, and Jon Orwant, published by O'Reilly). It has a nice long
chapter on regular expressions which helps out tremendously with things like
this.

Hope I didn't butcher any of those descriptions and they were of some use.
Sorry if I went over anything you already knew :)
-- 
Michael


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to