summaryrefslogtreecommitdiffstats
path: root/lib/App/REPL.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/App/REPL.pm')
-rw-r--r--lib/App/REPL.pm56
1 files changed, 47 insertions, 9 deletions
diff --git a/lib/App/REPL.pm b/lib/App/REPL.pm
index 0ec1e63..f9262fb 100644
--- a/lib/App/REPL.pm
+++ b/lib/App/REPL.pm
@@ -2,16 +2,22 @@ package App::REPL;
use strict;
use warnings;
+use App::REPL::Plugin::Defaults;
+
sub new {
- bless {}, shift;
+ bless {
+ plugins => [
+ App::REPL::Plugin::Defaults->new,
+ ]
+ }, shift;
}
sub run {
my $self = shift;
while (defined(my $line = $self->_read)) {
- my $result = $self->_eval($line);
- $self->_print($result);
+ my @result = $self->_eval($line);
+ $self->_print(@result);
}
print "\n";
}
@@ -19,23 +25,55 @@ sub run {
sub _read {
my $self = shift;
- print "> ";
- return <>;
+ $self->_wrapped_plugin('display_prompt');
+ my ($line) = $self->_wrapped_plugin('read_line');
+ ($line) = $self->_chained_plugin('munge_line', $line);
+
+ return $line;
}
sub _eval {
my $self = shift;
my ($line) = @_;
- return eval $line;
+ return $self->_wrapped_plugin('evaluate', $line);
}
sub _print {
my $self = shift;
- my ($result) = @_;
+ my (@result) = @_;
+
+ @result = $self->_chained_plugin('munge_result', @result);
+ $self->_wrapped_plugin('print_result', @result);
+}
+
+sub _wrapped_plugin {
+ my $self = shift;
+ my $plugins = ref($_[0]) ? pop : $self->{plugins};
+ my ($method, @args) = @_;
+
+ $plugins = [ grep { $_->can($method) } @$plugins ];
+
+ return @args unless @$plugins;
+
+ my $plugin = shift @$plugins;
+ my $next = sub { $self->_wrapped_plugin($plugins, @_) };
+
+ return $plugin->$method($next, @args);
+}
+
+sub _chained_plugin {
+ my $self = shift;
+ my $plugins = ref($_[0]) ? pop : $self->{plugins};
+ my ($method, @args) = @_;
+
+ $plugins = [ grep { $_->can($method) } @$plugins ];
+
+ for my $plugin (@$plugins) {
+ @args = $plugin->$method(@args);
+ }
- print $result, "\n"
- if defined $result;
+ return @args;
}
1;