You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Himanshu <hi...@gmail.com> on 2008/09/23 17:06:16 UTC

How to extract name/value pairs from the query string?

Hi,

     What is the recommended module to get the name/value pairs from the
query string. Apache2::RequestRec::args comes close but there must be
something easier to use.

sub login_response {
    my $r = shift;
    my $args = $r->args();
    ...
}

Thanks,
Himanshu

Re: How to extract name/value pairs from the query string?

Posted by ad...@utoronto.ca.
Quoting Fred Moyer <fr...@redhotpenguin.com>:
> See Apache2::Request, it mirrors the CGI param api.

In case you dont know, Apache2::Request is part of libapreq2  
(http://httpd.apache.org/apreq/).  IMO, using it is the best way to  
achieve what you're trying to do if you're developing for mod_perl  
only.  (ie you don't care if the code will run under plain CGI or not)

Adam



Re: How to extract name/value pairs from the query string?

Posted by Fred Moyer <fr...@redhotpenguin.com>.
Himanshu wrote:
> Hi,
> 
>      What is the recommended module to get the name/value pairs from the 
> query string. Apache2::RequestRec::args comes close but there must be 
> something easier to use.
> 
> sub login_response {
>     my $r = shift;
>     my $args = $r->args();
>     ...
> }

See Apache2::Request, it mirrors the CGI param api.


> 
> Thanks,
> Himanshu
> 

Re: How to extract name/value pairs from the query string?

Posted by Himanshu <hi...@gmail.com>.
2008/9/23 Perrin Harkins <pe...@elem.com>

> On Tue, Sep 23, 2008 at 12:32 PM, John Drago <jd...@yahoo.com> wrote:
> > Ha no not really.  Somehow everything else must suffer (usability,
> debugging, intuitiveness, etc) so that we can have a *fast* web programming
> environment. (Yay!)
>
> While I agree that the splitting of the APIs you're referring to is
> strange and confusing, this actually has nothing to do with that.
> There is no parsing of form data in $r->args built into mod_perl 2
> because the previous attempt at it in mod_perl 1 was broken and
> considered a bad idea in retrospect.  Both libapreq2 and the standard
> CGI module provide robust and easy-to-use parsing of form data for
> mod_perl 2.


Thanks for the replies. Apache2::Request has the method I needed. Sorry that
I missed it from the docs. Using CGI.pm looked odd because I was not doing
any CGI anywhere.

Thank You,
Himanshu


>
>
> - Perrin
>

Re: How to extract name/value pairs from the query string?

Posted by Perrin Harkins <pe...@elem.com>.
On Tue, Sep 23, 2008 at 12:32 PM, John Drago <jd...@yahoo.com> wrote:
> Ha no not really.  Somehow everything else must suffer (usability, debugging, intuitiveness, etc) so that we can have a *fast* web programming environment. (Yay!)

While I agree that the splitting of the APIs you're referring to is
strange and confusing, this actually has nothing to do with that.
There is no parsing of form data in $r->args built into mod_perl 2
because the previous attempt at it in mod_perl 1 was broken and
considered a bad idea in retrospect.  Both libapreq2 and the standard
CGI module provide robust and easy-to-use parsing of form data for
mod_perl 2.

- Perrin

Re: How to extract name/value pairs from the query string?

Posted by Perrin Harkins <pe...@elem.com>.
On Tue, Sep 23, 2008 at 2:51 PM, André Warnier <aw...@ice-sa.com> wrote:
> I'm sure someone can think of a way to strip the possible leading and
> trailing spaces off the keys at the same time though.

Please don't.  Some of the reasons for using a well-tested parser like
CGI or libapreq2 are listed here:
http://perl.apache.org/docs/2.0/user/porting/compat.html#C__r_E_gt_args__in_an_Array_Context

- Perrin

Re: How to extract name/value pairs from the query string?

Posted by André Warnier <aw...@ice-sa.com>.
John Drago wrote:
[...]

> 
> my %args = map { my ($k,$v) = split /\=/, $_; unescape($k) => (unescape($v) } split /\&/, $r->args;
> 
It's lines like this that make perl scripts small and perl great, and 
give them both a bad name for Java programmers.
I'm sure someone can think of a way to strip the possible leading and 
trailing spaces off the keys at the same time though.
;-)




Re: How to extract name/value pairs from the query string?

Posted by John Drago <jd...@yahoo.com>.


--- On Tue, 9/23/08, Himanshu <hi...@gmail.com> wrote:

> From: Himanshu <hi...@gmail.com>
> Subject: How to extract name/value pairs from the query string?
> To: modperl@perl.apache.org
> Date: Tuesday, September 23, 2008, 9:06 AM
> Hi,
> 
>      What is the recommended module to get the name/value
> pairs from the
> query string.


You could just do this:

my %args = map { my ($k,$v) = split /\=/, $_; unescape($k) => (unescape($v) } split /\&/, $r->args;

sub unescape {
  # Try CGI::Simple or something else for an unescape method.
}



> Apache2::RequestRec::args comes close but
> there must be
> something easier to use.

Ha no not really.  Somehow everything else must suffer (usability, debugging, intuitiveness, etc) so that we can have a *fast* web programming environment. (Yay!)

Doing those kinds of things that nobody ever really does (like, gee, parsing form contents) remains something of an oddity in the realm of mod_perl.  *However* if you need to print "HELLO, WORLD!" almost as fast as a static file does...well, we've got it covered!

NOTE:: You could also check out CGI::Apache2::Wrapper on CPAN.  It does everything you want, but you have to play with it to get it installed (just force install).

Best regards,
John Drago




> 
> sub login_response {
>     my $r = shift;
>     my $args = $r->args();
>     ...
> }
> 
> Thanks,
> Himanshu