I want to write a app that can be extended via plugins, using Perl and Moose. I know there are a few Moose modules for writing plugins and I know there are other ways.
What are my options? and what should I know about them? any thing I should think about before implementing a plugin system?
There are a few ways to provide extensibility; allow the user to apply roles to your class, or allow them to pass in small objects that do the interesting things (delegates). Delegates perform better than roles, but will require that you plan for all the extensibility up front. Roles allow more ad-hoc behaviors.
Here are two CPAN distributions that take each approach:
Delegates: AnyEvent::Subprocess
Roles: Devel::REPL
Plugin roles are implemented with MooseX::Object::Pluggable.
Delegates are implemented however you like; the pattern is passing an instance of a class A that does some role R to class C, and then class C delegates to A. Here's an example:
Then you use this like:
If you want the way Sort works to change, you just create a new class that does the
Compare
role, and then pass an instance to Sort's constructor. Instant flexibility!