From 34218fd2af78b254dd424de2289981621ee85f94 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 30 May 2013 02:23:27 -0500 Subject: use ini configuration, not perl --- lib/App/REPL.pm | 64 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 25 deletions(-) (limited to 'lib') 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; -- cgit v1.2.3-54-g00ecf