A note regarding CGI::Vars(): I never use it. It was created to ease the transition from the Perl4 cgi-lib.pl and the Perl5 CGI.pm. As a result, it uses the NUL byte (ASCII zero) to delimit multiple values. Since the NUL byte hack is so dangerous, I prefer not not to deliberately introduce them.
One quick and easy way of getting all params is the following:
my %param =
map { $_ => [$cgi->param($_)] }
$cgi->param;
Some might prefer the following:
#!/usr/bin/perl -wT
use strict;
use CGI qw/:standard/;
my %params =
map { $_ => get_data( $_ ) }
param;
sub get_data
{
my $name = shift;
my @values = param( $name );
return @values > 1
? \@values
: $values[0];
}
With the last snippet, if you only have one param value, the hash value is a scalar value. If you have multiple param values, the hash value is a reference to an anonymous array.
I used to think the last method was better, but as tilly alluded to above, and as I have seen before, most code that uses the ref function has problems. Most of the time, such code can be rethought to more naturally model the problem without having to insert a lot of extra logic to see what type of reference you are getting at any given time. Of course, that comes with the caveat that the only hard and fast rule is that there are no hard and fast rules.
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats. |