From 6bd057874f99aeb2c0d243880f3333a64d96045f Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 27 Jun 2013 18:21:59 -0400 Subject: clean up the publisher stuff some more --- lib/Reply.pm | 20 ++++++++++++++------ lib/Reply/Plugin.pm | 17 ++++++++++++++++- lib/Reply/Plugin/LexicalPersistence.pm | 3 +-- lib/Reply/Plugin/Packages.pm | 5 ++--- lib/Reply/Plugin/ReadLine.pm | 7 ++++--- lib/Reply/Plugin/ResultCache.pm | 3 +-- 6 files changed, 38 insertions(+), 17 deletions(-) diff --git a/lib/Reply.pm b/lib/Reply.pm index 57e9166..bfcab4f 100644 --- a/lib/Reply.pm +++ b/lib/Reply.pm @@ -81,11 +81,10 @@ sub new { my $class = shift; my %opts = @_; - require Reply::Plugin::Defaults; - my $self = bless { - plugins => [], - _default_plugin => Reply::Plugin::Defaults->new, - }, $class; + my $self = bless {}, $class; + + $self->{plugins} = []; + $self->{_default_plugin} = $self->_instantiate_plugin('Defaults'); if (defined $opts{config}) { if (!ref($opts{config})) { @@ -183,6 +182,15 @@ sub _load_plugin { my $self = shift; my ($plugin, $opts) = @_; + $plugin = $self->_instantiate_plugin($plugin, $opts); + + push @{ $self->{plugins} }, $plugin; +} + +sub _instantiate_plugin { + my $self = shift; + my ($plugin, $opts) = @_; + if (!blessed($plugin)) { $plugin = compose_module_name("Reply::Plugin", $plugin); require_module($plugin); @@ -198,7 +206,7 @@ sub _load_plugin { ); } - push @{ $self->{plugins} }, $plugin; + return $plugin; } sub _plugins { diff --git a/lib/Reply/Plugin.pm b/lib/Reply/Plugin.pm index 4eca813..c0746d1 100644 --- a/lib/Reply/Plugin.pm +++ b/lib/Reply/Plugin.pm @@ -137,7 +137,22 @@ should terminate. =cut -sub new { bless {}, shift } +sub new { + my $class = shift; + my (%opts) = @_; + + die "publisher is required" unless $opts{publisher}; + + return bless { + publisher => $opts{publisher}, + }, $class; +} + +sub publish { + my $self = shift; + + $self->{publisher}->(@_); +} =for Pod::Coverage new diff --git a/lib/Reply/Plugin/LexicalPersistence.pm b/lib/Reply/Plugin/LexicalPersistence.pm index 99c328c..e3bbb9a 100644 --- a/lib/Reply/Plugin/LexicalPersistence.pm +++ b/lib/Reply/Plugin/LexicalPersistence.pm @@ -26,7 +26,6 @@ sub new { my $self = $class->SUPER::new(@_); $self->{env} = {}; - $self->{publisher} = $opts{publisher}; return $self; } @@ -42,7 +41,7 @@ sub compile { %{ peek_sub($code) }, }; - $self->{publisher}->('lexical_environment', default => $self->{env}); + $self->publish('lexical_environment', default => $self->{env}); return $code; } diff --git a/lib/Reply/Plugin/Packages.pm b/lib/Reply/Plugin/Packages.pm index 054467a..a206d6e 100644 --- a/lib/Reply/Plugin/Packages.pm +++ b/lib/Reply/Plugin/Packages.pm @@ -26,9 +26,8 @@ sub new { my $self = $class->SUPER::new(@_); $self->{package} = $opts{default_package} || 'main'; - $self->{publisher} = $opts{publisher}; - $self->{publisher}->(package => $self->{package}); + $self->publish('package', $self->{package}); return $self; } @@ -57,7 +56,7 @@ sub compile { # eval_closure's environment parameter since we need to access the # information in a BEGIN block $self->{package} = our $package; - $self->{publisher}->(package => $self->{package}); + $self->publish('package', $self->{package}); return @result; } diff --git a/lib/Reply/Plugin/ReadLine.pm b/lib/Reply/Plugin/ReadLine.pm index 757ea53..6465369 100644 --- a/lib/Reply/Plugin/ReadLine.pm +++ b/lib/Reply/Plugin/ReadLine.pm @@ -7,6 +7,7 @@ use base 'Reply::Plugin'; use File::HomeDir; use File::Spec; +use Scalar::Util 'weaken'; use Term::ReadLine; =head1 SYNOPSIS @@ -37,7 +38,6 @@ sub new { my $self = $class->SUPER::new(@_); $self->{term} = Term::ReadLine->new('Reply'); - $self->{publisher} = $opts{publisher}; my $history = $opts{history_file} || '.reply_history'; $self->{history_file} = File::Spec->catfile( (File::Spec->file_name_is_absolute($history) @@ -91,7 +91,8 @@ sub _register_tab_complete { my $self = shift; my $term = $self->{term}; - my $publisher = $self->{publisher}; + + weaken(my $weakself = $self); if ($term->ReadLine eq 'Term::ReadLine::Gnu') { $term->Attribs->{attempted_completion_function} = sub { @@ -100,7 +101,7 @@ sub _register_tab_complete { # discard everything after the cursor for completion purposes substr($line, $end) = ''; - my @matches = $publisher->('tab_handler', $line); + my @matches = $weakself->publish('tab_handler', $line); my $match_index = 0; return $term->completion_matches($text, sub { diff --git a/lib/Reply/Plugin/ResultCache.pm b/lib/Reply/Plugin/ResultCache.pm index 713a7fd..beb884b 100644 --- a/lib/Reply/Plugin/ResultCache.pm +++ b/lib/Reply/Plugin/ResultCache.pm @@ -28,7 +28,6 @@ sub new { my $self = $class->SUPER::new(@_); $self->{results} = []; $self->{result_name} = $opts{variable} || 'res'; - $self->{publisher} = $opts{publisher}; return $self; } @@ -45,7 +44,7 @@ sub execute { push @{ $self->{results} }, \@res; } - $self->{publisher}->( + $self->publish( 'lexical_environment', result_cache => { "\@$self->{result_name}" => $self->{results}, -- cgit v1.2.3-54-g00ecf