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;

Replies are listed 'Best First'.
Re: plugins revisited
by broquaint (Abbot) on Dec 19, 2003 at 14:32 UTC
    You could drop that eval STRING and still keep within the bounds of strict e.g
    use Module::Locate 'locate'; my @plugFuncs; for(@plugins) { require( locate $_ ) and $_->import; $_->can('init')->(); push @plugFuncs, $_->can('worker'); }
    And while Module::Locate isn't necessary (although it was designed for this sort of usage), it does take out the drudgery of munging class names into file paths. You might also be interested in Simon Wistow's Module::Pluggable.
    HTH

    _________
    broquaint