http://qs321.pair.com?node_id=2244

vroom has asked for the wisdom of the Perl Monks concerning the following question: (cgi programming)

How do I get at the parameters in my CGI program?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I get at the parameters in my CGI program?
by vroom (His Eminence) on Jan 20, 2000 at 23:44 UTC
    You're going to want to start with CGI.pm and the param function
    use CGI; $query=CGI::new(); $param1=$query->param('param1'); @params=$query->param; #gets a list of all the names of the parameters + passed to the script
Re: How do I get at the parameters in my CGI program?
by slayven (Pilgrim) on Nov 12, 2000 at 05:20 UTC
    I really like this one:

    use CGI; my $q = new CGI; my %in = map { $_ => $q->param($_) } $q->param;
      Unfortunately, this fails when a single parameter name has multiple values, as in the case of a multiple selection box.

      That's not encountered often, but the failure when it is is spectacular enough that it bears mention.

        It shows up whenever you use checkboxes. I'd say that's pretty often!
Re: How do I get at the parameters in my CGI program?
by howard40 (Beadle) on Feb 27, 2000 at 08:10 UTC
    If the only thing you need to do is read in the submitted fields, i highly suggest you use Deurl.pm instead. It's smaller, faster, and (IMO) nicer to use. It is available on the authour's website http://Jenda.Krynicky.cz. Basically, the syntax is this, if you want to do it the simplest way possible
    use Deurl; my %FORM = %Deurl::query;
    but if you want to make it a little speedier, do this (the hash doesn't get copied, only aliased)
    use Deurl; local (*FORM) = \%Deurl::query;
Re: How do I get at the parameters in my CGI program?
by vroom (His Eminence) on Nov 15, 2000 at 23:42 UTC
    come again

    Originally posted as a Categorized Answer.

Re: How do I get at the parameters in my CGI program?
by icuc (Initiate) on Aug 02, 2000 at 10:47 UTC
    As contributed by icuc:
    use strict; use CGI; # ..... my $q = new CGI; my %params = {}; { my @params_names = $q->param; for(@params_names) { $params{$_} = $q->param($_); } }

    or, a shorter way, as contributed by slayven:

    use CGI; my $q = new CGI; my %in = map { $_ => $q->param($_) } $q->param;

    Click this Answer to see merlyn's note about $q...

      Lincoln Stein no longer recommends the use of the visible CGI object, simply because the number of CGI objects in a typical program is merely one. Instead, pull in the methods you need as subroutines. If parameter access is all that's required, use:
      use CGI qw(param); ... my $source = param('source'); my @destination_states = param('dest'); # multi-select
      But I typically use
      use CGI qw(:all);
      since I'm also using the HTML generation shortcuts.

      -- Randal L. Schwartz, Perl hacker

Re: How do I get at the parameters in my CGI program?
by turnstep (Parson) on Mar 29, 2000 at 21:10 UTC
    Here's a way to do "by hand":

    Please don't do this. It is shown here for illustrative purposes only

    ## First, grab any GET stuff (overrrides POST) if ($ENV{'REQUEST_METHOD'} eq "GET") { $sinstring = $ENV{'QUERY_STRING'} } ## Could change this to a simple "if" to allow GET *and* POST elsif ($ENV{'REQUEST_METHOD'} eq 'POST' && $ENV{'CONTENT_TYPE'} eq "application/x-www-form-urlencoded") { ## Okay if CONTENT_LENGTH is 0... read(STDIN, $sinstring, $ENV{CONTENT_LENGTH}); } else { ## Last ditch efforts, no REQUEST METHOD found ## Any GET info? $sinstring = $ENV{'QUERY_STRING'}; ## Any POST info? $sinstring || read(STDIN, $sinstring, $ENV{CONTENT_LENGTH}); } ## Find all of our sins: for (split(/\&/, $sinstring)) { if (($sin_name, $sin_val) = /(.*)=(.*)/) { ## Normal... $sin_val =~ tr/+/ /; $sin_val =~ s/%(..)/pack('c',hex($1))/eg; } else { ## Abnormal... $sin_name = $_; $sin_val = "0"; } $sin_name =~ tr/+/ /; $sin_name =~ s/%(..)/pack('c',hex($1))/eg; $sins++; if (defined $sin{$sin_name}) { ## Allows for "0" cases.... $sin{$sin_name} .= '#' . $sin_val; } else { $sin{$sin_name} = $sin_val; } }