instantiating the CGI module repeatedly or
Rarely, if ever, do you wish to instantiate a CGI object more than once. If you are using a POST request, the first instantiation of the CGI object reads the data from STDIN (unless you specify an alternate source of the data). Thus, CGI objects instantiated at a later date cannot access the data from STDIN.

One simple way to create a hash is to do the following (untested):

use CGI; my $query = CGI->new; my @names = $query->param; my %hash = map { $_, $query->param( $_ ) } @names;
In newer versions of CGI.pm, there is a direct method of creating a hash of CGI name/value pairs, but I can't recall it offhand. Also, note that the above method only works with single values for each param. If you have multiple values, you could try the following (also untested):
my %hash = map { my @vals = $query->param( $_ ); $_, \@vals } @names;
Each hash value should then be a reference to an array of the values associated with each name.

Cheers,
Ovid

Update: Three things:

  1. chipmunk has pointed out some problems with my untested code and improved upon it.
  2. I would not use %hash = $query->Vars; because of the null byte. That's bad juju that can get you in serious security trouble if you screw up!.
  3. chromatic made the point that he wasn't sure if CGI.pm caches the results of POST requests. Because of some problems that I have had with this issue before, I erroneously assumed that it didn't. I ran a test and instantiated two CGI objects and used Data::Dumper to output them to a browser. Apparently (as of CGI.pm version 2.74), CGI.pm does cache the output and it's available on subsequent requests. Of course, that really doesn't justify the overhead of instantiating multiple CGI objects.
If you'd like to check the results, create a form with textfields, checkboxes, and whatever other widgets you would like. Use the POST method and point it at the following hack:
#!c:/perl/bin/perl.exe -wT use strict; use Data::Dumper; use CGI; my $query = CGI->new; my @names = $query->param; my %hash = map { $_, $query->param( $_ ) } @names; # Clearly broken my %hash2 = map { $_, [$query->param( $_ )] } @names; # works great my %chipmunk = $query->Vars; # Lincoln should have known better! my $q2 = CGI->new; print $query->header, $query->start_html, $query->pre( $query->h1('First Query'), Dumper( $query )), $query->pre( $query->h1('Second Query'), Dumper( $q2 )), $query->pre( $query->h1('Hash'), Dumper( \%hash )), $query->pre( $query->h1('Hash2'), Dumper( \%hash2 )), $query->pre( $query->h1('Chipmunk'), Dumper( \%chipmunk )), $query->end_html;

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to (Ovid) Re: Passing Params by Ovid
in thread Passing Params by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.