r61342@onn: sartak | 2008-05-31 12:21:47 -0400
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / LexEnv.pm
CommitLineData
85cd2780 1package Devel::REPL::Plugin::LexEnv;
2
6a5409bc 3use Devel::REPL::Plugin;
85cd2780 4use namespace::clean -except => [ 'meta' ];
5use Lexical::Persistence;
6
bd67131f 7with 'Devel::REPL::Plugin::FindVariable';
8
85cd2780 9has 'lexical_environment' => (
10 isa => 'Lexical::Persistence',
11 is => 'rw',
12 required => 1,
13 lazy => 1,
14 default => sub { Lexical::Persistence->new }
15);
16
2bdaa93e 17has '_hints' => (
18 isa => "ArrayRef",
19 is => "rw",
20 predicate => '_has_hints',
21);
22
85cd2780 23around '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
40around '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
6d22063d 47# this doesn't work! yarg. we now just check $self->can('lexical_environment')
48# in FindVariable
49
50#around 'find_variable' => sub {
51# my $orig = shift;
52# my ($self, $name) = @_;
53#
54# return \( $self->lexical_environment->get_context('_')->{$name} )
55# if exists $self->lexical_environment->get_context('_')->{$name};
56#
57# return $orig->(@_);
58#};
bd67131f 59
85cd2780 601;
cfd1094b 61
62__END__
63
64=head1 NAME
65
66Devel::REPL::Plugin::LexEnv - Provide a lexical environment for the REPL
67
68=cut
69