On Wed, Jun 16, 2004 at 05:19:57PM -0700, Steven Scotten wrote:
> Hello, and thanks in advance for your patience as I ask how to do 
> something rudimentary, but I seem to be rather dense and a week up to 
> my eyeballs in documentation has left me no closer than when I began.

No apology necessary.  It's quite clear that you've made an effort,
and that effort is greatly appreciated.

> Most of what I've read suggests that what I've been doing with CGI.pm 
> should be replaced by Apache::Request. In fact, my module that uses 
> CGI.pm throws errors when included in startup.pl:
> 
>   Can't locate object method "new" via package "CGI" (perhaps you 
> forgot to load "CGI"?)

use CGI();
CGI->compile();

For more information, 'perldoc CGI' and scroll down to where the
"-compile" switch is explained.

That should get rid of that error.  However, as you already know,
Apache::Request is preferred over CGI.pm in mod_perl for both
performance (Apache::Request is part of the Apache API and
written in C) and resource usage (CGI.pm takes a lot more 
memory).

Now, please realize that there are _numerous_ ways to have modperl
run your module.  ModPerl::Registry is one of them.  Writing an
actual "handler", in the vernacular of the Apache API, is another,
and those are not the only ways.

Also, modperl is not limited to producing content.  It can do all
sorts of other neat configuration, authentication, and more cool
stuff in Apache.  But that's a bedtime story for another day.

> So I've set to trying to replace CGI.pm entirely in one particular 
> module. It looks as though Apache::Request would have been my path in 
> mod_perl 1.x and I've been looking at 
> http://perl.apache.org/docs/2.0/user/porting/compat.html (among other 
> places) for what to do.
> 
> Under $r->request it says "Use Apache->request".

$r is generally where the request object is stored.
(Whenever you see $r in the documentation, it refers to the current
 request object.)

To use Apache::Request instead of CGI.pm, replace

  use CGI;
  $q = new CGI;

with:

  use Apache::Request ();
  my $q = Apache::Request->new($r);

and see 
http://perl.apache.org/docs/1.0/guide/porting.html#Converting_to_use_Apache_Perl_Modules

Now, you don't _have_ to replace CGI.pm.
Apache::Request is just another way to do things.



If writing a handler, which is yet another way to run code in modperl,
the request object will be passed to you as a param.

sub handler {
    my $r = shift;

    # my main routine code 
    example_subroutine($r,$_and,$other,$params);

    return Apache::Constants::OK;
}

sub example_subroutine {
    my($r,$_and,$other,$params) = @_;

    # other code
}

As shown above, for subroutines, pass $r as a parameter to
pass the request object between your routines.

If you fall into a situation where it is gross or obtuse to
pass $r as a parameter down through some chains, then in the
deeply nested subroutine, you can actually get $r by doing

sub deeply_nested_routine {
    $r = Apache->request;
}


Or, you can do something like:

use vars qw($r);

sub handler {
    ## always local()ize global variables in modperl!  (well, almost always)
    local $r = shift;

    # ...

    return Apache::Constants::OK;
}

sub deeply_nested_routine {
    # $r is a global variable that has been localized
    # We already have access to use it in this routine.
    my $uri = $r->uri;
}


TIMTOWTDI.


Once you have $r, you have easy access to a whole lot of information
and request parameters.  'perldoc Apache' or perldoc 'ModPerl' (I think)
for more information.

Hope I haven't confused you more by giving you so many options.

Cheers,
Glenn

-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html

Reply via email to