3 package Devel::REPL::Plugin::LexEnv;
4 # ABSTRACT: Provide a lexical environment for the REPL
6 our $VERSION = '1.003028';
8 use Devel::REPL::Plugin;
9 use namespace::autoclean;
10 use Lexical::Persistence;
14 $self->load_plugin('FindVariable');
17 has 'lexical_environment' => (
18 isa => 'Lexical::Persistence',
21 default => sub { Lexical::Persistence->new }
27 predicate => '_has_hints',
30 around 'mangle_line' => sub {
32 my ($self, @rest) = @_;
33 my $line = $self->$orig(@rest);
34 my $lp = $self->lexical_environment;
35 # Collate my declarations for all LP context vars then add '';
36 # so an empty statement doesn't return anything (with a no warnings
37 # to prevent "Useless use ..." warning)
39 'BEGIN { if ( $_REPL->_has_hints ) { ( $^H, %^H ) = @{ $_REPL->_hints } } }',
40 ( map { "my $_;\n" } keys %{$lp->get_context('_')} ),
41 qq{{ no warnings 'void'; ''; }\n},
43 '; BEGIN { $_REPL->_hints([ $^H, %^H ]) }',
47 around 'execute' => sub {
49 my ($self, $to_exec, @rest) = @_;
50 my $wrapped = $self->lexical_environment->wrap($to_exec);
51 return $self->$orig($wrapped, @rest);
54 # this doesn't work! yarg. we now just check $self->can('lexical_environment')
57 #around 'find_variable' => sub {
59 # my ($self, $name) = @_;
61 # return \( $self->lexical_environment->get_context('_')->{$name} )
62 # if exists $self->lexical_environment->get_context('_')->{$name};
77 my $repl = Devel::REPL->new;
78 $repl->load_plugin('LexEnv');
80 $repl->lexical_environment->do(<<'CODEZ');
82 use lib "$FindBin::Bin/../lib";
84 my $s = MyApp::Schema->connect('dbi:Pg:dbname=foo','broseph','elided');
89 # after you run re.pl:
90 $ warn $s->resultset('User')->first->first_name # <-- note that $s works