.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Required to disable full justification in groff 1.23.0. .if n .ds AD l .\" ======================================================================== .\" .IX Title "Config::Grammar 3" .TH Config::Grammar 3 2025-12-14 "perl v5.42.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Config::Grammar \- A grammar\-based, user\-friendly config parser .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use Config::Grammar; \& \& my $args = { encoding => \*(Aqutf8\*(Aq }; # the second parameter to parse() is optional \& my $parser = Config::Grammar\->new(\e%grammar); \& my $cfg = $parser\->parse(\*(Aqapp.cfg\*(Aq, $args) or die "ERROR: $parser\->{err}\en"; \& my $pod = $parser\->makepod(); \& my $ex = $parser\->maketmpl(\*(AqTOP\*(Aq,\*(AqSubNode\*(Aq); \& my $minex = $parser\->maketmplmin(\*(AqTOP\*(Aq,\*(AqSubNode\*(Aq); .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Config::Grammar is a module to parse configuration files. The optional second parameter to the \fBparse()\fR method can be used to specify the file encoding to use for opening the file (see documentation for Perl\*(Aqs use open pragma). .PP 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 \fBmakepod\fR method you can generate documentation of the configuration file format. .PP The \fBmaketmpl\fR 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 \&\fBmaketmplmin\fR generates a minimal configuration template without examples, regexps or comments and thus allows an experienced user to fill in the configuration data more efficiently. .SS "Grammar Definition" .IX Subsection "Grammar Definition" The 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 \*(Aq_sections\*(Aq, \*(Aq_vars\*(Aq, \*(Aq_sub\*(Aq or \*(Aq_re\*(Aq 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 \*(Aqnew\*(Aq contains the "root section". .PP \fISpecial Section Keys\fR .IX Subsection "Special Section Keys" .IP _sections 12 .IX Item "_sections" Array containing the list of sub\-sections of this section. Each sub\-section must then be represented by a sub\-hash in this hash with the same name of the sub\-section. .Sp The sub\-section can also be a regular expression denoted by the syntax \*(Aq/re/\*(Aq, where re is the regular\-expression. In case a regular expression is used, a sub\-hash named with the same \*(Aq/re/\*(Aq must be included in this hash. .IP _vars 12 .IX Item "_vars" Array containing the list of variables (assignments) in this section. Analogous to sections, regular expressions can be used. .IP _mandatory 12 .IX Item "_mandatory" Array containing the list of mandatory sections and variables. .IP _inherited 12 .IX Item "_inherited" Array containing the list of the variables that should be assigned the same value as in the parent section if nothing is specified here. .IP _table 12 .IX Item "_table" Hash containing the table grammar (see Special Table Keys). If not specified, no table is allowed in this section. The grammar of the columns if specified by sub\-hashes named with the column number. .IP _text 12 .IX Item "_text" Section contains free\-form text. Only sections and \f(CW@includes\fR statements will be interpreted, the rest will be added in the returned hash under \*(Aq_text\*(Aq as string. .Sp \&\fB_text\fR is a hash reference which can contain a \fB_re\fR and a \fB_re_error\fR key which will be used to scrutanize the text ... if the hash is empty, all text will be accepted. .IP _order 12 .IX Item "_order" If defined, a \*(Aq_order\*(Aq element will be put in every hash containing the sections with a number that determines the order in which the sections were defined. .IP _doc 12 .IX Item "_doc" Describes what this section is about .IP _sub 12 .IX Item "_sub" A function pointer. It is called for every instance of this section, with the real name of the section passed as its first argument. This is probably only useful for the regexp sections. If the function returns a defined value it is assumed that the test was not successful and an error is generated with the returned string as content. .PP \fISpecial Variable Keys\fR .IX Subsection "Special Variable Keys" .IP _re 12 .IX Item "_re" Regular expression upon which the value will be checked. .IP _re_error 12 .IX Item "_re_error" String containing the returned error in case the regular expression doesn\*(Aqt match (if not specified, a generic \*(Aqsyntax error\*(Aq message will be returned). .IP _sub 12 .IX Item "_sub" A function pointer. It called for every value, with the value passed as its first argument. If the function returns a defined value it is assumed that the test was not successful and an error is generated with the returned string as content. .Sp If the \*(Aq_varlist\*(Aq key (see above) is defined in this section, the \*(Aq_sub\*(Aq function will also receive an array reference as the second argument. The array contains a list of those variables already defined in the same section. This can be used to enforce the order of the variables. .IP _default 12 .IX Item "_default" A default value that will be assigned to the variable if none is specified or inherited. .IP _doc 12 .IX Item "_doc" Description of the variable. .IP _example 12 .IX Item "_example" A one line example for the content of this variable. .PP \fISpecial Table Keys\fR .IX Subsection "Special Table Keys" .IP _columns 12 .IX Item "_columns" Number of columns. If not specified, it will not be enforced. .IP _key 12 .IX Item "_key" If defined, the specified column number will be used as key in a hash in the returned hash. If not defined, the returned hash will contain a \*(Aq_table\*(Aq element with the contents of the table as array. The rows of the tables are stored as arrays. .IP _sub 12 .IX Item "_sub" they work analog to the description in the previous section. .IP _doc 12 .IX Item "_doc" describes the content of the column. .IP _example 12 .IX Item "_example" example for the content of this column .PP \fISpecial Text Keys\fR .IX Subsection "Special Text Keys" .IP _re 12 .IX Item "_re" Regular expression upon which the text will be checked (everything as a single line). .IP _re_error 12 .IX Item "_re_error" String containing the returned error in case the regular expression doesn\*(Aqt match (if not specified, a generic \*(Aqsyntax error\*(Aq message will be returned). .IP _sub 12 .IX Item "_sub" they work analog to the description in the previous section. .IP _doc 12 .IX Item "_doc" Ditto. .IP _example 12 .IX Item "_example" Potential multi line example for the content of this text section .SS "Configuration Syntax" .IX Subsection "Configuration Syntax" \fIGeneral Syntax\fR .IX Subsection "General Syntax" .PP \&\*(Aq#\*(Aq denotes a comment up to the end\-of\-line, empty lines are allowed and space at the beginning and end of lines is trimmed. .PP \&\*(Aq\e\*(Aq at the end of the line marks a continued line on the next line. A single space will be inserted between the concatenated lines. .PP \&\*(Aq@include filename\*(Aq is used to include another file. Include works relative to the directory where the parent file is in. .PP \&\*(Aq@define a some value\*(Aq will replace all occurrences of \*(Aqa\*(Aq in the following text with \*(Aqsome value\*(Aq. .PP Fields in tables that contain white space can be enclosed in either \f(CW\*(C`\*(Aq\*(C'\fR or \f(CW\*(C`"\*(C'\fR. Whitespace can also be escaped with \f(CW\*(C`\e\*(C'\fR. Quotes inside quotes are allowed but must be escaped with a backslash as well. .PP \fISections\fR .IX Subsection "Sections" .PP Config::Grammar supports hierarchical configurations through sections, whose syntax is as follows: .IP "Level 1" 15 .IX Item "Level 1" *** section name *** .IP "Level 2" 15 .IX Item "Level 2" + section name .IP "Level 3" 15 .IX Item "Level 3" ++ section name .IP "Level n, n>1" 15 .IX Item "Level n, n>1" +..+ section name (number of \*(Aq+\*(Aq determines level) .PP \fIAssignments\fR .IX Subsection "Assignments" .PP Assignments take the form: \*(Aqvariable = value\*(Aq, where value can be any string (can contain whitespaces and special characters). The spaces before and after the equal sign are optional. .PP \fITabular Data\fR .IX Subsection "Tabular Data" .PP The data is interpreted as one or more columns separated by spaces. .SS Example .IX Subsection "Example" \fICode\fR .IX Subsection "Code" .PP .Vb 2 \& use Data::Dumper; \& use Config::Grammar; \& \& my $RE_IP = \*(Aq\ed+\e.\ed+\e.\ed+\e.\ed+\*(Aq; \& my $RE_MAC = \*(Aq[0\-9a\-f]{2}(?::[0\-9a\-f]{2}){5}\*(Aq; \& my $RE_HOST = \*(Aq\eS+\*(Aq; \& \& my $parser = Config::Grammar\->new({ \& _sections => [ \*(Aqnetwork\*(Aq, \*(Aqhosts\*(Aq ], \& network => { \& _vars => [ \*(Aqdns\*(Aq ], \& _sections => [ "/$RE_IP/" ], \& dns => { \& _doc => "address of the dns server", \& _example => "ns1.oetiker.xs", \& _re => $RE_HOST, \& _re_error => \& \*(Aqdns must be an host name or ip address\*(Aq, \& }, \& "/$RE_IP/" => { \& _doc => "Ip Adress", \& _example => \*(Aq10.2.3.2\*(Aq, \& _vars => [ \*(Aqnetmask\*(Aq, \*(Aqgateway\*(Aq ], \& netmask => { \& _doc => "Netmask", \& _example => "255.255.255.0", \& _re => $RE_IP, \& _re_error => \& \*(Aqnetmask must be a dotted ip address\*(Aq \& }, \& gateway => { \& _doc => "Default Gateway address in IP notation", \& _example => "10.22.12.1", \& _re => $RE_IP, \& _re_error => \& \*(Aqgateway must be a dotted ip address\*(Aq }, \& }, \& }, \& 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 => \& \*(Aqfirst column must be an ethernet mac address\*(Aq, \& }, \& 1 => { \& _doc => "IP Address", \& _example => "10.11.23.1", \& _re => $RE_IP, \& _re_error => \& \*(Aqsecond column must be a dotted ip address\*(Aq, \& }, \& 2 => { \& _doc => "Host Name", \& _example => "tardis", \& }, \& }, \& }, \& }); \& \& my $args = { encoding => \*(Aqutf8\*(Aq }; # the second parameter to parse() is optional \& my $cfg = $parser\->parse(\*(Aqtest.cfg\*(Aq, $args) or \& die "ERROR: $parser\->{err}\en"; \& print Dumper($cfg); \& print $parser\->makepod; .Ve .PP \fIConfiguration\fR .IX Subsection "Configuration" .PP .Vb 1 \& *** 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 .Ve .PP \fIResult\fR .IX Subsection "Result" .PP .Vb 10 \& { \& \*(Aqhosts\*(Aq => { \& \*(Aq00:50:fe:bc:65:11\*(Aq => [ \& \*(Aq00:50:fe:bc:65:11\*(Aq, \& \*(Aq192.168.7.97\*(Aq, \& \*(Aqplain.hades\*(Aq \& ], \& \*(Aq00:50:fe:bc:65:12\*(Aq => [ \& \*(Aq00:50:fe:bc:65:12\*(Aq, \& \*(Aq192.168.7.98\*(Aq, \& \*(Aqisg.ee.hades\*(Aq \& ], \& \*(Aq00:50:fe:bc:65:14\*(Aq => [ \& \*(Aq00:50:fe:bc:65:14\*(Aq, \& \*(Aq192.168.7.99\*(Aq, \& \*(Aqisg.ee.hades\*(Aq \& ] \& }, \& \*(Aqnetwork\*(Aq => { \& \*(Aq192.168.7.64\*(Aq => { \& \*(Aqnetmask\*(Aq => \*(Aq255.255.255.192\*(Aq, \& \*(Aqgateway\*(Aq => \*(Aq192.168.7.65\*(Aq \& }, \& \*(Aqdns\*(Aq => \*(Aq192.168.7.87\*(Aq \& } \& }; .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" Config::Grammar::Dynamic .SH COPYRIGHT .IX Header "COPYRIGHT" Copyright (c) 2000\-2005 by ETH Zurich. All rights reserved. Copyright (c) 2007 by David Schweikert. All rights reserved. .SH LICENSE .IX Header "LICENSE" This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. .SH AUTHORS .IX Header "AUTHORS" David Schweikert, Tobias Oetiker, Niko Tyni