8

How can I read the URL parameter in a Perl CGI program?

1
  • 5
    Stackoverflow is a goto for many, and Google's "I feel lucky" points here for several searches. So these comments above are useless noise and beg for stackexchange to add downvoting for comments. Commented Oct 19, 2012 at 21:05

3 Answers 3

13

For GET requests, CGI parses the specified parameters and makes them available via the param() method.

For POST requests, param() will return the parameters from the postdata, but any parameters specified via a query string in the URL itself are still available from the url_param() method. (This is can be helpful when a POST request is larger than $CGI::POST_MAX; in that case, CGI just discards the postdata, but you can arrange to have query string parameters that identify what kind of request it was to provide a good error message.)

For ISINDEX style requests, the keywords requested are available via the keywords() method, as well as via param() in a faux "keywords" parameter.

Update: in case you meant something other than the parameters by "URL Parameter", the url() method provides all or parts of the requested URL; see OBTAINING THE SCRIPT'S URL.

Sign up to request clarification or add additional context in comments.

Comments

5

It's recommended that you use a URL parser such as mentioned by ysth, but if you REALLY want the raw input, it's available through the following:

for GET:

$contents = $ENV{'QUERY_STRING'};

for POST:

$contents = <STDIN>;

Comments

3

Try thus code:

my @names = $query->param;
foreach $name ( @names ) {
    if (  $name =~ /\_/ ) { 
        next;
    } else {
        print "<p> ".$name."\t=\t".$query->param($name) . "</p>\n";
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.