r80567@dhcp117: nothingmuch | 2008-05-14 19:51:22 +0900
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / LexEnv.pm
CommitLineData
85cd2780 1package Devel::REPL::Plugin::LexEnv;
2
3use Moose::Role;
4use namespace::clean -except => [ 'meta' ];
5use Lexical::Persistence;
6
7has 'lexical_environment' => (
8 isa => 'Lexical::Persistence',
9 is => 'rw',
10 required => 1,
11 lazy => 1,
12 default => sub { Lexical::Persistence->new }
13);
14
2bdaa93e 15has '_hints' => (
16 isa => "ArrayRef",
17 is => "rw",
18 predicate => '_has_hints',
19);
20
85cd2780 21around '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
38around '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
451;