tadamec has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

I'm in the process of designing an application and have run into a limit of my Perl knowledge.

I'm attempting to create a handler under mod_perl that would allow me to specify different content generators based upon a configuration file or database entry. For example:

### Config file /foo.html = Generator::Foo /bar.html = Generator::Bar /foo/bar = Generator::Baz
The perl handler should look at the incoming URI and route the request to an instance of the appropriate object, as specified by the config file.

I knew the following was broken under strict when I wrote it, but I had to try...

# $generator is set by reading the above config file. my $g = $$generator->new( $r );
Obviously, $$generator->new(...) won't work with strict, but I can't seem to reason out how to create objects like this on the fly.

I've checked out the nodes on "Dynamic loading of object-oriented plugins" and "plugins and inheritance" but I didn't seem to find anything that really answered my question.

Thanks for any help!

tadamec

Replies are listed 'Best First'.
Re: "Dynamic" object creation.
by PodMaster (Abbot) on Jun 27, 2004 at 07:29 UTC
    use strict; use warnings; use CGI; my $blah = 'CGI'; my $new = $blah->new(); die $new; __END__ CGI=HASH(0x224f9c)

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Ergh.

      Test code that I used had the module name in double quotes instead of single quotes. Man, gotta quit programming when I'm so tired. I noticed later that my mistake wasn't interpolated vs. uninterpolated strings, but was the fact that I was trying to dereference a scalar (via $$ notation), rather than just using the value of the scalar in place of a module name.

      Thanks for the fix on my bonehead mistake.