summaryrefslogtreecommitdiffstats
path: root/lib/App/REPL/Plugin/LexicalPersistence.pm
blob: 986fa0393be5256fe55feae3c156ee016b98c658 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package App::REPL::Plugin::LexicalPersistence;
use strict;
use warnings;

use base 'App::REPL::Plugin';

use Lexical::Persistence;

sub new {
    my $class = shift;
    my $self = $class->SUPER::new(@_);
    $self->{env} = Lexical::Persistence->new;
    return $self;
}

sub compile {
    my $self = shift;
    my ($next, $line, %args) = @_;

    my %c = %{ $self->{env}->get_context('_') };

    $args{environment} ||= {};
    $args{environment} = {
        %{ $args{environment} },
        (map { $_ => ref($c{$_}) ? $c{$_} : \$c{$_} } keys %c),
    };
    my ($code) = $next->($line, %args);
    return $self->{env}->wrap($code);
}

1;