Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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

by vroom (Pope)
on Jan 20, 2000 at 23:43 UTC ( #2244=categorized question: print w/replies, xml ) Need Help??
Contributed by vroom on Jan 20, 2000 at 23:43 UTC
Q&A  > CGI programming

Answer: How do I get at the parameters in my CGI program?
contributed by vroom

You're going to want to start with 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
Answer: How do I get at the parameters in my CGI program?
contributed by howard40

If the only thing you need to do is read in the submitted fields, i highly suggest you use instead. It's smaller, faster, and (IMO) nicer to use. It is available on the authour's website 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;
Answer: How do I get at the parameters in my CGI program?
contributed by icuc

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...

Answer: How do I get at the parameters in my CGI program?
contributed by turnstep

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; } }

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [choroba]: 10 bugs in the tracker?
    [1nickt]: Yes, and recommendation against using it, in the doc for Devel::Declare, on whcih it is based.
    [1nickt]: I asked the new $boiss what I should brush up on before starting work next week, and he named Moo, DBIx::Class and Method::Signatures
    [1nickt]: choroba That's why I asked if anyone is using it :-)
    choroba only has used Moo from the htree
    [1nickt]: Pardonm?

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (6)
    As of 2017-11-24 13:21 GMT
    Find Nodes?
      Voting Booth?
      In order to be able to say "I know Perl", you must have:

      Results (349 votes). Check out past polls.