From 16ab48377311331748fe7b6e109d31822200968e Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 29 May 2013 01:03:24 -0500 Subject: plugins --- lib/App/REPL.pm | 56 ++++++++++++++++++++++++++++++++++------- lib/App/REPL/Plugin.pm | 7 ++++++ lib/App/REPL/Plugin/Defaults.pm | 34 +++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 lib/App/REPL/Plugin.pm create mode 100644 lib/App/REPL/Plugin/Defaults.pm (limited to 'lib') 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; -- cgit v1.2.3-54-g00ecf