summaryrefslogtreecommitdiffstats
path: root/lib/Reply/Plugin/LexicalPersistence.pm
blob: 3b1f3f5c31a1d10a43f6652fa24b8be65c13475d (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package Reply::Plugin::LexicalPersistence;
use strict;
use warnings;
# ABSTRACT: persists lexical variables between lines

use base 'Reply::Plugin';

use Lexical::Persistence;

=head1 SYNOPSIS

  ; .replyrc
  [LexicalPersistence]

=head1 DESCRIPTION

This plugin persists the values of lexical variables between input lines. For
instance, with this plugin you can enter C<my $x = 2> into the Reply shell, and
then use C<$x> as expected in subsequent lines.

=cut

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);
    $code = $self->_fixup_code($code, \%c);
    return $self->{env}->wrap($code);
}

# XXX this is maybe a bug in Lexical::Persistence - it clears variables that
# aren't in its context, regardless of if they may have been set elsewhere
sub _fixup_code {
    my $self = shift;
    my ($code, $context) = @_;

    require PadWalker;
    require Devel::LexAlias;

    my $pad = PadWalker::peek_sub($code);
    my %restore;
    for my $var (keys %$pad) {
        next unless $var =~ /^\$\@\%./;
        next if exists $context->{$var};
        $restore{$var} = $pad->{$var};
    }

    $self->{code} = $code;

    return sub {
        my $code = shift;
        for my $var (keys %restore) {
            Devel::LexAlias::lexalias($code, $var, $restore{$var});
        }
        $code->(@_);
    };
}

# XXX can't just close over $code, because it will also be cleared by the same
# bug! we have to pass it as a parameter instead
sub execute {
    my $self = shift;
    my ($next, @args) = @_;

    $next->(delete $self->{code}, @args);
}

1;