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

So I'm trying to pass an array to CGI from Jquery, I haven't had a problem with this in the past, but in this instance I'm trying to do key/value pairs. My http request headers look like this:
id:id1 services[0][name]:test services[0][type]:http services[0][port]:80
In Perl - I'm using:
@array = param('services[]');
This isn't a problem if I use a normal array - any thoughts?

Replies are listed 'Best First'.
Re: CGI array with key value pairs?
by space_monk (Chaplain) on Apr 30, 2013 at 06:09 UTC

    As a general advice, you can always use Data::Dumper to see what data structures are really coming into your module.

    It may also be a good idea to look at the CGI::Vars method...

    Updated advice: It has been pointed out that CGI::Struct is the correct way to go instead of CGI::Vars, but you still have to go through CGI::Vars before passing the result to CGI::Struct

    If any of my proposed solutions have minor errors, it's because I don't waste my genius on trivial matters. :-P

      It may also be a good idea to look at the CGI::Vars method...

      No, it is never a good idea to look at the legacy CGI::Vars () function, it exists solely for antiquely antique backwards compatibility , and has way too many caveats ( Problem with CGI::Vars )

      CGI::Struct does the job the OP is looking for

        Thanks for the info - I've updated my answer,

        If any of my proposed solutions have minor errors, it's because I don't waste my genius on trivial matters. :-P
Re: CGI array with key value pairs?
by Anonymous Monk on Apr 30, 2013 at 07:34 UTC
      Thanks for the advice, CGI::Struct is a cool module but actually just helped me move in the right direction. If anyone is curious, I fixed this problem by modifying the server-side call to be in JSON format and decoding it on the backend.
      Old HTTP request:
      services[0][name]:test services[0][type]:http services[0][port]:80
      New HTTP request:
      services:[{"name":"xyz","type":"http","port":"80"},{"name":"123","type +":"http","port":"80"}]
      Thanks!!