A few days ago I posted a question about a object-oriented plugin interface I was designing. I'm going to use the interface in a usenet cancelbot to examine a message in order to find out where to send complaints. Currently I've got modules to simply use the X-Complaints-To header, a abuse.net lookup and an advertised URL examiner.
package plugin; my @callbacks; sub register { push @callbacks, shift; } sub load { my $plugindir = shift; @callbacks = (); opendir PLUGIN, $plugindir or die "Cannot open dir $plugindir: + $!\n"; @plugins = grep { /\.pl$/ } readdir(PLUGIN); closedir PLUGIN; foreach $plugin (@plugins) { print "Loading $plugindir/$plugin...\n"; require "$plugindir/$plugin"; } return @callbacks; } 1; package example; sub new { return bless {}; } sub hello { my $self = shift; print "hello!\n"; } plugin::register(example->new); package main; use plugin; @plugins = plugin::load("a_directory"); foreach $plugin (@plugins) { $plugin->hello(); }

Replies are listed 'Best First'.
(Ovid) Re: Object oriented plugin interface
by Ovid (Cardinal) on Oct 27, 2001 at 04:02 UTC

    I see what you're trying to do, but trying to do Object-Oriented programming without using strict is a sure recipe for trouble.

    Also, I wouldn't have your "load" routine print the plugins that it is loading. It should return a list of successfully required plugins (and you should also check to see if the require is successful). If someone wants to use something like this, they may be very annoyed to see all of those print statements spilling out on their console.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Object oriented plugin interface
by jackdied (Monk) on Oct 30, 2001 at 04:43 UTC
    Is there any reason you are passing in the sub? If you wanted to be somewhat OO, there should be a sub named callback() in each package. Perl doesn't have pure virtual objects (for C++ folk) or interfaces (for Java junkies), but you could pretend that each plugin adheres to an imaginary parent object. If you wanted to make a real parent that calls die() for every sub, that works too.

    If you wanted to be very OO, you could have the callback sub return a ref to an object of the plugin derived object. Your milage may vary

    If you go with the package name instead of coderef, check out This node for a short example of plugin::register().

    -jackdied