Ben Wilder wrote:
> 1) Your handler example is fantastic, however in my application I need to
> set the $pkg (the package to be dynamically included) from some logic in
> another script, so the process may go as follows: 
> User request -> main script -> database call to get $pkg name - >
> dynamically call the package (as per your example).
See attached script.pl, Util.pm, and httpd.conf which will runs under the 
ModPerl::Registry
handler (handlers are better though :)).  The functionality is now in a library 
and can be called
from anywhere.

Your variables need to be scoped to a FUNCTION via
'my' (or re-initialized properly on each request) This scoping is the solution 
to your caching problem.

You need to read up on the "Core" modperl principles and what it actually does 
with your code and how it caches it.

http://perl.apache.org/docs/1.0/guide/porting.html
While for 1.x the same principles apply to 2.x

> If my thinking is correct (and I'm a little new to Mod_perl!) the handler
> example you kindly wrote will be called when the /foo path is visited,
> however I will be needing to deliver this from a number of places (and will
> not be able to rely on paths in the url) and would IDEALLY call the dynamic
> functionality from within a script, rather than having it as a handler. Is
> there a way to do this and still not have the cache problem?
Thats correct, and see the attachments.

> 2) What is the best way to install the libapreq objects, can the same
> functionality be got through the Apache2 classes?
http://httpd.apache.org/apreq
download the lastest 2.08 un zip/tar and read the INSTALL file.
Apache2::* classes are wrapper around the APR::* classes (similar but not 
always directly equivalent)

In general though both can get you what you want.
(attachments use Apache2::Request instead of APR::Request::Apache2)
http://httpd.apache.org/apreq/docs/libapreq2/modules.html

HTH

------------------------------------------------------------------------
Philip M. Gollucci ([EMAIL PROTECTED]) 323.219.4708
Consultant / http://p6m7g8.net/Resume/resume.shtml
Senior Software Engineer - TicketMaster - http://ticketmaster.com
1024D/A79997FA F357 0FDD 2301 6296 690F  6A47 D55A 7172 A799 97F

"In all that I've done wrong I know I must have done something right to
deserve a hug every morning and butterfly kisses at night."
   __  ___     ___ ____  __
  /  |/  /_ __/ __/ __ \/ /
 / /|_/ / // /\ \/ /_/ / /__
/_/  /_/\_, /___/\___\_\___/
       <___/
package My::Util;

#core
use strict;
use warnings FATAL => 'all';
use Carp;

#mp2
use Apache2::Log ();

#libapreq
use Apache2::Request ();

#cpan
use DBI ();

#custom

sub _sql { "SELECT name FROM packages WHERE foo = ?" }

sub get_pkg {
    my $req = shift;

    my $dbh = eval { DBI->connect(.....) };
    confess $@ if $@;

    my $foo = $req->param('foo');
    ## VALIDATE $foo VERY carefully for MALICOUS input!
    ## VALIDATE $foo VERY carefully for MALICOUS input!
    ## VALIDATE $foo VERY carefully for MALICOUS input!
    ## VALIDATE $foo VERY carefully for MALICOUS input!

    my $sth = $dbh->prepare(_sql());
    $sth->execute($foo);
    my ($pkg) = $sth->fetchrow_array();
    $sth->finish();

    my $rc = $dbh->disconnect();
    confess $rc unless $rc;

    return $pkg;
}

sub dispatch {
    my ($r, $pkg) = @_;

    if (eval "require $pkg") {
        ## if this can fail or not exist, eval {} it
        ## or use UNIVERSAL::can()
        my $rv = $pkg->methodX();
    }
    else {
        $r->log_error("$pkg require failed\n")
    }

    return $rv;
}
Alias /perl-reg /www/perl-scripts

PerlModule ModPerl::Registry
<Location /perl-reg>
  SetHandler perl-script
  PerlResponseHandler ModPerl::Registry
  Options +ExecCGI
</Location>
#!/usr/bin/perl -w

#core
use strict;
use warnings FATAL => 'all';
use Carp;

#mp2

#libapreq
use Apache2::Request ();

#cpan

#custom
use My::Util ();

## This works because of what mod_perl does
my $r   = shift;

sub main {

    my $req = Apache2::Request->new($r);

    my $pkg = My::Util::get_pkg($req);
    my $rv = My::Util::dispatch($r, $pkg);

    return;
}

main();

Reply via email to