in reply to Passing Params
instantiating the CGI module repeatedly orRarely, 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):
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):use CGI; my $query = CGI->new; my @names = $query->param; my %hash = map { $_, $query->param( $_ ) } @names;
Each hash value should then be a reference to an array of the values associated with each name.my %hash = map { my @vals = $query->param( $_ ); $_, \@vals } @names;
Cheers,
Ovid
Update: Three things:
#!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 |