1 package Devel::REPL::Plugin::LexEnv;
3 use Devel::REPL::Plugin;
4 use namespace::autoclean;
5 use Lexical::Persistence;
9 $self->load_plugin('FindVariable');
12 has 'lexical_environment' => (
13 isa => 'Lexical::Persistence',
17 default => sub { Lexical::Persistence->new }
23 predicate => '_has_hints',
26 around 'mangle_line' => sub {
28 my ($self, @rest) = @_;
29 my $line = $self->$orig(@rest);
30 my $lp = $self->lexical_environment;
31 # Collate my declarations for all LP context vars then add '';
32 # so an empty statement doesn't return anything (with a no warnings
33 # to prevent "Useless use ..." warning)
35 'BEGIN { if ( $_REPL->_has_hints ) { ( $^H, %^H ) = @{ $_REPL->_hints } } }',
36 ( map { "my $_;\n" } keys %{$lp->get_context('_')} ),
37 qq{{ no warnings 'void'; ''; }\n},
39 '; BEGIN { $_REPL->_hints([ $^H, %^H ]) }',
43 around 'execute' => sub {
45 my ($self, $to_exec, @rest) = @_;
46 my $wrapped = $self->lexical_environment->wrap($to_exec);
47 return $self->$orig($wrapped, @rest);
50 # this doesn't work! yarg. we now just check $self->can('lexical_environment')
53 #around 'find_variable' => sub {
55 # my ($self, $name) = @_;
57 # return \( $self->lexical_environment->get_context('_')->{$name} )
58 # if exists $self->lexical_environment->get_context('_')->{$name};
69 Devel::REPL::Plugin::LexEnv - Provide a lexical environment for the REPL
75 my $repl = Devel::REPL->new;
76 $repl->load_plugin('LexEnv');
78 $repl->lexical_environment->do(<<'CODEZ');
80 use lib "$FindBin::Bin/../lib";
82 my $s = MyApp::Schema->connect('dbi:Pg:dbname=foo','broseph','elided');
87 # after you run re.pl:
88 $ warn $s->resultset('User')->first->first_name # <-- note that $s works