|
NAMEConfig::Grammar - A grammar-based, user-friendly config parserSYNOPSISuse Config::Grammar; my $parser = Config::Grammar->new(\%grammar); my $cfg = $parser->parse('app.cfg') or die "ERROR: $parser->{err}\n"; my $pod = $parser->makepod(); my $ex = $parser->maketmpl('TOP','SubNode'); my $minex = $parser->maketmplmin('TOP','SubNode'); DESCRIPTIONConfig::Grammar is a module to parse configuration files. The configuration may consist of multiple-level sections with assignments and tabular data. The parsed data will be returned as a hash containing the whole configuration. Config::Grammar uses a grammar that is supplied upon creation of a Config::Grammar object to parse the configuration file and return helpful error messages in case of syntax errors. Using the makepod method you can generate documentation of the configuration file format.The maketmpl method can generate a template configuration file. If your grammar contains regexp matches, the template will not be all that helpful as Config::Grammar is not smart enough to give you sensible template data based in regular expressions. The related function maketmplmin generates a minimal configuration template without examples, regexps or comments and thus allows an experienced user to fill in the configuration data more efficiently. Grammar DefinitionThe grammar is a multiple-level hash of hashes, which follows the structure of the configuration. Each section or variable is represented by a hash with the same structure. Each hash contains special keys starting with an underscore such as '_sections', '_vars', '_sub' or '_re' to denote meta data with information about that section or variable. Other keys are used to structure the hash according to the same nesting structure of the configuration itself. The starting hash given as parameter to 'new' contains the "root section".Special Section Keys
Special Variable Keys
Special Table Keys
Special Text Keys
Configuration SyntaxGeneral Syntax'#' denotes a comment up to the end-of-line, empty lines are allowed and space at the beginning and end of lines is trimmed. '\' at the end of the line marks a continued line on the next line. A single space will be inserted between the concatenated lines. '@include filename' is used to include another file. Include works relative to the directory where the parent file is in. '@define a some value' will replace all occurences of 'a' in the following text with 'some value'. Fields in tables that contain white space can be enclosed in either "'" or """. Whitespace can also be escaped with "\". Quotes inside quotes are allowed but must be escaped with a backslash as well. Sections Config::Grammar supports hierarchical configurations through sections, whose syntax is as follows:
Assignments Assignements take the form: 'variable = value', where value can be any string (can contain whitespaces and special characters). The spaces before and after the equal sign are optional. Tabular Data The data is interpreted as one or more columns separated by spaces. ExampleCodeuse Data::Dumper; use Config::Grammar; my $RE_IP = '\d+\.\d+\.\d+\.\d+'; my $RE_MAC = '[0-9a-f]{2}(?::[0-9a-f]{2}){5}'; my $RE_HOST = '\S+'; my $parser = Config::Grammar->new({ _sections => [ 'network', 'hosts' ], network => { _vars => [ 'dns' ], _sections => [ "/$RE_IP/" ], dns => { _doc => "address of the dns server", _example => "ns1.oetiker.xs", _re => $RE_HOST, _re_error => 'dns must be an host name or ip address', }, "/$RE_IP/" => { _doc => "Ip Adress", _example => '10.2.3.2', _vars => [ 'netmask', 'gateway' ], netmask => { _doc => "Netmask", _example => "255.255.255.0", _re => $RE_IP, _re_error => 'netmask must be a dotted ip address' }, gateway => { _doc => "Default Gateway address in IP notation", _example => "10.22.12.1", _re => $RE_IP, _re_error => 'gateway must be a dotted ip address' }, }, }, hosts => { _doc => "Details about the hosts", _table => { _doc => "Description of all the Hosts", _key => 0, _columns => 3, 0 => { _doc => "Ethernet Address", _example => "0:3:3:d:a:3:dd:a:cd", _re => $RE_MAC, _re_error => 'first column must be an ethernet mac address', }, 1 => { _doc => "IP Address", _example => "10.11.23.1", _re => $RE_IP, _re_error => 'second column must be a dotted ip address', }, 2 => { _doc => "Host Name", _example => "tardis", }, }, }, }); my $cfg = $parser->parse('test.cfg') or die "ERROR: $parser->{err}\n"; print Dumper($cfg); print $parser->makepod; Configuration *** network *** dns = 192.168.7.87 + 192.168.7.64 netmask = 255.255.255.192 gateway = 192.168.7.65 *** hosts *** 00:50:fe:bc:65:11 192.168.7.97 plain.hades 00:50:fe:bc:65:12 192.168.7.98 isg.ee.hades 00:50:fe:bc:65:14 192.168.7.99 isg.ee.hades Result { 'hosts' => { '00:50:fe:bc:65:11' => [ '00:50:fe:bc:65:11', '192.168.7.97', 'plain.hades' ], '00:50:fe:bc:65:12' => [ '00:50:fe:bc:65:12', '192.168.7.98', 'isg.ee.hades' ], '00:50:fe:bc:65:14' => [ '00:50:fe:bc:65:14', '192.168.7.99', 'isg.ee.hades' ] }, 'network' => { '192.168.7.64' => { 'netmask' => '255.255.255.192', 'gateway' => '192.168.7.65' }, 'dns' => '192.168.7.87' } }; SEE ALSOConfig::Grammar::DynamicCOPYRIGHTCopyright (c) 2000-2005 by ETH Zurich. All rights reserved. Copyright (c) 2007 by David Schweikert. All rights reserved.LICENSEThis program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.AUTHORSDavid Schweikert, Tobias Oetiker, Niko Tyni
Visit the GSP FreeBSD Man Page Interface. |