summaryrefslogtreecommitdiffstats
path: root/lib/Reply/Plugin/Hints.pm
blob: a944e400019c4e5d739c01a1f1a46e23c7b83d93 (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
package main;

my $default_hints;
my $default_hinthash;
my $default_warning_bits;
BEGIN {
    $default_hints = $^H;
    $default_hinthash = { %^H };
    $default_warning_bits = ${^WARNING_BITS};
}

use strict;
use warnings;
# ABSTRACT: persists lexical hints across input lines

use mop;

=head1 SYNOPSIS

  ; .replyrc
  [Hints]

=head1 DESCRIPTION

This plugin persists the values of various compile time lexical hints between
evaluated lines. This means, for instance, that entering a line like C<use
strict> at the Reply prompt will cause C<strict> to be enabled for all future
lines (at least until C<no strict> is given).

=cut

class Reply::Plugin::Hints extends Reply::Plugin {
    has $hints        = $default_hints;
    has $hinthash     = $default_hinthash;
    has $warning_bits = $default_warning_bits;

    method mangle_line ($line) {
        my $package = __PACKAGE__;
        return <<LINE;
BEGIN {
    \$^H = \$${package}::HINTS;
    \%^H = \%\$${package}::HINTHASH;
    \${^WARNING_BITS} = \$${package}::WARNING_BITS;
}
$line
;
BEGIN {
    \$${package}::HINTS = \$^H;
    \$${package}::HINTHASH = \\\%^H;
    \$${package}::WARNING_BITS = \${^WARNING_BITS};
}
LINE
    }

    method compile ($next, $line, %args) {
        # XXX it'd be nice to avoid using globals here, but we can't use
        # eval_closure's environment parameter since we need to access the
        # information in a BEGIN block
        our $HINTS        = $hints;
        our $HINTHASH     = $hinthash;
        our $WARNING_BITS = $warning_bits;

        my @result = $next->($line, %args);

        $hints        = $HINTS;
        $hinthash     = $HINTHASH;
        $warning_bits = $WARNING_BITS;

        return @result;
    }
}

1;