You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Colin Wetherbee <cw...@denterprises.org> on 2007/06/11 04:14:41 UTC
[mp2] The right way to use CGI parameters in a handler
Hello!
It's been about four years since I've used mod_perl to any great extent,
and I'm afraid I'm somewhat rusty. I'm trying to create a handler that
can parse CGI parameters, but I'm unsure of the most modern way of doing
that.
I know Apache2::Request is supposed to be able to grab CGI parameters;
is that "better" than using CGI.pm? How about these APR modules that
seem to wind their way into the framework?
When I tried creating an Apache2::Request->new($r) object to use to get
CGI parameters, I found that everything I tried to print after the
object's instantiation wouldn't show up anywhere. All the pages served
were zero bytes, and I couldn't even print to STDERR.
Here's a simple handler I wrote. It works, but I'm afraid I might be
doing something incorrectly by mixing Apache2 and CGI. I'm wondering
how it would look if someone who was more up-to-date on mod_perl wrote
it? :)
# BEGIN CODE BLOCK
use Apache2::Const -compile => qw(OK);
use CGI;
sub handler
{
my ($r) = @_;
my $cgi = CGI->new($r);
$r->content_type('text/html');
if (defined $cgi->param('foo'))
{
print 'foo: ' . $cgi->param('foo') . "<br />\n";
}
return Apache2::Const::OK;
}
# END CODE BLOCK
Thanks. :)
Colin
Re: [mp2] The right way to use CGI parameters in a handler
Posted by Jeff Pang <pa...@earthlink.net>.
Foo JH 写道:
> You'd see that Apache2::Request exposes more stuff, which you can
> (lazily) use. Unless you are looking for some backward compatibility to
> tradditional CGI programming, why not embrace the entire modperl library?
>
I second it.Using Apache2::Request is not harder than CGI,but much faster.
Re: [mp2] The right way to use CGI parameters in a handler
Posted by Colin Wetherbee <cw...@denterprises.org>.
Colin Wetherbee wrote:
> # BEGIN CODE BLOCK
> my $req = Apache2::Request->new($r);
>
> $r->content_type('text/html');
> $r->print("test<br />\n");
> # END CODE BLOCK
OK, problem solved.
I didn't have mod_apreq enabled. For what it's worth, I don't recall
seeing anything about enabling mod_apreq in the documentation; it just
said enable mod_perl, and showers of gold will fall from the sky or
something. :P
Thanks for your help, everyone.
Colin
Re: [mp2] The right way to use CGI parameters in a handler
Posted by Colin Wetherbee <cw...@denterprises.org>.
Jonathan Vanasco wrote:
>> As I said in my original post, I tried to use Apache2::Request, but
>> after creating that object, I couldn't print anything anymore.
>> Perhaps someone could give me a hint on how to implement it?
>
> could you print beforehand?
>
> 'print' might not be tied to the same output.
Printing beforehand wouldn't allow me to use the CGI parameters to
determine the output. :)
> this might not be right based on your compile.
>>>> print 'foo: ' . $cgi->param('foo') . "<br />\n";
>
> try:
> $r->print( 'foo: ' . $cgi->param('foo') . "<br />\n" );
No dice.
# BEGIN CODE BLOCK
my $req = Apache2::Request->new($r);
$r->content_type('text/html');
$r->print("test<br />\n");
# END CODE BLOCK
The content-type doesn't even show up in this case.
If I put the $r->content_type() and $r->print() before the $req object
is created, *still* nothing shows up.
Colin
Re: [mp2] The right way to use CGI parameters in a handler
Posted by Jonathan Vanasco <jv...@2xlp.com>.
On Jun 11, 2007, at 4:29 PM, Colin Wetherbee wrote:
> That sounds good to me. I'm not looking for any particular
> backward compatibility, and speed is an issue.
last i checked the speed difference is negligible. its 'more proper'
to use libapreq, and if you're doing things in term of mod_perl
handlers (ie, not anything that would run as a std cgi ), then
there's little reason to use otherwise.
> As I said in my original post, I tried to use Apache2::Request, but
> after creating that object, I couldn't print anything anymore.
> Perhaps someone could give me a hint on how to implement it?
could you print beforehand?
'print' might not be tied to the same output.
this might not be right based on your compile.
>>> print 'foo: ' . $cgi->param('foo') . "<br />\n";
try:
$r->print( 'foo: ' . $cgi->param('foo') . "<br />\n" );
// Jonathan Vanasco
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - -
| CEO/Founder SyndiClick Networks
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - -
| FindMeOn.com - The cure for Multiple Web Personality Disorder
| Web Identity Management and 3D Social Networking
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - -
| RoadSound.com - Tools For Bands, Stuff For Fans
| Collaborative Online Management And Syndication Tools
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - -
Re: [mp2] The right way to use CGI parameters in a handler
Posted by Colin Wetherbee <cw...@denterprises.org>.
Foo JH wrote:
> You'd see that Apache2::Request exposes more stuff, which you can
> (lazily) use. Unless you are looking for some backward compatibility to
> tradditional CGI programming, why not embrace the entire modperl library?
That sounds good to me. I'm not looking for any particular backward
compatibility, and speed is an issue.
As I said in my original post, I tried to use Apache2::Request, but
after creating that object, I couldn't print anything anymore. Perhaps
someone could give me a hint on how to implement it?
Here's the original code again.
>> # BEGIN CODE BLOCK
>> use Apache2::Const -compile => qw(OK);
>> use CGI;
>>
>> sub handler
>> {
>> my ($r) = @_;
>>
>> my $cgi = CGI->new($r);
>>
>> $r->content_type('text/html');
>>
>> if (defined $cgi->param('foo'))
>> {
>> print 'foo: ' . $cgi->param('foo') . "<br />\n";
>> }
>>
>> return Apache2::Const::OK;
>> }
>> # END CODE BLOCK
Thank you very much.
Colin
Re: [mp2] The right way to use CGI parameters in a handler
Posted by Foo JH <jh...@extracktor.com>.
I've kinda moved out of CGI since I started using modperl for primarily
2 reasons: speed and functionality.
You'd see that Apache2::Request exposes more stuff, which you can
(lazily) use. Unless you are looking for some backward compatibility to
tradditional CGI programming, why not embrace the entire modperl library?
Colin Wetherbee wrote:
> Hello!
>
> It's been about four years since I've used mod_perl to any great
> extent, and I'm afraid I'm somewhat rusty. I'm trying to create a
> handler that can parse CGI parameters, but I'm unsure of the most
> modern way of doing that.
>
> I know Apache2::Request is supposed to be able to grab CGI parameters;
> is that "better" than using CGI.pm? How about these APR modules that
> seem to wind their way into the framework?
>
> When I tried creating an Apache2::Request->new($r) object to use to
> get CGI parameters, I found that everything I tried to print after the
> object's instantiation wouldn't show up anywhere. All the pages
> served were zero bytes, and I couldn't even print to STDERR.
>
> Here's a simple handler I wrote. It works, but I'm afraid I might be
> doing something incorrectly by mixing Apache2 and CGI. I'm wondering
> how it would look if someone who was more up-to-date on mod_perl wrote
> it? :)
>
> # BEGIN CODE BLOCK
> use Apache2::Const -compile => qw(OK);
> use CGI;
>
> sub handler
> {
> my ($r) = @_;
>
> my $cgi = CGI->new($r);
>
> $r->content_type('text/html');
>
> if (defined $cgi->param('foo'))
> {
> print 'foo: ' . $cgi->param('foo') . "<br />\n";
> }
>
> return Apache2::Const::OK;
> }
> # END CODE BLOCK
>
> Thanks. :)
>
> Colin
Re: [mp2] The right way to use CGI parameters in a handler
Posted by Joe Schaefer <jo...@sunstarsys.com>.
"Perrin Harkins" <pe...@elem.com> writes:
> On 6/10/07, Colin Wetherbee <cw...@denterprises.org> wrote:
>> I know Apache2::Request is supposed to be able to grab CGI parameters;
>> is that "better" than using CGI.pm?
>
> It's faster, but CGI.pm fully supports Apache 2 and works fine. If
> speed is not an issue for you, it's fine to stick with your CGI.pm
> code.
For a typical mod_perl application, I tend to agree. However there
is a significant difference in how apreq handles the raw post data
versus CGI. Because apreq is implemented as a transparent input
filter, it does not remove the post data from the input filter chain.
The upshot of that design is that it allows you to use apreq in
situations where CGI.pm's behavior of removing the post data is
undesirable, for instance when implementing an output filter or
an auth handler where the content handler itself isn't even mod-perl
based.
--
Joe Schaefer
Re: [mp2] The right way to use CGI parameters in a handler
Posted by Perrin Harkins <pe...@elem.com>.
On 6/10/07, Colin Wetherbee <cw...@denterprises.org> wrote:
> I know Apache2::Request is supposed to be able to grab CGI parameters;
> is that "better" than using CGI.pm?
It's faster, but CGI.pm fully supports Apache 2 and works fine. If
speed is not an issue for you, it's fine to stick with your CGI.pm
code.
- Perrin