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

When using the CGI module
I'm using if(param()) {...} to check wether
there any parameters from a form that is sending them,but
there can also be parameters encoded in the url like http://.../script?param1=val1¶m2=val2
The problem is that this code if(param()) {...},more precise the param() function
is returning true if either the script receives parameters from the URL or from POST parameters
How do I know where the parameters are coming from ?

I can use a CGI object like this to get URL parameters
5 my $q=new CGI; 6 my $params=$q->Vars; 7 $params->{'id'};
and for the parameters which were passed through POST , I can get
those using param('name_of_param');

Thank you

Replies are listed 'Best First'.
Re: making a difference between POST parameters and URL parameters
by moritz (Cardinal) on Feb 14, 2008 at 15:56 UTC
    You can check with CGI::request_method which one was used, and you can access the GET parameters in the URL with url_param.

    If request_method was POST, use param to access the post params.

Re: making a difference between POST parameters and URL parameters
by Sinistral (Monsignor) on Feb 14, 2008 at 16:14 UTC
    The CGI module deliberately makes a unified interface so that you can do:
    if (defined ($q->param('foo')) { print "foo is defined!\n"; print "foo = ", $q->param('foo'), "\n"; }

    regardless of the means by which the parameters were generated.

    Well formed HTML forms either specify a method of POST _or_ GET, not both. Doing both is supported by CGI, but only because the CGI developers are kind to web developers, not because it's an actual part of the CGI standard. More explicitly, you shouldn't generally care which method is used.

    If you can provide more information about your problem and what you're trying to accomplish, other monks might be able to help you solve the real issue, as opposed to processing GET and POST simultaneously

      Well formed HTML forms either specify a method of POST _or_ GET, not both.

      That's right, but you can use an html that has "post" as the request method to an URL that contains URL params, and the w3c spec doesn't recommend against it.

      (Or is that anywhere else, and I just haven't found it?)

      Doing both is supported by CGI, but only because the CGI developers are kind to web developers, not because it's an actual part of the CGI standard.

      Actually, both are in the standard. See here

      update: misunderstood the quoted line. I was trying to say that both methods are in the standard, not the usage of both simultaneously.

        Hmmm ... I wonder if Doing both is more about CGI possibly processing *both* URL params and STDIN params for a POST? One of the best comments in cpan:

        if ($meth eq 'POST') { $self->read_from_client(\$query_string,$content_length,0) if $content_length > 0; # Some people want to have their cake and eat it too! # Uncomment this line to have the contents of the query string # APPENDED to the POST data. # $query_string .= (length($query_string) ? '&' : '') . $ENV{'QUERY_ +STRING'} if defined $ENV{'QUERY_STRING'}; last METHOD; }
        ... I like cake.

        -derby