diff options
author | Jesse Luehrs <doy@tozt.net> | 2013-05-30 02:23:27 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2013-05-30 02:23:27 -0500 |
commit | 34218fd2af78b254dd424de2289981621ee85f94 (patch) | |
tree | 6285e43c4d6f1960af973c45dffb901104836ff4 | |
parent | 30443a17d94ddee3b5343aefeebb46e8b7383bc1 (diff) | |
download | reply-34218fd2af78b254dd424de2289981621ee85f94.tar.gz reply-34218fd2af78b254dd424de2289981621ee85f94.zip |
use ini configuration, not perl
-rw-r--r-- | bin/repl | 30 | ||||
-rw-r--r-- | lib/App/REPL.pm | 64 |
2 files changed, 51 insertions, 43 deletions
@@ -7,7 +7,7 @@ use App::REPL; my $cfg = "$ENV{HOME}/.replrc"; -my %args = (script => $cfg); +my %args = (config => $cfg); if (!-e $cfg) { print("$cfg not found. Generating a default...\n"); if (open my $fh, '>', $cfg) { @@ -23,20 +23,14 @@ if (!-e $cfg) { App::REPL->new(%args)->run; __DATA__ -load_plugin - 'Interrupt', - - 'FancyPrompt', - 'DataDumper', - - 'Colors', - 'ReadLine', - 'Hints', - 'Packages', - 'LexicalPersistence', - ; - -postlude <<POSTLUDE; -use strict; -use warnings; -POSTLUDE +script_line1 = use strict +script_line2 = use warnings + +[Interrupt] +[FancyPrompt] +[DataDumper] +[Colors] +[ReadLine] +[Hints] +[Packages] +[LexicalPersistence] diff --git a/lib/App/REPL.pm b/lib/App/REPL.pm index fde2d0c..54a674c 100644 --- a/lib/App/REPL.pm +++ b/lib/App/REPL.pm @@ -3,6 +3,7 @@ use strict; use warnings; # ABSTRACT: simple, pluggable repl +use Config::INI::Reader::Ordered; use Module::Runtime qw(compose_module_name use_package_optimistically); use Scalar::Util qw(blessed); use Try::Tiny; @@ -17,30 +18,12 @@ sub new { _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; + $self->load_plugin($_) for @{ $opts{plugins} || [] }; - if (defined $postlude) { - $self->_eval($postlude); + if (defined $opts{config}) { + print "Loading configuration from $opts{config}... "; + $self->load_config($opts{config}); + print "done\n"; } return $self; @@ -48,19 +31,50 @@ sub new { sub load_plugin { my $self = shift; - my ($plugin) = @_; + my ($plugin, $opts) = @_; if (!blessed($plugin)) { $plugin = compose_module_name("App::REPL::Plugin", $plugin); use_package_optimistically($plugin); die "$plugin is not a valid plugin" unless $plugin->isa("App::REPL::Plugin"); - $plugin = $plugin->new; + $plugin = $plugin->new(%$opts); } push @{ $self->{plugins} }, $plugin; } +sub load_config { + my $self = shift; + my ($file) = @_; + + my $data = Config::INI::Reader::Ordered->new->read_file($file); + + my $root_config; + for my $section (@$data) { + my ($name, $data) = @$section; + if ($name eq '_') { + $root_config = $data; + } + else { + $self->load_plugin($name => $data); + } + } + + for my $line (sort grep { /^script_line/ } keys %$root_config) { + $self->_eval($root_config->{$line}); + } + + if (defined(my $file = $root_config->{script_file})) { + my $contents = do { + open my $fh, '<', $file or die "Couldn't open $file: $!"; + local $/ = undef; + <$fh> + }; + $self->_eval($contents); + } +} + sub plugins { my $self = shift; |