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;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.