in reply to Parsing form post (content)

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

Replies are listed 'Best First'.
Re: Re: Parsing form post (content)
by Anonymous Monk on Aug 29, 2002 at 19:03 UTC
    That code dosen't handle the semicolon as a seperator, and it dosen't work for multiple values on the same key either (it says it seperates them with \0 but it really dosen't) You're just lucky merlyn isn't on at the moment...