3 package Devel::REPL::Plugin::LexEnv;
5 our $VERSION = '1.003027';
7 use Devel::REPL::Plugin;
8 use namespace::autoclean;
9 use Lexical::Persistence;
13 $self->load_plugin('FindVariable');
16 has 'lexical_environment' => (
17 isa => 'Lexical::Persistence',
20 default => sub { Lexical::Persistence->new }
26 predicate => '_has_hints',
29 around 'mangle_line' => sub {
31 my ($self, @rest) = @_;
32 my $line = $self->$orig(@rest);
33 my $lp = $self->lexical_environment;
34 # Collate my declarations for all LP context vars then add '';
35 # so an empty statement doesn't return anything (with a no warnings
36 # to prevent "Useless use ..." warning)
38 'BEGIN { if ( $_REPL->_has_hints ) { ( $^H, %^H ) = @{ $_REPL->_hints } } }',
39 ( map { "my $_;\n" } keys %{$lp->get_context('_')} ),
40 qq{{ no warnings 'void'; ''; }\n},
42 '; BEGIN { $_REPL->_hints([ $^H, %^H ]) }',
46 around 'execute' => sub {
48 my ($self, $to_exec, @rest) = @_;
49 my $wrapped = $self->lexical_environment->wrap($to_exec);
50 return $self->$orig($wrapped, @rest);
53 # this doesn't work! yarg. we now just check $self->can('lexical_environment')
56 #around 'find_variable' => sub {
58 # my ($self, $name) = @_;
60 # return \( $self->lexical_environment->get_context('_')->{$name} )
61 # if exists $self->lexical_environment->get_context('_')->{$name};
72 Devel::REPL::Plugin::LexEnv - Provide a lexical environment for the REPL
78 my $repl = Devel::REPL->new;
79 $repl->load_plugin('LexEnv');
81 $repl->lexical_environment->do(<<'CODEZ');
83 use lib "$FindBin::Bin/../lib";
85 my $s = MyApp::Schema->connect('dbi:Pg:dbname=foo','broseph','elided');
90 # after you run re.pl:
91 $ warn $s->resultset('User')->first->first_name # <-- note that $s works