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

I'm trying to pass an array, then fetch it using $cgi->param(). I can pass text and other scalars, but not arrays. Can someone please tell me what I'm doing wrong here, and how to fix it?
#!/usr/local/bin/perl -w use strict ; use English ; use Socket ; use CGI qw/:all/ ; my($rec) ; my(@Record) ; my($cgi) ; my(%Hash) ; my($String) ; my($item) ; $cgi = new CGI ; # # See if there were passed values from the input form # if ( $cgi->param('string') ) {$String = $cgi->param('string') ;}; if ( $cgi->param('array') ) {@Record = $cgi->param('array') ;}; # If string has a value, then do the following. If not, # present the input form. if ( $String ) { Header() ; print "The String is: $String<br>\n" ; print "The Array has in it:<br>\n" ; foreach ( @Record ) { print "File: $_->{file}\n" ; print "Item: $_->{item}\n" ; } ; print "We're done.....\n" ; closeHTML() ; # This is the input form....... } else { # Load up the array with some stuff.... $rec->{'file'} = "/usr/local/bin/myfile" ; $rec->{'item'} = "type: file" ; push ( @Record, $rec ) ; $rec->{'file'} = "/usr/local/bin/ourfile" ; $rec->{'item'} = "type: file" ; push ( @Record, $rec ) ; Header(); print "<FORM ACTION=\"/cgi-bin/test_ray.cgi\" METHOD=\"POST\" " ; print " enctype=\"multipart/form-data\" " ; print "<P><B>Enter a string in this box:</b>" ; print "<BR><INPUT TYPE=TEXT NAME=\"string\" "; print "VALUE=\"$String\" SIZE=15>\n" ; print hidden('array', @Record) ; print "<INPUT TYPE=SUBMIT VALUE=\"Submit Request\"> " ; print "<INPUT TYPE=RESET VALUE=\"Reset Form\"></form>\n" ; closeHTML(); } ; ############### HTML Header ################### sub Header { print "content-type: text/html\n\n" ; print "<html>\n" ; print "<head>\n" ; print "<title>Pass the array</title>\n" ; print "</head>\n" ; print "<h1>\n" ; print "Array:\n" ; print "</h1>\n" ; print "<body bgcolor=white>\n" ; print "<font color=black>\n" ; } ; ################# close HTML ################## sub closeHTML { print "</font>\n" ; print "</body>\n" ; print "</html>"; } ;
thanks, andy t.

Replies are listed 'Best First'.
Re: passing/retrieving arrays
by George_Sherston (Vicar) on Nov 03, 2001 at 05:24 UTC
    You seem to be not just passing an array, but passing an array of hashes. There may be a way that the wonderful wonderful CGI.pm handles this data structure, but if there is I don't know it. I would have thought it would be easier to break your data up into several name/value pairs, since that's what it is anyway, and save each pair in its own hidden variable.

    I know it's not what you asked about, but could I offer some observations on your use of CGI.pm? I've only recently started using it, so I'm not a huge expert, but I'm enthusiastic to share what works for me.

    I notice you import :all the CGI functions, but you then go ahead and create a CGI object as well - that, I think, is redundant. For what you are doing I think it wd be simpler to stick with the function-oriented interface. Also, you don't need :all the functions: use CGI qw/:standard/; should suffice.

    Then, having imported these functions (either explicitly, or implicitly by creating the CGI object) you might as well get your money's worth, and use them to create your html. To show how you could do that I've re-written Header:
    print header, start_html(-title => 'Pass the array'), h1('Array:'),
    (left off the last two lines since most times you'll get black text on white background by default). I think that's a lot easier to write and read, and it's also harder to get typos that can't be found but make a mess.

    That probably makes it short enough that you might not put it in a subroutine. But if you do, it strikes me that it's asking for trouble to give that subroutine (almost) the same name as a function that you just imported from CGI.pm.

    Sorry to go on about something you didn't ask about - hope some of it's useful.

    § George Sherston
Re: passing/retrieving arrays
by thunders (Priest) on Nov 03, 2001 at 02:22 UTC
    I think this line is your problem:
    print hidden('array', @Record) ;
    you are passing an array via the hidden method when, according to the docs in CGI.pm, you should be passing a reference to an array. The hidden() method creates a hidden field that can only hold a scalar value.
      Nope.
      CREATING A HIDDEN FIELD print $query->hidden(-name=>'hidden_name', -default=>['value1','value2'...]); -or- print $query->hidden('hidden_name','value1','value2'... +);
      But 'value1', etc. should be scalars not hashrefs.