add LexEnv plugin, rename $REPL to $_REPL to avoid clash with Lexical::Persistence
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / LexEnv.pm
CommitLineData
85cd2780 1package Devel::REPL::Plugin::LexEnv;
2
3use Moose::Role;
4use namespace::clean -except => [ 'meta' ];
5use Lexical::Persistence;
6
7has 'lexical_environment' => (
8 isa => 'Lexical::Persistence',
9 is => 'rw',
10 required => 1,
11 lazy => 1,
12 default => sub { Lexical::Persistence->new }
13);
14
15around 'mangle_line' => sub {
16 my $orig = shift;
17 my ($self, @rest) = @_;
18 my $line = $self->$orig(@rest);
19 my $lp = $self->lexical_environment;
20 return join('', map { "my $_;\n" } keys %{$lp->get_context('_')}).$line;
21};
22
23around 'execute' => sub {
24 my $orig = shift;
25 my ($self, $to_exec, @rest) = @_;
26 my $wrapped = $self->lexical_environment->wrap($to_exec);
27 return $self->$orig($wrapped, @rest);
28};
29
301;