Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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' ] } } };

In reply to Re^5: Building data structures from CGI params by Anonymous Monk
in thread Building data structures from CGI params by fullermd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-03-28 21:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found