in reply to Passing Params

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.

Replies are listed 'Best First'.
Re: (Ovid) Re: Passing Params
by chipmunk (Parson) on Jan 17, 2001 at 01:33 UTC
    The first code snippet will also fail if there is a parameter without a value, because param() returns an empty list in that case. The list will end up like ('param1' => 'value1', 'param2' => 'param3', 'value3'). That code really just won't work well, unfortunately.

    In the second snippet, it's simpler to use an anonymous array: my %hash = map {$_, [$query->param($_)]} @names; The new CGI method for getting a hash of the parameters is Vars(): my %hash = $query->Vars(); However, that uses the cgi-lib approach of joining multiple values with "\0". :/