in reply to (CGI::Vars) Re: Variable Variables
in thread Variable Variables

This is OK unless a parameter is specified more than once as in '?ar=3&ar=2' - in this case Vars loses a value. The following is what I use:
my %args = (); # get cgi parameters into a hash. preserve multi-valued # parms for my $p ( $cg->param() ) { my @tmp = $cg->param($p); if ( scalar @tmp == 1 ) { $args{$p} = $tmp[0] } else { $args{$p} = [ @tmp ] } }
Now %args has your params, w/ multi-valued ones as references to arrays. - Bob Niederman

Replies are listed 'Best First'.
(the magical NUL) Re (2): (CGI::Vars) Re: Variable Variables
by mwp (Hermit) on Dec 06, 2000 at 10:01 UTC
    That's a good point. (Neat routine, too.) But consider this:

    Before coming here, and before CGI.pm matured, I was a notorious advocate of cgi-lib.pl. As a matter of fact, part of the reason that the Vars method in CGI.pm was added was to facilitate the transition of cgi-lib.pl users over to CGI.pm. In cgi-lib.pl you would do something like this:

    require "cgi-lib.pl"; &ReadParse(*input); # store form values in the glob "input" print &PrintHeader; # print HTML header print "Name: ", $input{name}, "<br>\n";

    This was very nice syntax and worked well for me for many moons. =) When the form parser would encounter multiple values for a single field name, it would string them together with the NUL character (by default), so you could do this:

    print "First value: ", $input{valueList}, "<br>\n"; print "All values: ", join("<br>\n", split(/\0/, $input{valueList}));

    And it would do exactly what you'd expect it to do. Neat, huh? Well, conveniently enough, CGI.pm inherited this behaviour. But don't take my word for it:

    #!/usr/bin/perl -w # /~alakaboo/cgi-perl/multi.pl use strict; use CGI; my $q = new CGI; my %v = $q->Vars(); print $q->header('text/plain'); { local $" = "\n"; my @tmp = split /\0/, $v{ar}; print "@tmp"; }
    Voila.