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();
}