in reply to Re^4: Building data structures from CGI params
in thread Building data structures from CGI params
http://php.net/manual/en/function.http-build-query.php
Ok, its the reverse of what you want, and the reverse of it seems to be http://www.php.net/manual/en/function.parse-str.php
http://search.cpan.org/dist/PHP-Strings/Strings.pm#parse_str
PHP::Strings::parse_str WILL NOT BE IMPLEMENTED.Ha ha, at least now you the real format this is supposed to take, and you write a module to bring this feature to the perl worldSee instead the CGI and URI modules which handles that sort of thing.
For a reference implementation see http://phpjs.org/functions/parse_str:484, inifintely more readable than the php c-source maze
The reference tests makes it seem doable mb_parse_str.phpt/ mb_parse_str02.phpt
#!/usr/bin/perl -- use strict; use warnings; use CGI; use Data::Dump::Streamer; Main(@ARGV); exit(0); BEGIN { my %queries = ( # PHP::HTTPBuildQuery # URL decoded: "foo[bar]=baz", "foo[quick][quack]=schmack" "foo%5Bbar%5D=baz&foo%5Bquick%5D%5Bquack%5D=schmack" => { foo => { bar => "baz", quick => { "quack" => "schmack" }, }, }, ); sub Main { for my $query ( keys %queries ) { my $q = CGI->new($query); print $query, "\n", Dump( $queries{$query}, parse_str($q) +), "\n"; } } ## end sub Main } ## end BEGIN sub parse_str { my ($q) = @_; my %params; for my $k ( $q->param ) { my @v = $q->param($k); my $level = \%params; #~ my @k = grep length, split /\[([^\[]+)\]/, $k; #~ use DDS; warn Dump( { " $k => ", [ $k =~ /([^\[\]]+)/g ] } ); my @items = $k =~ /([^\[\]]+)/g; my $key = pop @items; for (@items) { $level->{$_} ||= {}; $level = $level->{$_}; } $level->{$key} = \@v; } ## end for my $k ( $q->param ) return \%params; } ## end sub parse_str __END__ foo%5Bbar%5D=baz&foo%5Bquick%5D%5Bquack%5D=schmack $HASH1 = { foo => { bar => 'baz', quick => { quack => 'schmack' } } }; $HASH2 = { foo => { bar => [ 'baz' ], quick => { quack => [ 'schmack' ] } } };
|
|---|