in reply to Printing variables from CGI form (was: I know this is very easy)

Use CGI.pm!!
use strict; use CGI; my $CGI = new CGI; print $CGI->header, $CGI->start_html, $CGI->pre; foreach my $name ($CGI->param) { print "$name = ", $CGI->param($name), "\n"; }

UPDATE: (an explanation)

When a form is submitted to a Perl script that uses CGI.pm, the form variables are magically available via the param() method.

The param() method works two ways: when called with no args, it returns a list of the available form variables. When called with a single argument(the name of the form variable you want the value of) it returns the value of that variable.

The big gotcha is dealing with multiple-valued variables, then param() will return a list of the values. Change the line inside the for loop to this:

my $value = join(",", $CGI->param($name)) || ''; print "$name = $value\n";
if you want to handle multiple-valued variables. Otherwise the script will only print the last assigned value. Jeff

R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
L-L--L-L--L-L--L-L--L-L--L-L--L-L--

Replies are listed 'Best First'.
Re: (jeffa) Re: I know this is very easy
by Trimbach (Curate) on Jun 02, 2001 at 04:34 UTC
    ...and for Old and New alike, the functional interface to CGI.pm is much cleaner and easier to understand. The following is functionally (:-D) identical to jeffa's code:
    use strict; use CGI qw(:standard); print header, start_html, pre; foreach my $name (param) { print "$name = ", param($name), "\n"; }
    Less typing means a few more months before Carpal Tunnel sets in.

    Gary Blackburn
    Trained Killer

Re: (jeffa) Re: I know this is very easy
by mpolo (Chaplain) on Jun 02, 2001 at 10:59 UTC
    If you don't mind "polluting" your name space, CGI.pm can also magically create a hash of your parameters with the Vars method.
    my %params=$cgi->Vars;