summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2013-05-29 04:45:46 -0500
committerJesse Luehrs <doy@tozt.net>2013-05-29 04:45:46 -0500
commitce62e06a2ad801aec1bf9f393475b01d25670892 (patch)
treee8111b32210cafdefe22938d3312fa0c46dedb2e
parentf976a32382588d1596c4f13d471bd6664ad9748d (diff)
downloadreply-ce62e06a2ad801aec1bf9f393475b01d25670892.tar.gz
reply-ce62e06a2ad801aec1bf9f393475b01d25670892.zip
config file loading
-rw-r--r--bin/repl2
-rw-r--r--lib/App/REPL.pm51
2 files changed, 41 insertions, 12 deletions
diff --git a/bin/repl b/bin/repl
index b61c670..24cc351 100644
--- a/bin/repl
+++ b/bin/repl
@@ -4,4 +4,4 @@ use warnings;
use App::REPL;
-App::REPL->new->run;
+App::REPL->new(script => "$ENV{HOME}/.replrc")->run;
diff --git a/lib/App/REPL.pm b/lib/App/REPL.pm
index 28837d4..484cb84 100644
--- a/lib/App/REPL.pm
+++ b/lib/App/REPL.pm
@@ -2,19 +2,47 @@ package App::REPL;
use strict;
use warnings;
-use App::REPL::Plugin::Defaults;
-
use Module::Runtime qw(compose_module_name use_package_optimistically);
use Scalar::Util qw(blessed);
use Try::Tiny;
sub new {
- bless {
- plugins => [],
- _default_plugins => [
- App::REPL::Plugin::Defaults->new,
- ],
- }, shift;
+ my $class = shift;
+ my %opts = @_;
+
+ require App::REPL::Plugin::Defaults;
+ my $self = bless {
+ plugins => [],
+ _default_plugin => App::REPL::Plugin::Defaults->new,
+ }, $class;
+
+ my @plugins;
+ my $postlude;
+ if (exists $opts{script}) {
+ my $script = do {
+ open my $fh, '<', $opts{script}
+ or die "Can't open $opts{script}: $!";
+ local $/ = undef;
+ <$fh>
+ };
+ local *main::load_plugin = sub {
+ push @plugins, @_;
+ };
+ local *main::postlude = sub {
+ $postlude .= $_[0];
+ };
+ print "Loading configuration from $opts{script}... ";
+ $self->_eval($script);
+ print "done\n";
+ }
+
+ $self->load_plugin($_) for @{ $opts{plugins} || [] }, @plugins;
+
+ if (defined $postlude) {
+ $self->_eval($postlude);
+ }
+
+ return $self;
}
sub load_plugin {
@@ -37,7 +65,7 @@ sub plugins {
return (
@{ $self->{plugins} },
- @{ $self->{_default_plugins} },
+ $self->{_default_plugin},
);
}
@@ -61,8 +89,6 @@ sub _read {
$self->_wrapped_plugin('display_prompt');
my ($line) = $self->_wrapped_plugin('read_line');
- ($line) = $self->_chained_plugin('mangle_line', $line)
- if defined $line;
return $line;
}
@@ -71,6 +97,9 @@ sub _eval {
my $self = shift;
my ($line) = @_;
+ ($line) = $self->_chained_plugin('mangle_line', $line)
+ if defined $line;
+
return $self->_wrapped_plugin('evaluate', $line);
}