r61423@onn: sartak | 2008-06-02 16:00:33 -0400
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / LexEnv.pm
1 package Devel::REPL::Plugin::LexEnv;
2
3 use Devel::REPL::Plugin;
4 use namespace::clean -except => [ 'meta' ];
5 use Lexical::Persistence;
6
7 sub BEFORE_PLUGIN {
8     my $self = shift;
9     $self->load_plugin('FindVariable');
10 }
11
12 has 'lexical_environment' => (
13   isa => 'Lexical::Persistence',
14   is => 'rw',
15   required => 1,
16   lazy => 1,
17   default => sub { Lexical::Persistence->new }
18 );
19
20 has '_hints' => (
21   isa => "ArrayRef",
22   is => "rw",
23   predicate => '_has_hints',
24 );
25
26 around 'mangle_line' => sub {
27   my $orig = shift;
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)
34   return join('',
35     'BEGIN { if ( $_REPL->_has_hints ) { ( $^H, %^H ) = @{ $_REPL->_hints } } }',
36     ( map { "my $_;\n" } keys %{$lp->get_context('_')} ),
37     qq{{ no warnings 'void'; ''; }\n},
38     $line,
39     '; BEGIN { $_REPL->_hints([ $^H, %^H ]) }',
40   );
41 };
42
43 around 'execute' => sub {
44   my $orig = shift;
45   my ($self, $to_exec, @rest) = @_;
46   my $wrapped = $self->lexical_environment->wrap($to_exec);
47   return $self->$orig($wrapped, @rest);
48 };
49
50 # this doesn't work! yarg. we now just check $self->can('lexical_environment')
51 # in FindVariable
52
53 #around 'find_variable' => sub {
54 #  my $orig = shift;
55 #  my ($self, $name) = @_;
56 #
57 #  return \( $self->lexical_environment->get_context('_')->{$name} )
58 #    if exists $self->lexical_environment->get_context('_')->{$name};
59 #
60 #  return $orig->(@_);
61 #};
62
63 1;
64
65 __END__
66
67 =head1 NAME
68
69 Devel::REPL::Plugin::LexEnv - Provide a lexical environment for the REPL
70
71 =cut
72