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 | |
bd67131f |
7 | with 'Devel::REPL::Plugin::FindVariable'; |
8 | |
85cd2780 |
9 | has 'lexical_environment' => ( |
10 | isa => 'Lexical::Persistence', |
11 | is => 'rw', |
12 | required => 1, |
13 | lazy => 1, |
14 | default => sub { Lexical::Persistence->new } |
15 | ); |
16 | |
2bdaa93e |
17 | has '_hints' => ( |
18 | isa => "ArrayRef", |
19 | is => "rw", |
20 | predicate => '_has_hints', |
21 | ); |
22 | |
85cd2780 |
23 | around 'mangle_line' => sub { |
24 | my $orig = shift; |
25 | my ($self, @rest) = @_; |
26 | my $line = $self->$orig(@rest); |
27 | my $lp = $self->lexical_environment; |
4d33251a |
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) |
2bdaa93e |
31 | return join('', |
32 | 'BEGIN { if ( $_REPL->_has_hints ) { ( $^H, %^H ) = @{ $_REPL->_hints } } }', |
33 | ( map { "my $_;\n" } keys %{$lp->get_context('_')} ), |
34 | qq{{ no warnings 'void'; ''; }\n}, |
35 | $line, |
36 | '; BEGIN { $_REPL->_hints([ $^H, %^H ]) }', |
37 | ); |
85cd2780 |
38 | }; |
39 | |
40 | around 'execute' => sub { |
41 | my $orig = shift; |
42 | my ($self, $to_exec, @rest) = @_; |
43 | my $wrapped = $self->lexical_environment->wrap($to_exec); |
44 | return $self->$orig($wrapped, @rest); |
45 | }; |
46 | |
bd67131f |
47 | around 'find_variable' => sub { |
48 | my $orig = shift; |
49 | my ($self, $name) = @_; |
b314b3ec |
50 | |
51 | return \( $self->lexical_environment->get_context('_')->{$name} ) |
52 | if exists $self->lexical_environment->get_context('_')->{$name}; |
53 | |
bd67131f |
54 | return $orig->(@_); |
55 | }; |
56 | |
85cd2780 |
57 | 1; |