CGI Forms   «Prev  Next»

Perl Data Encoding - Exercise

Form-data encoding

You have seen this decoding code already in the previous few lessons.
As your exercise for this lesson, describe the lines where the query string is decoded in this subroutine:

sub getquery{
my $method = $ENV{'REQUEST_METHOD'};
my $query_string, $pair;
my %query_hash;

$query_string = $ENV{'QUERY_STRING'} if $method eq 'GET';
$query_string = <STDIN> if $method eq 'POST';
return undef unless $query_string;

foreach $pair (split(/&/, $query_string)) {
 $pair =~ s/\+/ /g;
 $pair =~ s/%([\da-f]{2})/pack('c',hex($1))/ieg;
 ($_qsname, $_qsvalue) = split(/=/, $pair);
 $query_hash{$_qsname} = $_qsvalue;
}
return %query_hash;
}

Enter your answer into the text area right below and click the Submit button when you are done.