in reply to Please explain this (CGI) parsing routine
This is some decoding script for form input. You should not be doing this yourself. See Use CGI or die; and No excuses about not using CGI.pm
Anyway here is an explanation:
# iterate over @pairs array assigning each value to $pair in turn # this should start foreach my $pair (@pairs) # see Use strict warnings and diagnostics foreach $pair (@pairs){ # this splits each pair into $name and $value at the "=" # the "=" is thrown away ($name,$value) = split(/=/,$pair); # this tr(ansliterates) the "+" chars into spaces # in HTTP this is how spaces are encoded $value =~ tr/+/ /; # this s(ubstitutes) some special encoded characters into their ASCII values # In HTTP certain chars have special meaning to the protocol so need # to be encoded. These are encoded as their hex equivalent # The encoded form is %DD where the DD are two hex digits and # the leading % is a flag to let us know the next two chars are # hex digits encoding a ASCII literal. Now to the regex itself. # the first part captures the two hex digits after the % into $1 # the second part converts these to their ASCII equivalents # the /eg at the end means "e" evaluate the second part and # "g" do globally ie all ocurrences $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg; # this last line generates a associative array (a hash) called %VALUE # the key in this hash is $name and the value is $value $VALUE{$name} = $value;
I recommend you check out Ovid's Web Programming with Perl tutorial for more details. This code will choke on names like "Jack&Jill" or "John Smith" because the decoding is only performed on $value, not $name. These will remain encoded as "Jack%26Jill" and "John+Smith".
cheers
tachyon
|
|---|