JMa JMa - 1 year ago 117
Perl Question

Nested hash parameters in URL - CGI

How shall I pass nested hash params into URL for CGI script?

my $params = {
pwd => 'abc',
usr => 'abb',
data => { xyz => 'xxx' },

I tried:


But this results into:

$VAR1 = {
'data[yxz]' => 'xxx',
'pwd' => 'abc',
'usr' => 'abb'

Thanks for any hints.

Note: OK, I can parse string 'data[yxz]' and transform whole pair into hashref, but I am wondering if there is any 'native' function for this.

Answer Source


There is no native way to do that. If you are using, then you can pass multiple parameters with the same name and fetch them all into an array.

GET /?foo=1&foo=2&foo=3
use strict;
use warnings;
use CGI;
use Data::Dumper;

my $q = CGI->new;
my @foo = $q->multi_param('foo');

print Dumper \@foo;

You will achieve this kind of behavior from an HTML form by having multiple input fields with the same name attribute. But that does not give you a hash.

To pass along an actual data structure, that is not enough though. You could encode your multi-dimensional parameters as JSON.


Correctly URL-encoded that would be the following.


In your application, you can just convert it from JSON to a Perl data structure using the JSON module.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download