summaryrefslogtreecommitdiff
path: root/lib/Config/AST.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Config/AST.pm')
-rw-r--r--lib/Config/AST.pm42
1 files changed, 34 insertions, 8 deletions
diff --git a/lib/Config/AST.pm b/lib/Config/AST.pm
index 721d515..958c106 100644
--- a/lib/Config/AST.pm
+++ b/lib/Config/AST.pm
@@ -23,6 +23,7 @@ use Text::Locus;
use Config::AST::Node qw(:sort);
use Config::AST::Node::Section;
use Config::AST::Node::Value;
+use Config::AST::Follow;
use Data::Dumper;
require Exporter;
@@ -670,7 +671,7 @@ node at path
one can write:
- $node = $cfg->tree->Foo->Bar->Baz
+ $node = $cfg->foo->bar->baz
This statement is equivalent to
@@ -678,20 +679,45 @@ This statement is equivalent to
except that if the node in question does not exist, direct access returns
a I<null node>, and B<getnode> returns C<undef>. Null node is a special node
-representing a missing node. Its B<is_null> method returns true and it can
+representing a missing node. Its B<is_null> method returns true and it can
be used in conditional context as a boolean value, e.g.:
- if (my $node = $cfg->tree->Foo->Bar->Baz) {
+ if (my $node = $cfg->foo->bar->baz) {
$val = $node->value;
}
-To compose direct access statement, first capitalize each path component. If
-the component name contains dashes, replace them with double underscores. Use
-the resulting names as methods of B<$cfg-E<gt>tree>. For example, to
-retrieve the C<qw(files temp-dir)> node, use
+Direct addressing is enabled only if lexicon is provided (either during
+creation of the object, or later, via the B<lexicon> method).
- $cfg->tree->Files->Temp__dir;
+Obviously, statements that have names coinciding with one of the methods of
+the B<Config::AST> class (or any of its subclasses) can't be used in direct
+addressing. In other words, you can't have a top-level statement called
+C<tree> and access it as
+ $cfg->tree
+
+This statement will always refer to the method B<tree> of the B<Config::AST>
+class.
+
+Another possible problem when using direct access are keywords with dashes.
+Currently a kludge is implemented to make it possible to access such
+keywords: when looking for a matching keyword, double underscores compare
+equal to a single dash. For example, to retrieve the C<qw(files temp-dir)>
+node, use
+
+ $cfg->files->temp__dir;
+
+=cut
+
+our $AUTOLOAD;
+sub AUTOLOAD {
+ my $self = shift;
+ $AUTOLOAD =~ s/(?:(.*)::)?(.+)//;
+ my ($p, $m) = ($1, $2);
+ croak "Can't locate object method \"$m\" via package \"$p\""
+ if @_ || !$self->lexicon;
+ return Config::AST::Follow->new($self->tree, $self->lexicon)->${\$m};
+}
=head1 CONSTRUCTING THE SYNTAX TREE

Return to:

Send suggestions and report system problems to the System administrator.