From: matthewt Date: Tue, 10 Apr 2007 03:22:46 +0000 (+0000) Subject: switch Devel::REPL to sub {} generation and split functionality for maximum ease... X-Git-Tag: v1.003015~176 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=911a1c242561b001192b9541b3aa813fe0b70b41;p=p5sagit%2FDevel-REPL.git switch Devel::REPL to sub {} generation and split functionality for maximum ease of overriding git-svn-id: http://dev.catalyst.perl.org/repos/bast/trunk/Devel-REPL@3186 bd8105ee-0ff8-0310-8827-fb3f25b6796d --- diff --git a/lib/Devel/REPL.pm b/lib/Devel/REPL.pm index 3a6db51..89c3a8b 100644 --- a/lib/Devel/REPL.pm +++ b/lib/Devel/REPL.pm @@ -32,7 +32,7 @@ sub run_once { my ($self) = @_; my $line = $self->read; return unless defined($line); # undefined value == EOF - my @ret = $self->execute($line); + my @ret = $self->eval($line); $self->print(@ret); return 1; } @@ -42,13 +42,43 @@ sub read { return $self->term->readline($self->prompt); } +sub eval { + my ($self, $line) = @_; + my ($to_exec, @rest) = $self->compile($line); + return @rest unless defined($to_exec); + my @ret = $self->execute($to_exec); + return @ret; +} + +sub compile { + my ($self, $line) = @_; + my $compiled = eval $self->wrap_as_sub($line); + return (undef, $self->error_return("Compile error", $@)) if $@; + return $compiled; +} + +sub wrap_as_sub { + my ($self, $line) = @_; + return qq!sub {\n!.$self->mangle_line($line).qq!\n}\n!; +} + +sub mangle_line { + my ($self, $line) = @_; + return $line; +} + sub execute { - my ($self, $to_exec) = @_; - my @ret = eval $to_exec; - @ret = ("ERROR: $@") if $@; + my ($self, $to_exec, @args) = @_; + my @ret = eval { $to_exec->(@args) }; + return $self->error_return("Runtime error", $@) if $@; return @ret; } +sub error_return { + my ($self, $type, $error) = @_; + return "${type}: ${error}"; +} + sub print { my ($self, @ret) = @_; my $fh = $self->out_fh;