Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Caveats apply. This is not recursive and it almost certainly should be; it fails on deeper data structures. It only serializes. I am almost positive there is something in some corner of the CPAN that does this but I couldn't find it in a somewhat casual search. This is first draft quality code. This style of query string is popular with PHP and Ruby apps. I think this is how jQuery does it if you don't request "traditional" (meaning *standards compliant* serialization) so it might be good to just copy its code if there turns out to be nothing solid in Perl already.

use strictures; use Carp; use URI::Escape; my %hash = ( "name" => "test name", "file_ids" => [ 1, 2 ], "sub" => { "name" => "foo", "message" => "bar" } ); print serialize_web2_0_query_string(\%hash), $/; sub serialize_web2_0_query_string { no warnings "uninitialized"; my $datum = shift; my $strings = shift || []; for my $key ( keys %$datum ) { if ( ref $datum->{$key} eq "HASH" ) { while ( my ( $k, $v ) = each %{ $datum->{$key} } ) { push @$strings, sprintf "%s[%s]=%s", uri_escape($key), + uri_escape($k), uri_escape($v); } } elsif ( ref $datum->{$key} eq "ARRAY" ) { push @$strings, sprintf "%s[]=%s", uri_escape($key), uri_e +scape($_) for @{ $datum->{$key} }; } elsif ( not ref $datum->{$key} ) { push @$strings, join "=", map uri_escape($_), $key, $datum +->{$key}; } else { croak "Nope. Don't know what to do with ", $key, " => ", $ +datum->{$key}; } } return join "&", @$strings } __DATA__ sub[name]=foo&sub[message]=bar&file_ids[]=1&file_ids[]=2&name=test%20n +ame

In reply to Re: Building a URL Query String from a Data Structure by Your Mother
in thread Building a URL Query String from a Data Structure by dorko

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 about the Monastery: (6)
As of 2024-04-16 07:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found