authorSergey Poznyakoff <>2019-08-27 12:24:29 (GMT)
committer Sergey Poznyakoff <>2019-08-27 12:24:29 (GMT)
commit745a02db1c53ed90e89bb9c6f57db450d2011106 (patch) (side-by-side diff)
parent058e552bd37c0acf9d3a10fc743ac4d76d579b45 (diff)
Improve commit / lint API
* lib/Config/ (commit): Take optional keyword arguments. (lint): The argument (reference to a lexicon) is optional.
Diffstat (more/less context) (ignore whitespace changes)
1 files changed, 44 insertions, 16 deletions
diff --git a/lib/Config/ b/lib/Config/
index 837bbac..fb10569 100644
--- a/lib/Config/
+++ b/lib/Config/
@@ -376,25 +376,45 @@ the configuration should be reported using B<error>.
sub parse {
my ($self) = @_;
croak "call to abstract method"
-=head2 $cfg->commit
+=head2 $cfg->commit([%hash])
Must be called after B<parse> to finalize the parse tree. This function
-does the actual syntax checking and applied default values to the statements
-where such are defined. Returns true on success.
+applies default values on settings where such are defined.
+Optional arguments control what steps are performed.
+=over 4
+=item lint => 1
+Forse syntax checking. This can be necessary if new nodes were added to
+the tree after parsing.
+=item lexicon => I<$hashref>
+Override the lexicon used for syntax checking and default value processing.
+Returns true on success.
sub commit {
- my ($self) = @_;
- $self->fixup_tree($self->tree, $self->{_lexicon})
- if exists $self->{_lexicon};
+ my ($self, %opts) = @_;
+ my $lint = delete $opts{lint};
+ my $lexicon = delete $opts{lexicon} // $self->lexicon;
+ croak "unrecognized arguments" if keys(%opts);
+ if ($lexicon) {
+ $self->lint_subtree($lexicon, $self->tree) if $lint;
+ $self->fixup_tree($self->tree, $lexicon);
+ }
return $self->{_error_count} == 0;
sub fixup_tree {
my ($self, $section, $params, @path) = @_;
@@ -762,13 +782,17 @@ Adds the node in the node corresponding to B<$path>. B<$path> can be
either a list of keyword names, or its string representation, where
names are separated by dots. I.e., the following two calls are equivalent:
$cfg->add_node(qw(core pidfile), $node)
$cfg->add_node('core.pidfile', $node)
+If the node already exists at B<$path>, new node is merged to it according
+to the lexical rules. I.e., for scalar value, new node overwrites the old
+one. For lists, it is appended to the list.
sub add_node {
my ($self, $path, $node) = @_;
unless (ref($path) eq 'ARRAY') {
@@ -888,24 +912,31 @@ sub add_node {
=head2 $cfg->add_value($path, $value, $locus)
Adds a statement node with the given B<$value> and B<$locus> in position,
indicated by $path.
+If the setting already exists at B<$path>, the new value is merged to it
+according to the lexical rules. I.e., for scalars, B<$value> overwrites
+prior setting. For lists, it is appended to the list.
sub add_value {
my ($self, $path, $value, $locus) = @_;
$self->add_node($path, new Config::AST::Node::Value(value => $value,
- locus => $locus));
+ locus => $locus));
=head2 $cfg->set(@path, $value)
Sets the configuration variable B<@path> to B<$value>.
+No syntax checking is performed. To enforce syntax checking use
sub set {
my $self = shift;
my $node = $self->tree;
@@ -1166,31 +1197,28 @@ sub lint_subtree {
locus => $value->locus);
-=head2 $cfg->lint(\%lex)
+=head2 $cfg->lint([\%lex])
-Checks the syntax according to the keyword lexicon B<%lex>. On success,
+Checks the syntax according to the keyword lexicon B<%lex> (or
+B<$cfg-E<gt>lexicon>, if called without arguments). On success,
applies eventual default values and returns true. On errors, reports
them using B<error> and returns false.
This method provides a way to delay syntax checking for a later time,
which is useful, e.g. if some parts of the parser are loaded as modules
after calling B<parse>.
sub lint {
my ($self, $lexicon) = @_;
-# $synt->{'*'} = { section => { '*' => 1 }} ;
- $self->lint_subtree($lexicon, $self->tree);
- $self->fixup_tree($self->tree, $lexicon);
- return $self->{_error_count} == 0;
+ return $self->commit(lint => 1, lexicon => $lexicon);
=head1 SEE ALSO

Return to:

Send suggestions and report system problems to the System administrator.