diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2019-08-19 17:51:19 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2019-08-19 18:19:33 +0300 |
commit | 11e6beb1f44fb74dc11e79c3bda25aebfd80ae12 (patch) | |
tree | bef1bfcecd9ad587c582a79a8dac78ca5f7569b7 /lib | |
parent | d417ed4a1875321b28c47ea5f6324479d1592309 (diff) | |
download | config-parser-11e6beb1f44fb74dc11e79c3bda25aebfd80ae12.tar.gz config-parser-11e6beb1f44fb74dc11e79c3bda25aebfd80ae12.tar.bz2 |
Improve constructor
This commit introduces the "mangle" method, which can be implemented
by the caller in order to run any additional tree modifications after
a successful parse and commit (see the new testcase for an example).
* lib/Config/Parser.pm (new): Call the init method if lexicon
hash has been loaded.
(init,inited,commit): New methods.
* t/ConfigSpec3.pm: New file.
* t/TestConfig.pm: Don't attempt to load empty files.
* t/conf08.t: New file.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Config/Parser.pm | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/lib/Config/Parser.pm b/lib/Config/Parser.pm index 843b70c..fed0100 100644 --- a/lib/Config/Parser.pm +++ b/lib/Config/Parser.pm @@ -12,6 +12,7 @@ our $VERSION = "1.00"; sub new { my $class = shift; local %_ = @_; + my $loaded = 0; my @parseargs; if (my $filename = delete $_{filename}) { @@ -31,6 +32,7 @@ sub new { # print "LOADING FROM $c\n"; if (my $s = loadsynt($c)) { $_{lexicon} = { %{$_{lexicon}}, %$s }; + $loaded++; } last if $c eq $class; } @@ -39,7 +41,7 @@ sub new { } my $self = $class->SUPER::new(%_); - + $self->init if $loaded; if (@parseargs) { $self->parse(@parseargs); $self->commit or croak "configuration failed"; @@ -48,6 +50,22 @@ sub new { return $self; } +sub init { + my $self = shift; + $self->{_inited} = 1; +} + +sub inited { shift->{_inited} }; + +sub commit { + my $self = shift; + my $res = $self->SUPER::commit; + if ($res && $self->inited && $self->can('mangle')) { + $self->mangle; + } + return $res; +} + sub findsynt { my $class = shift; my $file = $class; @@ -67,7 +85,8 @@ sub findsynt { sub loadsynt { my ($class) = @_; if (my ($file, $line, $data) = findsynt($class)) { - open(my $fh, '<', \$data); + open(my $fh, '<', \$data) + or croak "can't open filehandle for data string"; my $d = $class->new(filename => $file, fh => $fh, line => $line, |