Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

( #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 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
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 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;
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!
  • 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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (10)
    As of 2015-07-06 21:46 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (83 votes), past polls