Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: ModPerl: ModPerl

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

 

 

ModPerl modperl RSS feed   Index | Next | Previous | View Threaded


himanshu.garg at gmail

Sep 23, 2008, 8:06 AM

Post #1 of 8 (1239 views)
Permalink
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


fred at redhotpenguin

Sep 23, 2008, 8:50 AM

Post #2 of 8 (1207 views)
Permalink
Re: How to extract name/value pairs from the query string? [In reply to]

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
>


jdrago_999 at yahoo

Sep 23, 2008, 9:32 AM

Post #3 of 8 (1208 views)
Permalink
Re: How to extract name/value pairs from the query string? [In reply to]

--- On Tue, 9/23/08, Himanshu <himanshu.garg [at] gmail> wrote:

> From: Himanshu <himanshu.garg [at] gmail>
> Subject: How to extract name/value pairs from the query string?
> To: modperl [at] perl
> 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


adam.prime at utoronto

Sep 23, 2008, 10:09 AM

Post #4 of 8 (1206 views)
Permalink
Re: How to extract name/value pairs from the query string? [In reply to]

Quoting Fred Moyer <fred [at] redhotpenguin>:
> 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


perrin at elem

Sep 23, 2008, 10:55 AM

Post #5 of 8 (1201 views)
Permalink
Re: How to extract name/value pairs from the query string? [In reply to]

On Tue, Sep 23, 2008 at 12:32 PM, John Drago <jdrago_999 [at] yahoo> 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


aw at ice-sa

Sep 23, 2008, 11:51 AM

Post #6 of 8 (1187 views)
Permalink
Re: How to extract name/value pairs from the query string? [In reply to]

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


perrin at elem

Sep 23, 2008, 12:10 PM

Post #7 of 8 (1190 views)
Permalink
Re: How to extract name/value pairs from the query string? [In reply to]

On Tue, Sep 23, 2008 at 2:51 PM, André Warnier <aw [at] ice-sa> 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


himanshu.garg at gmail

Sep 23, 2008, 5:39 PM

Post #8 of 8 (1201 views)
Permalink
Re: How to extract name/value pairs from the query string? [In reply to]

2008/9/23 Perrin Harkins <perrin [at] elem>

> On Tue, Sep 23, 2008 at 12:32 PM, John Drago <jdrago_999 [at] yahoo> 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
>

ModPerl modperl RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.