Simple pluggable class.
use Class::Pluggable; use base qw(Class::Pluggable);
# Some::Plugin::Module has sub routin called newAction add_plugin("Some::Plugin::Module");
newAction(); # Plugged action.
This class makes your class (sub class of Class::Pluggable) pluggable. In this documentatin, the word \*(L"pluggable\*(R" has two meanings.
One is just simply adding new method to your pluggable classs from other plugin modules. So, after you plugged some modules to your class, you can use there method exactly same as your own object method.
You can see this kind of plugin mechanism in CGI::Application and CGI::Application::Plugin::Session.
There are one thing that Plugin developer have to know. The plugin module \s-1MUST\s0 have @EXPORT_AS_PLUGIN to use this pluggable mechanism. This works almost same as @EXPORT. But the methods in the @EXPORT_AS_PLUGIN wouldn't be exported to your package. But it would be exported to the subclass of Class::Pluggable (only when you call add_plugin()).
And the another meaning of \*(L"pluggable\*(R" is so called hook-mechanism. For example, if you want to allow to other modules to do something before and/or after some action. You can do like this:
$self->execute_plugin_method($_, "before_action") foreach $self->get_plugins();
## do some your own action here.
$self->execute_plugin_method($_, "after_action") foreach $self->get_plugins();
Here are all methods of Class::Pluggable.
$object->add_plugin($pluginName) This will add new plugin to your class. What you added to here would be returned by get_plugins() method.
@plugins = $object->get_plugins(); It will return all of plugin names that are already added to YouClass.
$result = $object->execute_plugin_method("SomePlugin", "someMethod"); This will execute the method someMethod of SomePlugin.
$object->execute_all_plugin_method("someMethod"); This will execute the method someMethod of all plugin we have. This is almost same as following code. $self->execute_plugin_method($_, "someMethod") foreach $self->get_plugins(); The difference is executeAllPluginMethod can't return any values. But executePluginMethod can.
$object->add_hook("pre-init", "pre_init"); This will add new hook to your class. Whenever run_hook(\*(L"pre-init\*(R") has called, the method pre_init of all plugins which we have will be executed.
$object->run_hook("pre-init"); This will execute the hook-method of all plugins which we have.
$object->remove_hook("pre-init"); This will delete the hook from YourClass. After calling this method, you cannot call run_hook(\*(L"pre-init\*(R"). If you do, it will die immediately.
...
Ken Takeshige, <[email protected]>
Copyright (C) 2006 by Ken Takeshige This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.