Recently I implemented a simple plugin scheme that some of you might find interesting. I build a list of plugins that are called successively until one of them returns 1. This allows me to skip an entry (always return 1), or check values (always return 0), or have default actions (place the plugin at the end of the list). Comments welcome.
The main program that loads plugins...
#!/usr/bin/perl -w
use strict;
use lib "./plugins"; # we place plugins here
my @plugins = (
'nullPlugin',
'checkPlugin',
'printPlugin'
);
my @plugFuncs;
foreach my $plugin (@plugins){
eval "use $plugin;
$plugin\:\:init();
push \@plugFuncs, \\\&$plugin\:\:worker;
";
}
# Point of use...
my $res = 0;
foreach my $plugFunc (@plugFuncs){
last if $res == 1;
$res = &{ $plugFunc }($data);
}
Sample plugin code:
package nullPlugin;
# A plugin that skips IDs found in a skiplist-file
use strict;
our %skiplist;
sub init {
open INPUT, "nullPlugin-list" or return 1;
while(<INPUT>){
chomp;
$nullPlugin::skiplist{$_} = undef if /^\d+$/;
}
close INPUT;
return 1;
}
sub worker {
my $id = shift;
return 1 if exists($nullPlugin::skiplist{$id});
}
1;