This is stolen and modified from CGI.pm's ReadParse method: It'll work fine unless there are multiple query params with the same name.
sub ReadParse {
my $in = shift;
@in = split(/&/,$in);
foreach $i (0 .. $#in) {
# Convert plus's to spaces
$in[$i] =~ s/\+/ /g;
# Split into key and value.
($key, $val) = split(/=/,$in[$i],2); # splits on the first =.
# Convert %XX from hex numbers to alphanumeric
$key =~ s/%(..)/pack("c",hex($1))/ge;
$val =~ s/%(..)/pack("c",hex($1))/ge;
# Associate key and value. \0 is the multiple separator
$in{$key} = $val;
}
return \%in;
}
my $data = ReadParse('a=7&b=1&text=Some+text');
print $data->{text}, "\n";
# prints 'Some text'
PS: you want those brackets in your question to be curlies, not square brackets. Otherwise you'll get an array ref, not a hash ref.
PPS: You mentioned that CGI and Apache::Request modules both contained the functionality to do this. When that's the case, it's usually helpful (and a learning experience) to jump into the code for those modules and try to extract only the parts that you need. But then again, reinventing the wheel yourself is also a good learning experience too. ;) Take your pick!
blokhead |