4

我是Moose的新手。我必须创建一个应该加载多个插件的对象。结构是这样的:

  • 主对象 -> 一些通用函数
  • 插件 -> 主对象的扩展

插件位于服务器上的单独文件夹中。主对象必须加载插件,初始化它们并将对象存储在自身中。每个插件的返回值都必须经过主对象。因为主对象应该为调用者转换 JSON 结构中的每个返回值。

我会这样称呼:

my $main_obj = Main->new();
$main_obj->plugin('MainExtention')->get_title();

这是我的示例代码:

主要对象:

package Main;
use Moose;

has 'plugins' => (
    is          => 'rw',
);

has 'title' => (
    is          => 'rw',
    isa         => 'Str',
    reader  => '_get_title',
);

# load plugins
sub _load_modules {
  my $self = shift;

    my $path = "/usr/local/apache/sites/.../Plugins";
  push(@INC, $path);

  my @modules = _find_modules_to_load($path);

  eval { 
        my $plugins = {};
        foreach my $module ( sort @modules) {
            (my $file = $module) =~ s|::|/|g;
            (my $modname = $module) =~ s/.*?(\w+$)/$1/;
            require $file . '.pm';
            my $obj = $module->new();
            $plugins->{$modname} = $obj;
            1;
        }
        $self->plugins($plugins);
  } or do {
      my $error = $@;
      warn "Error loading modules: $error" if $error;
  };

}

# read plugins
sub _find_modules_to_load {
    my ($dir) = @_;
    my @files = glob("$dir/*.pm");

    my $namespace = $dir;
    $namespace =~ s/\//::/g;

    # Get the leaf name and add the System::Module namespace to it
    my @modules = map { s/.*\/(.*).pm//g;  "${namespace}::$1"; } @files;

    return @modules;
}

sub BUILD {
    my $self = shift;
    $self->_load_modules();
}

sub get_title {
    return 'main title'
}

1;
__PACKAGE__->meta->make_immutable;

目录“/usr/local/apache/sites/.../Plugins”中的插件 MainExtention:

package MainExtention;
use Moose;

has 'title' => (
    is          => 'rw',
    isa         => 'Str',
    default     => 'Default',
);

sub get_title {
    return 'MainExtention Title';
}

1;

这像这样工作:

my $main = Main->new();
my $plugins = $main->plugins();
print $plugins->{'MainExtention'}->get_title();

但这不是我将拥有的 :) 我将不是直接从插件而是从主对象获得插件的返回。有人有想法吗?第二个问题:有没有更简单的方法来加载插件?如何?

4

1 回答 1

1

要加载插件,我建议使用Module::Pluggable,它可以从目录加载一堆包并根据需要实例化它们(或不实例化它们)。

如果您需要让主对象包装插件,只需在主对象上定义一个方法来执行您需要的任何操作:

package Main;
use Moose;

# Adds a plugins() method to Main that returns a list of all loaded plugin packages
use Module::Pluggable search_dirs => [ "/usr/local/apache/sites/.../Plugins" ];

# Used to store the plugins after ->new is called on each package
has loaded_plugins => (
    is => 'rw',
    isa => 'HashRef[Object]',
    lazy_build => 1,
    traits => [ 'Hash' ],
    handles => { _plugin => 'get' },
);

# Constructor for loaded_plugins, implied by lazy_build => 1
sub _build_loaded_plugins {
    my ($self) = @_;

    my %loaded_plugins;
    for my $plugin ($self->plugins) {
        $loaded_plugins{$plugin} = $plugin->new;
    }

    return \%loaded_plugins;
}

# Method for getting the processed title from any plugin
sub get_plugin_title {
    my ($self, $name) = @_;

    my $plugin = $self->_plugin($name);

    my $title = $plugin->get_title;

    # process the title according to whatever Main needs to do...
    ...

    return $title;
}

然后,您的代码:

my $main = Main->new();
print $main->get_plugin_title('MainExtension');

如果您使用 Moose,我还建议您让所有插件实现一个角色,这可以帮助您发现插件是否已正确实现。

于 2012-08-15T17:33:53.747 回答