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

I would like to get all the param from my HTML form.
Which function of CGI ':standard' should i use and how ??
In the form should i put post or get ??
thanks,

Replies are listed 'Best First'.
Re: Easy CGI Question
by Ovid (Cardinal) on Mar 30, 2004 at 00:39 UTC

    For the most part, either POST or GET is fine (just don't mix the two unless you know what you're doing).

    To get all of the params, you can do this:

    use CGI ':cgi-lib'; $params = Vars; $address = $params->{address}; @foo = split("\0",$params->{'foo'});

    Of course, that uses the old-style "split on a null byte" to fake a data structure for multiple param values. As long as each param has only one instance, though, you can ignore this.

    Another relatively simple method, if you don't mind all of your param values as array references:

    use CGI qw':standard'; my %params = map { $_ => [param($_)] } param;

    Cheers,
    Ovid

    New address of my CGI Course.

Re: Easy CGI Question
by stonecolddevin (Parson) on Mar 30, 2004 at 02:36 UTC
    I believe if you wanted to loop through them, you could simply do:
    foreach my $i ( param() ){ ... }
    All posts are spawns of an archaeic and instinctual remnant of pre-neanderthalian neural synapse that, while irrevocably human, remains an anomoly to the common laws of nature.
      This will work only for SINGLE-values
      foreach my $n ($query->param) { $param{$n} = $query->param($n); }
      try this too:
      my( %param )= @_;

      CSUhockey3
Re: Easy CGI Question
by jZed (Prior) on Mar 30, 2004 at 00:35 UTC
    See the Vars or param methods in the CGI.pm docs. Use either POST or GET depending on your needs though POST is more generally useful (allows larger amounts of input, etc.). There are plenty of examples in the docs.
Re: Easy CGI Question
by Trimbach (Curate) on Mar 30, 2004 at 04:31 UTC
    And for completeness sake you can always import all your vars into a separate namespace, automagically creating a set of variables on the way. From the CGI.pm docs:
    $query->import_names('R'); This creates a series of variables in the 'R' namespace. For example, $R::foo, @R:foo. For keyword lists, a variable @R::keywords will appear. If no namespace is given, this method will assume 'Q'. WARNING: don't import anything into 'main'; this is a major security risk!!!! NOTE 1: Variable names are transformed as necessary into legal Perl variable names. All non-legal characters are transformed into underscores. If you need to keep the original names, you should use the param() method instead to access CGI variables by name. NOTE 2: In older versions, this method was called import(). As of version 2.20, this name has been removed completely to avoid conflict with the built-in Perl module import operator.

    Gary Blackburn
    Trained Killer