SelfTaught
SelfTaught

Reputation: 499

Perl - How can I turn a hash into a query string?

How can I turn a hash into a query string? I would like to find a solution that doesn't involve using the CGI module. Here's an example of what I'm wanting to do but don't know what the best and most elegant approach would be, since I'm relatively new to Perl.

Hash:

my $data = {
         'key1' => 'value1',
         'key2' => 'value2',
         'key3' => 'value3',
         'key4' => 'value4'
};

To desired query string:

key1=value1&key2=value2&key3=value3&key4=value4

Upvotes: 3

Views: 1497

Answers (1)

ikegami
ikegami

Reputation: 385764

use URI qw( );
my $url = URI->new('', 'http');
$url->query_form(%$data);
my $query = $url->query;

If you're actually trying to build a POST request, check out HTTP::Request::Common's POST.

Upvotes: 8

Related Questions