Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^5: Building data structures from CGI params

by Anonymous Monk
on Nov 22, 2010 at 02:31 UTC ( [id://872864]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Building data structures from CGI params
in thread Building data structures from CGI params

Well, that doesn't exactly match your syntaxt, but I found
it on 3rd try, PHP::HTTPBuildQuery

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.

See instead the CGI and URI modules which handles that sort of thing.

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 world

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

Ok, here is a start (with help from Corion)
#!/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' ] } } };

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://872864]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-03-19 10:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found