diff options
Diffstat (limited to 'lib/Config/Tree/Node.pm')
-rw-r--r-- | lib/Config/Tree/Node.pm | 63 |
1 files changed, 61 insertions, 2 deletions
diff --git a/lib/Config/Tree/Node.pm b/lib/Config/Tree/Node.pm index 79e105f..f836edf 100644 --- a/lib/Config/Tree/Node.pm +++ b/lib/Config/Tree/Node.pm @@ -3,6 +3,7 @@ package Config::Tree::Node; use strict; use warnings; use parent 'Exporter'; +use Config::Tree::Locus; use Clone 'clone'; use Carp; @@ -37,8 +38,6 @@ Creates new object. Recognized arguments are: Clone object I<OBJ>, which must be an instance of B<Config::Tree::Node> or its derived class. -=item - =item B<default =E<gt>> I<VAL> Sets default value. @@ -244,6 +243,66 @@ sub flatten { return &{$sort}(grep { $_->[1]->is_value } @ar); } +=head2 $cfg->canonical(%args) + +Returns the canonical string representation of the configuration node. +For value nodes, canonical representation is: + + QVAR=VALUE + +where QVAR is fully qualified variable name, and VALUE is the corresponding +value. + +For sections, canonical representation is a list of canonical representations +of the underlying nodes, delimited by newlines (or another character - see the +description of the B<delim> argument, below). The list is sorted by QVAR in +ascending lexicographical order. + +B<%args> are zero or more of the following keywords: + +=over 4 + +=item B<delim =E<gt> >I<STR> + +Use I<STR> to delimit statements, instead of the newline. + +=item B<locus =E<gt> 1> + +Prefix each statement with its location. + +=back + +=cut + +sub canonical { + my $self = shift; + local %_ = @_; + my $delim; + unless (defined($delim = delete $_{delim})) { + $delim = "\n"; + } + my $prloc = delete $_{locus}; + carp "unknown parameters: " . join(', ', keys(%_)) if (keys(%_)); + + return join $delim, map { + ($prloc ? '[' . $_->[1]->locus . ']: ' : '') + . join('.', map { + if (/[\.="]/) { + s/\"/\\"/; + '"'.$_.'"' + } else { + $_ + } + } @{$_->[0]}) + . "=" + . Data::Dumper->new([scalar $_->[1]->value]) + ->Useqq(1) + ->Terse(1) + ->Indent(0) + ->Dump + } $self->flatten(sort => SORT_PATH); +} + use overload bool => sub { 1 }, '""' => sub { shift->as_string }, |