Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Dynamic loading of object-oriented plugins

by Erik Hensema (Sexton)
on Oct 24, 2001 at 01:21 UTC ( [id://120920]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to develop an application which is able to dynamically load plugins. These plugins are objects (packages).

I've got some code working, but I've got the feeling it's not the prettiest of all scripts :-/

Basically my in the main package I load the plugins by calling require. The plugins then register themselves to the main package by calling this function:

sub register_plugin { push @callbacks, shift; }

A plugin basically looks like this:

register_plugin("foo"); package foo; sub new { return bless {}; } sub hello { print "Hello!\n"; } 1;

Then this works from main:

foreach $callback (@callbacks) { $bar = $callback->new(); $bar->hello(); }

I think it's quite ugly to return the package name as a string to main. Isn't there some other way, preferably using a reference or something like that?

Replies are listed 'Best First'.
Re: Dynamic loading of object-oriented plugins
by dragonchild (Archbishop) on Oct 24, 2001 at 01:34 UTC
    It would seem to me that the plugin isn't the class, but the object, or the instantiation of that class. So, I'd look at doing something like (completely off the cuff):
    package foo; my $foo_thing = foo->new; &::register_plugin($foo_thing); sub new { return bless {}, shift } sub hello { print "Hello from foo!\n" } 1; package main; sub register_plugin { push @plugins, shift; } foreach $plugin (@plugins) { $plugin->hello; }
    Neat idea, btw.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: Dynamic loading of object-oriented plugins
by jackdied (Monk) on Oct 24, 2001 at 02:43 UTC
    Check out Re: Re (tilly) 1: Stopgap measures when you don't have a QA department, it demonstrates a little registration sub for packages that uses caller instead of passing the package name. I've used this strategy sucessfully in the past. Just be careful if you are using mod_perl and STAT_INC, when the GateKeeper (central registration point) module reloads, it will forget all the packages that have registered up to that point.

    Now for the fun part, you can have multiple stragegies for figuring out which modules to load. You could have a basic static list, a run-time user interface, and do a foreach on a readdir, requiring all modules in the puglins/ directory.

    This is a great base for a Factory class or a list of arbitrary handlers.

    -jackdied

Re: Dynamic loading of object-oriented plugins
by chromatic (Archbishop) on Oct 24, 2001 at 08:30 UTC
    That's allowed and quite fine, in my opinion. Another approach is to use a reference, as you say:
    sub register_plugin { my $class = shift; if (defined(my $constructor = UNIVERSAL::can($class, 'new'))) { push @callbacks, sub { $constructor->($class) }; } } foreach my $callback (@callbacks) { my $obj = $callback->(); $obj->hello(); }
    It's a little more paranoid, but I prefer your original version.
Re: Dynamic loading of object-oriented plugins
by tomhukins (Curate) on Oct 24, 2001 at 02:36 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://120920]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-24 00:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found