Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

hash to query string using semicolon

by metaperl (Curate)
on Feb 16, 2009 at 22:00 UTC ( [id://744221]=perlquestion: print w/replies, xml ) Need Help??

metaperl has asked for the wisdom of the Perl Monks concerning the following question:

Both URI::QueryParam and Data::URIEncode use '&' as the separator for query parameters.

I would like to form a query string from a hash and have the query string use semicolon as separator.

I know the code is short, but I dont believe in writing what has/should have been written... plus what a shame that the first link for Perl in Google does not even escape its data.

Replies are listed 'Best First'.
Re: hash to query string using semicolon
by Corion (Patriarch) on Feb 16, 2009 at 22:05 UTC

    What's wrong with using just CGI?

    use strict; use CGI; my $c=CGI->new(); $c->param('foo'=>'bar'); $c->param('baz'=>'boo'); print $c->url(-query=>1,-relative=>1); __END__ ?foo=bar;baz=boo
Re: hash to query string using semicolon
by jasonk (Parson) on Feb 16, 2009 at 22:18 UTC

    URI::QueryParam doesn't use anything for query parameters, it just passes it's data on to URI, which lets you choose how they are delimited...

    use URI; my $u = URI->new( "http://www.example.com/" ); $u->query_form( { foo => 1, bar => 2 }, ';' ); print "$u\n";

    And if you just want the query string, you can get it from $u->query.


    www.jasonkohles.com
    We're not surrounded, we're in a target-rich environment!
Re: hash to query string using semicolon
by ikegami (Patriarch) on Feb 16, 2009 at 22:10 UTC
    use URI::Escape qw( uri_escape ); my $query = '?' . join ';', map { uri_escape($_->[0]) .'='. uri_escape($_->[1]) } @query_args;

    Update: Oops, Forgot there are keys and values rather than just values. Fixed.

Re: hash to query string using semicolon
by Your Mother (Archbishop) on Feb 17, 2009 at 00:20 UTC

    And you don't have to go all the way to QueryParam; depending on your needs. URI proper supports it now: $uri->query_form( \%hash, $delim ).

      I should have included that this can be set globally with $URI::DEFAULT_QUERY_FORM_DELIMITER = ";";.

      I know it's mildly controversial with the current RFCs but the & as standard was always a mistake since it effectively made real world separators the incredibly clunky &. As long as you support or know the receiving end supports the semicolon, I think it's the best practice. While I'm telling you kids to get off my lawn, the ?arg[0]=foo&arg[1]=bar crap we got from PHP and ruby webdevs being unable to parse naked lists in order is also a travesty.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-23 18:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found