jeffa has asked for the wisdom of the Perl Monks concerning the following question:
Several months ago i was playing with pulling form vars from
URI's. The problem is handling multiple keys such as
'foo=bar&foo=baz'. I decided to use URI
and was shocked to find that the following code:
Yielded:use URI; use Data::Dumper; my $uri = URI->new('http://localhost/foo.cgi?foo=bar&foo=baz'); my %form_var = $uri->query_form; print Dumper \%form_var;
$VAR1 = { 'foo' => 'baz' };So i hunted down query_form() (it is located in URI/_query.pm) and discovered this at the end of that subroutine:
Then i decided to recode that with something like this:map { s/\+/ /g; uri_unescape($_) } map { /=/ ? split(/=/, $_, 2) : ($_ => '')} split(/&/, $old);
Now when i run my test code, i get the output i expect:my %hash; for my $pair (split /&/, $old) { my ($k,$v) = map { s/\+/ /g; uri_unescape($_) } split /=/, $pair, 2; if (exists $hash{$k}) { if (ref $hash{$k}) { push @{$hash{$k}}, $v; } else { $hash{$k} = [$hash{$k},$v]; } } else { $hash{$k} = $v; } } return %hash;
$VAR1 = { 'foo' => [ 'bar', 'baz' ] };The reason i am bringing this up is because i submitted this as a patch to Gisle Aas and never heard back. The bug has been 'resolved', but my test case is contrary ...
Is this really a 'bug' in URI or am i missing something? I really feel that query_form() should handle multiple values with the same key, but giving the lack of response i got from Mr. Aas, i can't help but think i am missing something ... thanks :) (oh, and i am not ruling out the possibility that my message never reached Mr. Aas. - i just want to be sure before i bug him again.)
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
Back to
Seekers of Perl Wisdom