From: matthewt Date: Wed, 25 Apr 2007 01:29:07 +0000 (+0000) Subject: add LexEnv plugin, rename $REPL to $_REPL to avoid clash with Lexical::Persistence X-Git-Tag: v1.003015~169 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=p5sagit%2FDevel-REPL.git;a=commitdiff_plain;h=85cd27808a3e9294c1b5345fb5bbca046821933d add LexEnv plugin, rename $REPL to $_REPL to avoid clash with Lexical::Persistence git-svn-id: http://dev.catalyst.perl.org/repos/bast/trunk/Devel-REPL@3209 bd8105ee-0ff8-0310-8827-fb3f25b6796d --- diff --git a/lib/Devel/REPL.pm b/lib/Devel/REPL.pm index c52f0bf..b096f9b 100644 --- a/lib/Devel/REPL.pm +++ b/lib/Devel/REPL.pm @@ -51,9 +51,9 @@ sub eval { } sub compile { - my $REPL = shift; - my $compiled = eval $REPL->wrap_as_sub($_[0]); - return (undef, $REPL->error_return("Compile error", $@)) if $@; + my $_REPL = shift; + my $compiled = eval $_REPL->wrap_as_sub($_[0]); + return (undef, $_REPL->error_return("Compile error", $@)) if $@; return $compiled; } diff --git a/lib/Devel/REPL/Plugin/LexEnv.pm b/lib/Devel/REPL/Plugin/LexEnv.pm new file mode 100644 index 0000000..f78fb01 --- /dev/null +++ b/lib/Devel/REPL/Plugin/LexEnv.pm @@ -0,0 +1,30 @@ +package Devel::REPL::Plugin::LexEnv; + +use Moose::Role; +use namespace::clean -except => [ 'meta' ]; +use Lexical::Persistence; + +has 'lexical_environment' => ( + isa => 'Lexical::Persistence', + is => 'rw', + required => 1, + lazy => 1, + default => sub { Lexical::Persistence->new } +); + +around 'mangle_line' => sub { + my $orig = shift; + my ($self, @rest) = @_; + my $line = $self->$orig(@rest); + my $lp = $self->lexical_environment; + return join('', map { "my $_;\n" } keys %{$lp->get_context('_')}).$line; +}; + +around 'execute' => sub { + my $orig = shift; + my ($self, $to_exec, @rest) = @_; + my $wrapped = $self->lexical_environment->wrap($to_exec); + return $self->$orig($wrapped, @rest); +}; + +1; diff --git a/lib/Devel/REPL/Plugin/Turtles.pm b/lib/Devel/REPL/Plugin/Turtles.pm index 5faafc6..ca16591 100644 --- a/lib/Devel/REPL/Plugin/Turtles.pm +++ b/lib/Devel/REPL/Plugin/Turtles.pm @@ -6,7 +6,7 @@ around 'eval' => sub { my $next = shift; my ($self, $line) = @_; if ($line =~ /^#(.*)/) { - return $next->($self, ('$REPL->' . $1 . '; return();')); + return $next->($self, ('$_REPL->' . $1 . '; return();')); } else { return $next->($self, $line);