| 1 | package Config::Parser; | ||||||||||||||||||||||||||||
| 2 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use parent 'Config::AST'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use Cwd qw(abs_path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use Text::ParseWords; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use mro; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 10 | our $VERSION = "1.05"; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 11 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=15 SCAR=31.1
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 11 | my $class = shift; | |||||||||||||||||||||||||||
| 14 | 11 | local %_ = @_; | |||||||||||||||||||||||||||
| 15 | 11 | my $loaded = 0; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 11 | my @parseargs; | |||||||||||||||||||||||||||
| 18 | 11 | ▶ | if (my $filename = delete $_{filename}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 0 | push @parseargs, $filename; | |||||||||||||||||||||||||||
| 20 | 0 | foreach my $k (qw(fh line)) { | |||||||||||||||||||||||||||
| 21 | 0 | ▶ | if (my $v = delete $_{$k}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 0 | push @parseargs, ($k, $v); | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 11 | my $self = $class->SUPER::new(%_); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 11 | ▶ | if (my $lex = delete $_{lexicon}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 0 | $self->lexicon($lex); | |||||||||||||||||||||||||||
| 31 | } else { | ||||||||||||||||||||||||||||
| 32 | 11 | $self->lexicon({ '*' => '*' }); | |||||||||||||||||||||||||||
| 33 | 66 | ▶ | my @cl = grep { $_ ne __PACKAGE__ && $_->isa(__PACKAGE__) } | ||||||||||||||||||||||||||
|
Condition100%3/3
$_ ne __PACKAGE__ && $_->isa(__PACKAGE__)
MC/DC100%2/2
$_ ne __PACKAGE__ && $_->isa(__PACKAGE__)
$_ ne __PACKAGE__
$_->isa(__PACKAGE__)
Truth table100%3/3
A && B
A= $_ ne __PACKAGE__B= $_->isa(__PACKAGE__)
| |||||||||||||||||||||||||||||
| 34 | 11 | reverse @{mro::get_linear_isa($class)}; | |||||||||||||||||||||||||||
| 35 | 11 | my $dict; | |||||||||||||||||||||||||||
| 36 | 11 | ▶ | if (@cl) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 11 | foreach my $c (@cl) { | |||||||||||||||||||||||||||
| 38 | 33 | ▶ | if (my ($file, $line, $data) = $c->findsynt) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 11 | my $d = $self->loadsynt($file, $line, $data); | |||||||||||||||||||||||||||
| 40 | 11 | ▶ | if ($d) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 11 | ▶ | $dict = { %{$dict // {}}, %$d } | ||||||||||||||||||||||||||
|
Condition50%1/2
$dict // {}
MC/DC0%0/1
$dict // {}
$dict
Truth table50%1/2
A
A= $dict
| |||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 33 | ▶ | last if $c eq $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 11 | ▶ | $self->lexicon($dict) if $dict; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 11 | $self->init; | |||||||||||||||||||||||||||
| 51 | 11 | ▶ | if (@parseargs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | $self->parse(@parseargs); | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | $self->commit or croak "configuration failed"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 11 | return $self; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 11 | ▶ | sub init {} | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 3 | ▶ | sub mangle {} | ||||||||||||||||||||||||||
|
Subroutine
mangle: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 9 | ▶ | sub commit { | ||||||||||||||||||||||||||
|
Subroutine
commit: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 64 | 9 | my $res = $self->SUPER::commit; | |||||||||||||||||||||||||||
| 65 | 9 | ▶ | $self->mangle if $res; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 9 | return $res; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 33 | ▶ | sub findsynt { | ||||||||||||||||||||||||||
|
Subroutine
findsynt: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | 33 | my $class = shift; | |||||||||||||||||||||||||||
| 71 | 33 | my $file = $class; | |||||||||||||||||||||||||||
| 72 | 33 | $file =~ s{::}{/}g; | |||||||||||||||||||||||||||
| 73 | 33 | $file .= '.pm'; | |||||||||||||||||||||||||||
| 74 | 33 | ▶ | $file = abs_path($INC{$file}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | or croak "can't find module file for $class"; | ||||||||||||||||||||||||||||
| 76 | 33 | local ($/, *FILE); | |||||||||||||||||||||||||||
| 77 | 33 | ▶ | open FILE, $file or croak "Can't open $file"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 33 | my ($text, $data) = split /(?m)^__DATA__$/, <FILE>, 2; | |||||||||||||||||||||||||||
| 79 | 33 | close FILE; | |||||||||||||||||||||||||||
| 80 | 33 | ▶ | return ($file, 1+($text =~ tr/\n//), $data) if $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 22 | return (); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 11 | ▶ | sub loadsynt { | ||||||||||||||||||||||||||
|
Subroutine
loadsynt: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 85 | 11 | my ($self, $file, $line, $data) = @_; | |||||||||||||||||||||||||||
| 86 | 11 | ▶ | open(my $fh, '<', \$data) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | or croak "can't open filehandle for data string"; | ||||||||||||||||||||||||||||
| 88 | 11 | ▶ | $self->parse($file, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | fh => $fh, | ||||||||||||||||||||||||||||
| 90 | line => $line) | ||||||||||||||||||||||||||||
| 91 | or croak "Failed to parse template at $file:$line"; | ||||||||||||||||||||||||||||
| 92 | 11 | close $fh; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 11 | my @sections; | |||||||||||||||||||||||||||
| 95 | 95 | ▶ | my $lex = $self->as_hash(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=14 SCAR=26.5
| |||||||||||||||||||||||||||||
| 96 | 95 | my ($what, $name, $val) = @_; | |||||||||||||||||||||||||||
| 97 | 95 | ▶ | $name = '*' if $name eq 'ANY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 95 | ▶ | if ($what eq 'section') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 39 | $val->{section} = {}; | |||||||||||||||||||||||||||
| 100 | 39 | push @sections, $val; | |||||||||||||||||||||||||||
| 101 | 39 | ($name, $val->{section}); | |||||||||||||||||||||||||||
| 102 | } else { | ||||||||||||||||||||||||||||
| 103 | 56 | my @words = parse_line('\s+', 0, $val); | |||||||||||||||||||||||||||
| 104 | 56 | my $ret = {}; | |||||||||||||||||||||||||||
| 105 | 56 | $val = shift @words; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 56 | ▶ | if ($val eq 'STRING') { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 108 | # nothing | ||||||||||||||||||||||||||||
| 109 | ▶ | } elsif ($val eq 'NUMBER' || $val eq 'DECIMAL') { | |||||||||||||||||||||||||||
|
Condition66%2/3
$val eq "NUMBER" or $val eq "DECIMAL"
MC/DC50%1/2
$val eq "NUMBER" or $val eq "DECIMAL"
$val eq "NUMBER"
$val eq "DECIMAL"
Truth table66%2/3
A or B
A= $val eq "NUMBER"B= $val eq "DECIMAL"
| |||||||||||||||||||||||||||||
| 110 | 6 | $ret->{re} = '^\d+$'; | |||||||||||||||||||||||||||
| 111 | } elsif ($val eq 'OCTAL') { | ||||||||||||||||||||||||||||
| 112 | 3 | $ret->{re} = '^[0-7]+$'; | |||||||||||||||||||||||||||
| 113 | } elsif ($val eq 'HEX') { | ||||||||||||||||||||||||||||
| 114 | 0 | $ret->{re} = '^([0-9][A-Fa-f])+$'; | |||||||||||||||||||||||||||
| 115 | } elsif ($val =~ /^BOOL(EAN)?$/) { | ||||||||||||||||||||||||||||
| 116 | 6 | $ret->{check} = \&check_bool; | |||||||||||||||||||||||||||
| 117 | } else { | ||||||||||||||||||||||||||||
| 118 | 3 | unshift @words, $val; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 56 | ▶ | while (($val = $words[0]) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$val = $words[0] and $val =~ /^:(?<kw>.+?)(?:\s*=\s*(?<val>.*))?$/
MC/DC100%2/2
$val = $words[0] and $val =~ /^:(?<kw>.+?)(?:\s*=\s*(?<val>.*))?$/
$val = $words[0]
$val =~ /^:(?<kw>.+?)(?:\s*=\s*(?<val>.*))?$/
Truth table100%3/3
A and B
A= $val = $words[0]B= $val =~ /^:(?<kw>.+?)(?:\s*=\s*(?<val>.*))?$/
| |||||||||||||||||||||||||||||
| 122 | && $val =~ /^:(?<kw>.+?)(?:\s*=\s*(?<val>.*))?$/) { | ||||||||||||||||||||||||||||
| 123 | 38 | ▶ | $ret->{$+{kw}} = $+{val} // 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
$+{"val"} // 1
MC/DC100%1/1
$+{"val"} // 1
$+{"val"}
Truth table100%2/2
A
A= $+{"val"}
| |||||||||||||||||||||||||||||
| 124 | 38 | shift @words; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 56 | ▶ | if (@words) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 9 | ▶ | if ($ret->{array}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | $ret->{default} = [@words]; | |||||||||||||||||||||||||||
| 129 | } else { | ||||||||||||||||||||||||||||
| 130 | 9 | $ret->{default} = join(' ', @words); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 56 | ($name, $ret); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | 11 | })->{section}; | |||||||||||||||||||||||||||
| 136 | # Process eventual __options__ keywords | ||||||||||||||||||||||||||||
| 137 | 11 | foreach my $s (@sections) { | |||||||||||||||||||||||||||
| 138 | 39 | ▶ | if (exists($s->{section}{__options__})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 3 | @{$s}{keys %{$s->{section}{__options__}}} | |||||||||||||||||||||||||||
| 140 | 3 | = values %{$s->{section}{__options__}}; | |||||||||||||||||||||||||||
| 141 | 3 | delete $s->{section}{__options__}; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 11 | return $lex; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 1 | ▶ | sub check_bool { | ||||||||||||||||||||||||||
|
Subroutine
check_bool: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 148 | 1 | my ($self, $valref, undef, $locus) = @_; | |||||||||||||||||||||||||||
| 149 | 1 | my %bv = ( | |||||||||||||||||||||||||||
| 150 | yes => 1, | ||||||||||||||||||||||||||||
| 151 | no => 0, | ||||||||||||||||||||||||||||
| 152 | true => 1, | ||||||||||||||||||||||||||||
| 153 | false => 0, | ||||||||||||||||||||||||||||
| 154 | on => 1, | ||||||||||||||||||||||||||||
| 155 | off => 0, | ||||||||||||||||||||||||||||
| 156 | t => 1, | ||||||||||||||||||||||||||||
| 157 | nil => 0, | ||||||||||||||||||||||||||||
| 158 | 1 => 1, | ||||||||||||||||||||||||||||
| 159 | 0 => 0 | ||||||||||||||||||||||||||||
| 160 | ); | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 1 | ▶ | if (exists($bv{$$valref})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 1 | $$valref = $bv{$$valref}; | |||||||||||||||||||||||||||
| 164 | 1 | return 1; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | 0 | $self->error("$$valref is not a valid boolean value", locus => $locus); | |||||||||||||||||||||||||||
| 167 | 0 | return 0; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 10 | 1; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =head1 NAME | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | Config::Parser - base class for configuration file parsers | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | B<Config::Parser> provides a framework for writing configuration file | ||||||||||||||||||||||||||||
| 179 | parsers. It is an intermediate layer between the abstract syntax tree | ||||||||||||||||||||||||||||
| 180 | (L<Config::AST>) and implementation of a parser for a particular | ||||||||||||||||||||||||||||
| 181 | configuration file format. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | It takes a I<define by example> approach. That means that the implementer | ||||||||||||||||||||||||||||
| 184 | creates a derived class that implements a parser on top of B<Config::Parser>. | ||||||||||||||||||||||||||||
| 185 | Application writers write an example of configuration file in the B<__DATA__> | ||||||||||||||||||||||||||||
| 186 | section of their application, which defines the statements that are allowed | ||||||||||||||||||||||||||||
| 187 | in a valid configuration. This example is then processed by the parser | ||||||||||||||||||||||||||||
| 188 | implementation to create an instance of the parser, which is then used to | ||||||||||||||||||||||||||||
| 189 | process the actual configuration file. | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | Let's illustrate this on a practical example. Suppose you need a parser for | ||||||||||||||||||||||||||||
| 192 | a simple configuration file, which consists of keyword/value pairs. In each | ||||||||||||||||||||||||||||
| 193 | pair, the keyword is separated from the value by an equals sign. Pairs are | ||||||||||||||||||||||||||||
| 194 | delimited by newlines. Leading and trailing whitespace characters on a line | ||||||||||||||||||||||||||||
| 195 | are ignored as well as are empty lines. Comments begin with a hash sign and | ||||||||||||||||||||||||||||
| 196 | end with a newline. | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | You create the class, say B<Config::Parser::KV>, inherited from | ||||||||||||||||||||||||||||
| 199 | B<Config::Parser>. The method B<parser> in this class implements the actual | ||||||||||||||||||||||||||||
| 200 | parser. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | Application writer decides what keywords are allowed in a valid configuration | ||||||||||||||||||||||||||||
| 203 | file and what are their values and describes them in the B<__DATA__> section | ||||||||||||||||||||||||||||
| 204 | of his program (normally in a class derived from B<Config::Parser::KV>, in | ||||||||||||||||||||||||||||
| 205 | the same format as the actual configuration file. For example: | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | __DATA__ | ||||||||||||||||||||||||||||
| 208 | basedir = STRING :mandatory | ||||||||||||||||||||||||||||
| 209 | mode = OCTAL | ||||||||||||||||||||||||||||
| 210 | size = NUMBER :array | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | This excerpt defines a configuration with three allowed statements. Uppercase | ||||||||||||||||||||||||||||
| 213 | values to the right of the equals sign are data types. Values starting with | ||||||||||||||||||||||||||||
| 214 | a colon are flags that define the semantics of the values. This section | ||||||||||||||||||||||||||||
| 215 | declares that three keywords are allowed. The B<basedir> keyword takes | ||||||||||||||||||||||||||||
| 216 | string as its argument and must be present in a valid configuration. The | ||||||||||||||||||||||||||||
| 217 | B<mode> expects octal number as its argument. The B<size> keyword takes | ||||||||||||||||||||||||||||
| 218 | a number. Multiple B<size> statements are collapsed into an array. | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | To parse the actual configuration file, the programmer creates an instance | ||||||||||||||||||||||||||||
| 221 | of the B<Config::Parse::KV> class, passing it the name of the file as its | ||||||||||||||||||||||||||||
| 222 | argument: | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | $cf = new Config::Parse::KV($filename); | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | This call first parses the B<__DATA__> section and builds validation rules, | ||||||||||||||||||||||||||||
| 227 | then it parses the actual configuration from B<$filename>. Finally, it | ||||||||||||||||||||||||||||
| 228 | applies the validation rules to the created syntax tree. If all rules pass, | ||||||||||||||||||||||||||||
| 229 | the configuration is correct and the constructor returns a valid object. | ||||||||||||||||||||||||||||
| 230 | Otherwise, it issues proper diagnostics and croaks. | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | Upon successful return, the B<$cf> object is used to obtain the actual | ||||||||||||||||||||||||||||
| 233 | configuration values as needed. | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | Notice that syntax declarations in the B<__DATA__> section always follow the | ||||||||||||||||||||||||||||
| 236 | actual configuration file format, that's why we call them I<definition by | ||||||||||||||||||||||||||||
| 237 | example>. For instance, the syntax definition for a configuration file in | ||||||||||||||||||||||||||||
| 238 | Apache-like format would look like | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | __DATA__ | ||||||||||||||||||||||||||||
| 241 | <section ANY> | ||||||||||||||||||||||||||||
| 242 | basedir STRING :mandatory | ||||||||||||||||||||||||||||
| 243 | mode OCTAL | ||||||||||||||||||||||||||||
| 244 | size NUMBER :array | ||||||||||||||||||||||||||||
| 245 | </section> | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =head2 $cfg = new Config::Parser(%hash) | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | Creates a new parser object. Keyword arguments are: | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | =over 4 | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | =item B<filename> | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | Name of the file to parse. If supplied, the constructor will call | ||||||||||||||||||||||||||||
| 258 | the B<parse> and B<commit> methods automatically and will croak if | ||||||||||||||||||||||||||||
| 259 | the latter returns false. The B<parse> method is given B<filename>, | ||||||||||||||||||||||||||||
| 260 | B<line> and B<fh> keyword-value pairs (if present) as its arguments. | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | If not supplied, the caller is supposed to call both methods later. | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | =item B<line> | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | Optional line where the configuration starts in B<filename>. It is used to | ||||||||||||||||||||||||||||
| 267 | keep track of statement location in the file for correct diagnostics. If | ||||||||||||||||||||||||||||
| 268 | not supplied, B<1> is assumed. | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | Valid only together with B<filename>. | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | =item B<fh> | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | File handle to read from. If it is not supplied, new handle will be | ||||||||||||||||||||||||||||
| 275 | created by using B<open> on the supplied filename. | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | Valid only together with B<filename>. | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | =item B<lexicon> | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | Dictionary of allowed configuration statements in the file. You will not | ||||||||||||||||||||||||||||
| 282 | need this parameter. It is listed here for completeness sake. Refer to | ||||||||||||||||||||||||||||
| 283 | the L<Config::AST> constructor for details. | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | =back | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | =head1 USER HOOKS | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | These are the methods provided for implementers to do any implementation- | ||||||||||||||||||||||||||||
| 290 | specific tasks. Default implementations are empty placeholders. | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | =head2 $cfg->init | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | Called after creation of the base object, when parsing of the syntax | ||||||||||||||||||||||||||||
| 295 | definition has finished. Implementers can use it to do any | ||||||||||||||||||||||||||||
| 296 | implementation-specific initialization. | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | =head2 $cfg->mangle | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | Called after successful parsing. It can be used to modify the created | ||||||||||||||||||||||||||||
| 301 | source tree. | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | =head1 PARSER METHODS | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | The following two methods are derived from L<Config::AST>. They are | ||||||||||||||||||||||||||||
| 306 | called internally by the constructor, if the file name is supplied. | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | =head2 $cfg->parse($filename, %opts) | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | Parses the configuration from B<$filename>. Optional arguments are: | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | =over 4 | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | =item B<fh> | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | File handle to read from. If it is not supplied, new handle will be | ||||||||||||||||||||||||||||
| 317 | created by using B<open> on the supplied filename. | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | =item B<line> | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | Line to start numbering of lines from. It is used to keep track of | ||||||||||||||||||||||||||||
| 322 | statement location in the file for correct diagnostics. If not supplied, | ||||||||||||||||||||||||||||
| 323 | B<1> is assumed. | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | =back | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | =head2 $cfg->commit | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | Finalizes the syntax tree. Returns true on success, and false on errors. | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | =head1 SYNTAX DEFINITION | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | Syntax definition is a textual description of statements allowed in | ||||||||||||||||||||||||||||
| 334 | a configuration file. It is written in the format of the configuration | ||||||||||||||||||||||||||||
| 335 | file itself and is parsed using the same object (derivative of | ||||||||||||||||||||||||||||
| 336 | B<Config::Parser>) that will be used later to parse the actual configuration. | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | Syntax definitions are gathered from the B<__DATA__> blocks of | ||||||||||||||||||||||||||||
| 339 | subclasses of B<Config::Parser>. | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | In a syntax definition the value of each statement consists of optional | ||||||||||||||||||||||||||||
| 342 | data type followed by zero or more options delimited with whitespace. | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | Valid data types are: | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | =over 4 | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | =item B<STRING> | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | String value. | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | =item B<NUMBER> or B<DECIMAL> | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | Decimal number. | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | =item B<OCTAL> | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | Octal number. | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | =item B<HEX> | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | Hex number. | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | =item B<BOOL> or B<BOOLEAN> | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | Boolean value. Allowed values are: | ||||||||||||||||||||||||||||
| 367 | B<yes>, B<true>, B<on>, B<t>, B<1>, for C<true> and | ||||||||||||||||||||||||||||
| 368 | B<no>, B<false>, B<off>, B<nil>, B<0>, for C<false>. | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | =back | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | If the data type is omitted, no checking is performed unless specified | ||||||||||||||||||||||||||||
| 373 | otherwise by other options (see the B<:re> and B<:check> options below). | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | Options are special names prefixed with a colon. Option names follow | ||||||||||||||||||||||||||||
| 376 | the keywords from the L<Config::AST> keyword lexicon value. An option | ||||||||||||||||||||||||||||
| 377 | can be followed by an equals sign and its value. If an option is used | ||||||||||||||||||||||||||||
| 378 | without arguments, the value B<1> is implied. | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | Any word not recognized as an option or its value starts the I<default | ||||||||||||||||||||||||||||
| 381 | value>. | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | Available options are described below: | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | =over 4 | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | =item B<:mandatory> | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | Marks the statement as a mandatory one. If such a statement is missing from | ||||||||||||||||||||||||||||
| 390 | the configuration file, the parser action depends on whether the default value | ||||||||||||||||||||||||||||
| 391 | is supplied. If it is, the statement will be inserted in the parse tree with | ||||||||||||||||||||||||||||
| 392 | the default value. Otherwise, a diagnostic message will be printed and the | ||||||||||||||||||||||||||||
| 393 | constructor will return B<undef>. | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | =item B<:default> | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | Argument supplies the default value for this setting. | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | =item B<:array> | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | If the value is 1, declares that the statement is an array. Multiple | ||||||||||||||||||||||||||||
| 402 | occurrences of the statement will be accumulated. They can be retrieved as | ||||||||||||||||||||||||||||
| 403 | a reference to an array when the parsing is finished. | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | =item B<:re = >I<string> | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | Defines a regular expression which the value must match in order to be | ||||||||||||||||||||||||||||
| 408 | accepted. This provides a more elaborate mechanism of checking than the | ||||||||||||||||||||||||||||
| 409 | data types. In fact, data types are converted to the appropriate B<:re> | ||||||||||||||||||||||||||||
| 410 | options internally, for example B<OCTAL> becomes B<:re = "^[0-7]+$">. | ||||||||||||||||||||||||||||
| 411 | If data type and B<:re> are used together, B<:re> takes precedence. | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | =item B<:select = >I<method> | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | Argument is the name of a method to call in order to decide | ||||||||||||||||||||||||||||
| 416 | whether to apply this definition. The method will be called as | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | $cfg->{ \$method }($node, @path) | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | where $node is the B<Config::AST::Node::Value> object (use | ||||||||||||||||||||||||||||
| 421 | B<$vref-E<gt>value>, to obtain the actual value), and B<@path> is its pathname. | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | =item B<:check = >I<method> | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | Argument is the name of a method which will be invoked after parsing the | ||||||||||||||||||||||||||||
| 426 | statement in order to verify its value. This provides the most flexible | ||||||||||||||||||||||||||||
| 427 | way of verification (the other two being the B<:re> option and data type | ||||||||||||||||||||||||||||
| 428 | declaration). The method will be invoked as follows: | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | $cfg->{ \$method }($valref, $prev_value, $locus) | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | where B<$valref> is a reference to the value, and B<$prev_value> is the | ||||||||||||||||||||||||||||
| 433 | value of the previous instance of this setting. The method must return | ||||||||||||||||||||||||||||
| 434 | B<true>, if the value is OK for that setting. In that case, it is allowed | ||||||||||||||||||||||||||||
| 435 | to modify the value referenced by B<$valref>. If the value is erroneous, | ||||||||||||||||||||||||||||
| 436 | the method must issue an appropriate error message using B<$cfg-E<gt>error>, | ||||||||||||||||||||||||||||
| 437 | and return 0. | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | =back | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | To specify options for a section, use the reserved keyword B<__options__>. | ||||||||||||||||||||||||||||
| 442 | Its value is the list of options as described above. After processing, the | ||||||||||||||||||||||||||||
| 443 | keyword itself is removed from the lexicon. | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | =head1 OTHER METHODS | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | =head2 $cfg->check($valref, $prev, $locus) | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | This method implements syntax checking and translation for C<BOOLEAN> data | ||||||||||||||||||||||||||||
| 450 | types. If B<$$valref> is one of the valid boolean values (as described | ||||||||||||||||||||||||||||
| 451 | above), it translates it to B<1> or B<0>, stores that value in B<$valref>, | ||||||||||||||||||||||||||||
| 452 | and returns 1. Otherwise, it emits error message using B<$cfg->error> and | ||||||||||||||||||||||||||||
| 453 | returns 0. | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | L<Config::AST>(3). | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | =cut | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||