plugin metaclass, profiles, commands plugi
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / LexEnv.pm
1 package Devel::REPL::Plugin::LexEnv;
2
3 use Moose::Role;
4 use namespace::clean -except => [ 'meta' ];
5 use Lexical::Persistence;
6
7 has 'lexical_environment' => (
8   isa => 'Lexical::Persistence',
9   is => 'rw',
10   required => 1,
11   lazy => 1,
12   default => sub { Lexical::Persistence->new }
13 );
14
15 around 'mangle_line' => sub {
16   my $orig = shift;
17   my ($self, @rest) = @_;
18   my $line = $self->$orig(@rest);
19   my $lp = $self->lexical_environment;
20   # Collate my declarations for all LP context vars then add '';
21   # so an empty statement doesn't return anything (with a no warnings
22   # to prevent "Useless use ..." warning)
23   return join('', map { "my $_;\n" } keys %{$lp->get_context('_')})
24            .qq{{ no warnings 'void'; ''; }\n}.$line;
25 };
26
27 around 'execute' => sub {
28   my $orig = shift;
29   my ($self, $to_exec, @rest) = @_;
30   my $wrapped = $self->lexical_environment->wrap($to_exec);
31   return $self->$orig($wrapped, @rest);
32 };
33
34 1;