Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How can I loop through a foreach(keys) on a form using CGI.pm
use strict; use warnings; use CGI my $form = new CGI; foreach(keys %$form) { print "$_ = $form->param($_}\n"; }
and
use strict; use warnings; use CGI my $form = new CGI; foreach(keys %{$form->param}) { print "$_ = $form->param($_}\n"; }
both seem to fail. Any ideas

Replies are listed 'Best First'.
Re: CGI Forms
by ikegami (Patriarch) on Jun 09, 2005 at 18:33 UTC

    Here's Ovid's solution without the single-value assumption:

    use strict; use warnings; use CGI; my $form = CGI->new; # Loop over each parameter. foreach my $param_name ($form->param) { # Loop over each value of the parameter. foreach my $value (@{$form->param($param_name)}) { print "$param_name = $value\n"; } }

    I recommend the above, but since you were trying to use a hash-like syntax, I'll show you how that can be done:

    use strict; use warnings; use CGI; my $cgi = CGI->new; # Get the parameters as a hash my $form = $cgi->Vars(); # Loop over each parameter. foreach my $param_name (keys %$form) { # Loop over each value of the parameter. foreach my $value (@{$form->{$param_name}}) { print "$param_name = $value\n"; } }
Re: CGI Forms
by Ovid (Cardinal) on Jun 09, 2005 at 18:32 UTC
    use strict; use warnings; use CGI; my $form = CGI->new; foreach my $param ($form->param) { my $value = $form->param($param); # assumes a single value print "$param = $value\n"; }

    You may want to take a gander at my CGI Course.

    Cheers,
    Ovid

    New address of my CGI Course.

Re: CGI Forms
by tlm (Prior) on Jun 09, 2005 at 18:35 UTC

    Maybe what you are looking for is this:

    use strict; use CGI; my $q = CGI->new(); for my $p ( $q->param ) { my @vals = $q->param( $p ); print "$p: @vals<br>\n"; }
    See the docs for more details on the param method.

    the lowliest monk

      Hey,

      Doesn't the '$q->param' method retrieve a list of parameters passed through each key?

      From the CGI doc:

      If the script was invoked with a parameter list (e.g. "name1=value1&name2=value2&name3=value3"), the param() method will return the parameter names as a list. If the script was invoked as an <ISINDEX> script and contains a string without ampersands (e.g. "value1+value2+value3") , there will be a single parameter named "keywords" containing the "+"-delimited keywords

      Is it me or am I reading the question wrong about him wanting each key value?

      perleager

        param is a weird function. Loosely speaking, with no args it returns the names of the params, but with a param name as argument it returns the value(s) of the so-named param.

        the lowliest monk

Re: CGI Forms
by waswas-fng (Curate) on Jun 09, 2005 at 19:05 UTC
    If all you are trying to do is list all of the params, you could also just use
    print $cgi->Dump();
    From the CGI docs: The Dump() method produces a string consisting of all the query's name/value pairs formatted nicely as a nested list. This is useful for debugging purposes:


    -Waswas
Re: CGI Forms
by perleager (Pilgrim) on Jun 09, 2005 at 18:31 UTC
    Hey,

    Try this:
    my $form = new CGI; @keywords = $form->keywords foreach $keyword (@keywords) { print "$keyword - $form->param($keyword)\n"; }
    perleager