diff options
author | Jesse Luehrs <doy@tozt.net> | 2013-05-29 01:20:26 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2013-05-29 01:25:22 -0500 |
commit | 3b4c8e20ecc690836ff7f7ea7889bbcd43b3d3e8 (patch) | |
tree | c004ea4004a9641dc1c14b4ae2e015284b34e14e | |
parent | 16ab48377311331748fe7b6e109d31822200968e (diff) | |
download | reply-3b4c8e20ecc690836ff7f7ea7889bbcd43b3d3e8.tar.gz reply-3b4c8e20ecc690836ff7f7ea7889bbcd43b3d3e8.zip |
plugin loading
-rw-r--r-- | lib/App/REPL.pm | 48 |
1 files changed, 38 insertions, 10 deletions
diff --git a/lib/App/REPL.pm b/lib/App/REPL.pm index f9262fb..6f74385 100644 --- a/lib/App/REPL.pm +++ b/lib/App/REPL.pm @@ -4,14 +4,42 @@ use warnings; use App::REPL::Plugin::Defaults; +use Module::Runtime qw(compose_module_name use_package_optimistically); +use Scalar::Util qw(blessed); + sub new { bless { - plugins => [ + plugins => [], + _default_plugins => [ App::REPL::Plugin::Defaults->new, - ] + ], }, shift; } +sub load_plugin { + my $self = shift; + my ($plugin) = @_; + + if (!blessed($plugin)) { + $plugin = compose_module_name("App::REPL::Plugin", $plugin); + use_package_optimistically($plugin); + die "$plugin is not a valid plugin" + unless $plugin->isa("App::REPL::Plugin"); + $plugin = $plugin->new; + } + + push @{ $self->{plugins} }, $plugin; +} + +sub plugins { + my $self = shift; + + return ( + @{ $self->{plugins} }, + @{ $self->{_default_plugins} }, + ); +} + sub run { my $self = shift; @@ -49,27 +77,27 @@ sub _print { sub _wrapped_plugin { my $self = shift; - my $plugins = ref($_[0]) ? pop : $self->{plugins}; + my @plugins = ref($_[0]) ? @{ shift() } : $self->plugins; my ($method, @args) = @_; - $plugins = [ grep { $_->can($method) } @$plugins ]; + @plugins = grep { $_->can($method) } @plugins; - return @args unless @$plugins; + return @args unless @plugins; - my $plugin = shift @$plugins; - my $next = sub { $self->_wrapped_plugin($plugins, @_) }; + my $plugin = shift @plugins; + my $next = sub { $self->_wrapped_plugin(\@plugins, $method, @_) }; return $plugin->$method($next, @args); } sub _chained_plugin { my $self = shift; - my $plugins = ref($_[0]) ? pop : $self->{plugins}; + my @plugins = ref($_[0]) ? @{ shift() } : $self->plugins; my ($method, @args) = @_; - $plugins = [ grep { $_->can($method) } @$plugins ]; + @plugins = grep { $_->can($method) } @plugins; - for my $plugin (@$plugins) { + for my $plugin (@plugins) { @args = $plugin->$method(@args); } |