1 package Devel::REPL::Plugin::LexEnv;
3 use Devel::REPL::Plugin;
4 use namespace::clean -except => [ 'meta' ];
5 use Lexical::Persistence;
7 with 'Devel::REPL::Plugin::FindVariable';
9 has 'lexical_environment' => (
10 isa => 'Lexical::Persistence',
14 default => sub { Lexical::Persistence->new }
20 predicate => '_has_hints',
23 around 'mangle_line' => sub {
25 my ($self, @rest) = @_;
26 my $line = $self->$orig(@rest);
27 my $lp = $self->lexical_environment;
28 # Collate my declarations for all LP context vars then add '';
29 # so an empty statement doesn't return anything (with a no warnings
30 # to prevent "Useless use ..." warning)
32 'BEGIN { if ( $_REPL->_has_hints ) { ( $^H, %^H ) = @{ $_REPL->_hints } } }',
33 ( map { "my $_;\n" } keys %{$lp->get_context('_')} ),
34 qq{{ no warnings 'void'; ''; }\n},
36 '; BEGIN { $_REPL->_hints([ $^H, %^H ]) }',
40 around 'execute' => sub {
42 my ($self, $to_exec, @rest) = @_;
43 my $wrapped = $self->lexical_environment->wrap($to_exec);
44 return $self->$orig($wrapped, @rest);
47 # this doesn't work! yarg. we now just check $self->can('lexical_environment')
50 #around 'find_variable' => sub {
52 # my ($self, $name) = @_;
54 # return \( $self->lexical_environment->get_context('_')->{$name} )
55 # if exists $self->lexical_environment->get_context('_')->{$name};
66 Devel::REPL::Plugin::LexEnv - Provide a lexical environment for the REPL