Commit | Line | Data |
85cd2780 |
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 | |
2bdaa93e |
15 | has '_hints' => ( |
16 | isa => "ArrayRef", |
17 | is => "rw", |
18 | predicate => '_has_hints', |
19 | ); |
20 | |
85cd2780 |
21 | around 'mangle_line' => sub { |
22 | my $orig = shift; |
23 | my ($self, @rest) = @_; |
24 | my $line = $self->$orig(@rest); |
25 | my $lp = $self->lexical_environment; |
4d33251a |
26 | # Collate my declarations for all LP context vars then add ''; |
27 | # so an empty statement doesn't return anything (with a no warnings |
28 | # to prevent "Useless use ..." warning) |
2bdaa93e |
29 | return join('', |
30 | 'BEGIN { if ( $_REPL->_has_hints ) { ( $^H, %^H ) = @{ $_REPL->_hints } } }', |
31 | ( map { "my $_;\n" } keys %{$lp->get_context('_')} ), |
32 | qq{{ no warnings 'void'; ''; }\n}, |
33 | $line, |
34 | '; BEGIN { $_REPL->_hints([ $^H, %^H ]) }', |
35 | ); |
85cd2780 |
36 | }; |
37 | |
38 | around 'execute' => sub { |
39 | my $orig = shift; |
40 | my ($self, $to_exec, @rest) = @_; |
41 | my $wrapped = $self->lexical_environment->wrap($to_exec); |
42 | return $self->$orig($wrapped, @rest); |
43 | }; |
44 | |
45 | 1; |