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:
- chipmunk has pointed out some problems with my untested code and improved upon it.
- 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!.
- 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. | [reply] [d/l] [select] |
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". :/
| [reply] [d/l] [select] |
Try passing $query to your subroutine. | [reply] |
Would passing $query to the subroutine have the fastest
execution of the above methods?
| [reply] |
From where I sit, yes. The overhead of creating a new CGI object is not trivial. It has to read the query string or STDIN (if it can, I don't remember offhand if it caches it) to get parameters, setting things up to be retrieved at will.
Creating a hash means allocating memory and hashing keys you may or may not use.
Passing in $query means dereferencing the object to call a method on it.
Depending on how many parameters you have and how many things you need to call and if you can get by with sticking things in a hash just once and you don't want sticky fields behavior that CGI.pm does so nicely, I'd give the edge to passing in $query. Sometimes I pass a hash.
This is unlikely to be the largest bottleneck in your program, however.
| [reply] |
I would tend to think that passing $query (which is a reference to an existing CGI query object) is much faster than creating a hash or another new CGI query object.
| [reply] |