=head1 NAME Plugins - Generic plugins framework =head1 SYNOPSIS use Plugins; $plugins = Plugins->new context => $context; $plugins->readconfig($config_file, self => $self); $plugins->initialize(); $plugins->invoke($method, @args); $plugins->invoke_until($method, sub { scalar(@_) }, @args); my $iterator = $plugins->iterator(); while (@results = &$iterator(@args)) { } for my $plugin ($plugins->plugins()) { $plugin->invoke($method); } =head1 DESCRIPTION Plugins provides a simple way for a programs to be assembled at runtime via configuration files. Generally, there are only a few ways that a module in a library can be customized for an application: (1) You can change the code of the module; (2) if it presents an object-oriented interface you can subclass it; or (3) the module in question can have a configuration file that allows you to do what you need. Plugins makes it easier to allow behavior to be modified by a configuration file by allowing other code to be mixed in. Plugins allows plugins have plugins and for all the plugins to share a single configuration file. This isn't required but sometimes it's nice to have everything in one place. Each plugin is implemented by a perl module. There can be more than one instance (object) for each plugin (class). The Plugins module itself isn't complete and must be subclassed. An example subclass that completes Plugins is L. Plugins written for use with the Plugins module generally don't need to know how Plugins has been subclassed because the C<$context> that is passed to a plugin allows it to use Plugins directly even if its ancestor did not. =head1 NAMING CONVENTION Since plugins can have plugins, some plugins will have Plugins objects themselves. Some won't. We will term a user of Plugins to be I. A I that is not itself a plugin will be termed the I. A plugin will be termed a I and the I that invoked it will be termed the I. A plugin that has plugins is a I. =head1 CONSTRUCTION AND INITIALIAZATION While there are similarities, using Plugins for the first time (by the I) is different than using it as a plugin that has plugins (I). =head2 I initialization. The I will need to create a plugins object. Since Plugins needs to be subclassed it will create the object using the subclass. For example, L: $plugins = Plugins::Style1->new(%args); $plugins->readconfig($configfile, %args); $plugins->initialize(); =head2 I initialization There are three steps for a I to take to start using plugins. Each step is a call to Plugins: $plugins = Plugins->new(context => $context, %args); $plugins->readconfig($configfile, %args); $plugins->initialize(); The most important argument for C is C $context>. The C<$context> comes from the first argument to I-Enew() when Plugins creates plugin object instances. For I, no C<%args> are needed for new() except for C $context>. Likewise, no C<%args> are needed for C. =head2 %args for new() There may be additional parameters depending on how Plugins is subclassed. The C<%args> that C userstands are: =over 4 =item configfile =E "/config/file" Provide a configuration file name. If this is done here then C can be passwd for the configfile to C. =item api =E $api Provide a L object that will be passed to any plugins's C invocation. =back =head2 %args for readconfig() Readconfig has a positional argument: C<$configfile>: $plugins->readconfig($configfile, %args); C<%args> for C depend upon how Plugins is subclassed. =head1 USING PLUGINS Plugins provides the following methods for I: =over 4 =item invoke($method, @args) This calls each plugin in turn. Plugins are called in the order in which they were configured. No return value is defined. The calls are not eval-wrapped so a C in a plugin is fatal. =item invoke_until($method, $testfunc, @args) This calls each plugin in turn. Plugins are called in the order in which they were configured. After each call, the return value from the plugin method invocation is evaluated with C<&$testfunc()>. If C<&$testfunc()> returns a true value, then the return value from the plugin method invocation is returned and no further calls are made. =item plugins() This returns the list of plugin objects. This will be in the order in which the plugins were defined. =item iterator($method) This returns an anonymous function that when invoked will call the first plugin (C<$plugin->method(@args)). Each successive call will call another plugin until it returns undef. =item startconfig() This method can be used instead of C to start the configuration process. It does not read any configuration files. It is required prior to C or C. =item parseconfig($configfile, %args) This parses a configuration file. Unlike C it does not call C first. =item api($api) This will set an $api variable that will be passed to C when creating new plugin instances. This is expected to be a L object. =back =head1 RE-CONFIGURATION The C method may be called more than once. Each time it is called, C may be called again. When C is called a second time, it calls C<$plugin-Eshutdown()> for each of the old plugins before calling C<$plugin-Enew()> to create the new ones. Each call to C starts fresh. If you want to add to an existing set of plugins rather than replace them, use C instead of C. =head1 WRITING PLUGINS Plugins can be either a perl module that can be found by looking in C<@INC> or they can be files that are wrapped and eval'ed by the Plugins module. For plugins that are files, they will be wrapped with a bit of code: package Plugins::AutoGenerated::A_UNIQUE_GENERATED_VALUE; our @ISA = qw(Plugins::Plugin); use strict; THE_CONTENTS_OF_THE_FILE This wrapping is done by C method. For plugins that are regular perl modules and thus not auto-wrapped, they should declare themselves to be subclasses of C. Plugin objects provide the following methods: =over 4 =item invoke($method, @args) Basically this just does C<$plugin-Emethod(@args)>. If C<$method> does not exist, just return undef. =item new() The default C does the following: sub new { my ($pkg, $pconfig, %args) = @_; return bless { context => $pconfig->{context}, api => $pconfig->{api}, config => \%args }, $pkg; } You'll often want to override C. =back =head2 Plugin-defined methods Plugins will need to define methods to be called. What methods need to be defined depends upon what they are a plugin for. Plugins for programs that are a L will generally need C and C methods. Look at the documentation or code of the I. The following methods are called directly by Plugins: =over 4 =item new($pconfig, @args) Invoked to create an object instance of a plugin. The C<@args> come from the configuration file. The C<$pconfig> is used to pass in plugin-related configuration data. It's a reference to a hash and C<$pconfig-E{context}> needs to be passed to any I so that they can fit themselves in properly. =item shutdown() Called when a object instance is no longer needed due to a reconfig after the configuration file has been changed. On a reconfiguration, all the old objects are destoryed and a new set are created. The default C doesn't do anything. =back =head2 AUTOLOAD with L The Plugins module has defines an C function in Plugins::Plugin base class to map unknown method invocations into C. It does this through the L module. Plugins that invoke a method that isn't formally defined will have the attempted method invocation redirected through C<$self-E{myapi}-Einvoke()> or C<$self-E{api}-Einvoke()>. The default C for plugins will capture a L object passed in from Plugins as C<$self-E{api}>. Plugins that have plugins might want to name their L object C<$self-E{myapi}> (assuming they have one). =head2 Plugins with plugins Plugins that themselves have plugins (I) will need to invoke Plugins themselves. If the overall program uses L, then doing this in a C method is reccomended. In other contexts this may need to be done in C. sub new { my ($pkg, $pconfig, %args) = @_; return bless { context => $pconfig->{context}, api => $pconfig->{api}, config => \%args }, $pkg; } sub preconfig { my ($self, $configfile) = @_; my $config = $self->{config}{configfile} || $configfile; $self->{plugins} = new Plugins %{$self->{context}; $self->{plugins}->readconfig($config, self => $self); $self->{plugins}->initialize(); $self->{plugins}->invoke('preconfig', $config); } It is expected that whichever subclass of Plugins is used by the I should also be used by the child. The following snippet will supress this behavior. This is probably a bad idea unless the I uses a different configuration file than then I. local($self->{context}{pkg_override}{$config}); $self->{plugins} = new Plugins context => $self->{context}; $self->{plugins}->readconfig($config, self => $self); =head1 SUBCLASSING If you don't want to use an existing configuration file format, you don't have to. Subclass Plugins and override a couple of methods to change the behavior. In addition to the methods defined above in the L and in L that probably shoudn't be changed, Plugins defines the following methods: =over 4 =item parseconfig($configfile, %args) This is the method that actually parses a config file. This must be overridden as the default just calls C. The parameters come from either a direct call from the I or are passed through unchanged from C. =item post_initialize($context, $plugin) Called by C after new'ing up an instance. The default behavior is to do nothing. The C<$context> is the same context that is passwed to C and C and C. =item genkey($context) This is called to generate a unique identifier for each plugin instance. Duplicate identifiers will result in a C. The default C combines the configuration file name with the plugin package name and thus does not allow multiple instances of the same package. =item $pkg = file_plugin($file, %opts) This will look for C<$file> and then auto-wrap it with a package declaration to turn into a plugin perl module. The auto-generated package name is returned. C<%opts> can be: =over 4 =item search_path If C<$file> isn't an absolute path, search for it in C<@{$opts{search_path}}>. No search is done if unspecified. =item referenced Where was C<$file> referenced from? (eg: "/some/config-file, line 33") Used in error messages only. =item prefile Code to insert in the eval just before the file's contents. =item postfile Code to insert in the eval just after the file's contents. =item isa The value for the generated plugin's C<@ISA>. Defaults to C. =back =item registerplugin(%context) This add a plugin to a configuration. This is used after C and before C. This will C the plugin unless the symbol table for the plugin already exists. This method should not be overridden. =item addplugin(%context) This is used to bypass the entire C, C, C process. The plugin will be registered and initialized all at once. This can be used after C to add additional plugins. This will C the plugin unless the symbol table for the plugin already exists. Duplicate registrations of the same plugin will replace the old plugin (C will be called). This method should not be overridden. The C<%context> is the same as for C<%registerplugin()> =item pkg_invoke($pkg, [$method, @args]) This method will C a plugin and (optionally) call a class method. This method should not be overridden. =item initialize_plugin($self, $context) This is used by C and C to create plugin instances. It calls C and then C. This method should not be overridden. =back =head2 %context for addplugin() and registerplugin() Most of the C<%context> has that is given to C and C should (in theory) be passed as C<$context> to the plugin's C and then as C $context> to C if the is a I. The part that is not passed to C is the part that defines the I plugin: =over 4 =item pkg =E 'Some::Plugin::Module' The perl package name for the plugin. =item new_args =E [@args] The C<@args> will be passed to the the plugin's C: new $pkg (\%context, @args) =item requestor =E 'The::Parent::Module' The perl package name of the I module. This will be found with C if it isn't supplied. =item configfile =E "/some/file" Which configuration file referenced the plugin and caused it to be loaded. =item lineno =E 38 The of where the plugin was registered in C<$configfile>. Used for error messages. =back The rest will be passed to I C by way of C<$context>: =over 4 =item pkg_override =E "Plugins::Subclass" Plugins has been subclassed, this the name of the subclass. Subclassing should usually be inherited by I and this is the mechenism that makes it happen. =back =head2 Re-parsing the same configuration file The tricky part of subclassing Plugins is that if a configruation file is shared between I and I the C method will be invoked on the same file more than once. Store extra things in C<%context> to work around this issue. =head1 SEE ALSO L L =head1 THANK THE AUTHOR If you find this module useful and wish to show your appreciation to the author, please give me a Request-For-Quote on your next high-speed internet pipe order. I have good pricing for T1s, T3s, OC3s etc. =head1 LICENSE Copyright (C) 2006, David Muir Sharnoff . This module may be used and redistributed on the same terms as Perl itself.