Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

perl - What is the best option for building a plugin system for a Moose application?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

package Compare;
use Moose::Role;
requires 'compare';


package SpaceshipCompare;
use Moose;
with 'Compare';  

sub compare { my ($a, $b) = @_; return $a <=> $b }

package Sort;
use Moose;

has 'comparer' => (
    is       => 'ro',
    does     => 'Compare',
    handles  => 'Compare',
    required => 1,
);

sub my_sort {
    my ($self, @list) = @_;
    return sort { $self->compare($a, $b) } @list;
}

Then you use this like:

my $sorter = Sort->new( comparer => SpaceshipCompare->new );
my @sorted = $sorter->my_sort("1one", "0", "43");

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...