From: matthewt Date: Mon, 9 Apr 2007 17:20:39 +0000 (+0000) Subject: initial Devel::REPL implementation X-Git-Tag: v1.003015~177 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=afe61f9c1991af3ae964bd2e34056a0b4e50ff13;p=p5sagit%2FDevel-REPL.git initial Devel::REPL implementation git-svn-id: http://dev.catalyst.perl.org/repos/bast/trunk/Devel-REPL@3184 bd8105ee-0ff8-0310-8827-fb3f25b6796d --- afe61f9c1991af3ae964bd2e34056a0b4e50ff13 diff --git a/lib/Devel/REPL.pm b/lib/Devel/REPL.pm new file mode 100644 index 0000000..3a6db51 --- /dev/null +++ b/lib/Devel/REPL.pm @@ -0,0 +1,58 @@ +package Devel::REPL; + +use Term::ReadLine; +use Moose; +use namespace::clean; + +with 'MooseX::Object::Pluggable'; + +has 'term' => ( + is => 'rw', required => 1, + default => sub { Term::ReadLine->new('Perl REPL') } +); + +has 'prompt' => ( + is => 'rw', required => 1, + default => sub { '$ ' } +); + +has 'out_fh' => ( + is => 'rw', required => 1, lazy => 1, + default => sub { shift->term->OUT || \*STDOUT; } +); + +sub run { + my ($self) = @_; + while ($self->run_once) { + # keep looping + } +} + +sub run_once { + my ($self) = @_; + my $line = $self->read; + return unless defined($line); # undefined value == EOF + my @ret = $self->execute($line); + $self->print(@ret); + return 1; +} + +sub read { + my ($self) = @_; + return $self->term->readline($self->prompt); +} + +sub execute { + my ($self, $to_exec) = @_; + my @ret = eval $to_exec; + @ret = ("ERROR: $@") if $@; + return @ret; +} + +sub print { + my ($self, @ret) = @_; + my $fh = $self->out_fh; + print $fh "@ret"; +} + +1;