I no longer use this plugin in my bundle
[p5sagit/Devel-REPL.git] / lib / Devel / REPL / Plugin / Commands.pm
CommitLineData
1716b200 1use strict;
2use warnings;
4d33251a 3package Devel::REPL::Plugin::Commands;
4
54beb05d 5our $VERSION = '1.003027';
6
4d33251a 7use Devel::REPL::Plugin;
8use Scalar::Util qw(weaken);
aa8b7647 9use namespace::autoclean;
1d6c2dbc 10
28e93f31 11our $COMMAND_INSTALLER;
4d33251a 12
13has 'command_set' => (
b595a818 14 is => 'ro',
4d33251a 15 lazy => 1, default => sub { {} }
16);
17
18sub BEFORE_PLUGIN {
19 my ($self) = @_;
deda07fb 20 $self->load_plugin('Packages');
4d33251a 21 unless ($self->can('setup_commands')) {
22 $self->meta->add_method('setup_commands' => sub {});
23 }
24}
25
26sub AFTER_PLUGIN {
27 my ($self) = @_;
28 $self->setup_commands;
29}
30
31after 'setup_commands' => sub {
32 my ($self) = @_;
33 weaken($self);
6edfdc07 34 $self->command_set->{load_plugin} = sub {
35 my $self = shift;
36 sub { $self->load_plugin(@_); };
37 };
4d33251a 38};
39
40sub command_installer {
41 my ($self) = @_;
6edfdc07 42 my $command_set = $self->command_set;
43 my %command_subs = map {
44 ($_ => $command_set->{$_}->($self));
45 } keys %$command_set;
4d33251a 46 return sub {
47 my $package = shift;
6edfdc07 48 foreach my $command (keys %command_subs) {
4d33251a 49 no strict 'refs';
50 no warnings 'redefine';
6edfdc07 51 *{"${package}::${command}"} = $command_subs{$command};
4d33251a 52 }
53 };
54}
55
56around 'mangle_line' => sub {
57 my ($orig, $self) = (shift, shift);
58 my ($line) = @_;
59 my $name = '$'.__PACKAGE__.'::COMMAND_INSTALLER';
60 return qq{BEGIN { ${name}->(__PACKAGE__) }\n}.$self->$orig(@_);
61};
62
63around 'compile' => sub {
64 my ($orig, $self) = (shift, shift);
65 local $COMMAND_INSTALLER = $self->command_installer;
66 $self->$orig(@_);
67};
68
691;
cfd1094b 70
71__END__
72
73=head1 NAME
74
75Devel::REPL::Plugin::Commands - Generic command creation plugin using injected functions
76
77=cut
78