summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2013-05-29 01:03:24 -0500
committerJesse Luehrs <doy@tozt.net>2013-05-29 01:03:24 -0500
commit16ab48377311331748fe7b6e109d31822200968e (patch)
treed08189f37319fcea50ec18ca2aa0d5c3c29e88a4 /lib
parentf57cbd571049fcff4656a8c8b4f1ea6344ef4626 (diff)
downloadreply-16ab48377311331748fe7b6e109d31822200968e.tar.gz
reply-16ab48377311331748fe7b6e109d31822200968e.zip
plugins
Diffstat (limited to 'lib')
-rw-r--r--lib/App/REPL.pm56
-rw-r--r--lib/App/REPL/Plugin.pm7
-rw-r--r--lib/App/REPL/Plugin/Defaults.pm34
3 files changed, 88 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;
diff --git a/lib/App/REPL/Plugin.pm b/lib/App/REPL/Plugin.pm
new file mode 100644
index 0000000..a255f62
--- /dev/null
+++ b/lib/App/REPL/Plugin.pm
@@ -0,0 +1,7 @@
+package App::REPL::Plugin;
+use strict;
+use warnings;
+
+sub new { bless {}, shift }
+
+1;
diff --git a/lib/App/REPL/Plugin/Defaults.pm b/lib/App/REPL/Plugin/Defaults.pm
new file mode 100644
index 0000000..f2b4705
--- /dev/null
+++ b/lib/App/REPL/Plugin/Defaults.pm
@@ -0,0 +1,34 @@
+package App::REPL::Plugin::Defaults;
+use strict;
+use warnings;
+
+use base 'App::REPL::Plugin';
+
+sub display_prompt {
+ my $self = shift;
+
+ print "> ";
+}
+
+sub read_line {
+ my $self = shift;
+
+ return scalar <>;
+}
+
+sub evaluate {
+ my $self = shift;
+ my ($next, $line) = @_;
+
+ return eval $line;
+}
+
+sub print_result {
+ my $self = shift;
+ my ($next, $result) = @_;
+
+ print $result, "\n"
+ if defined $result;
+}
+
+1;