.\" Automatically generated by Pod::Man 4.07 (Pod::Simple 3.32) .\" .\" 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 .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . 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 .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "Parse::PlainConfig::Legacy 3pm" .TH Parse::PlainConfig::Legacy 3pm "2016-10-16" "perl v5.24.1" "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" Parse::PlainConfig::Legacy \- Parsing engine Legacy for Parse::PlainConfig .SH "VERSION" .IX Header "VERSION" \&\f(CW$Id:\fR lib/Parse/PlainConfig/Legacy.pm, 3.03 2016/08/09 16:11:12 acorliss Exp $ .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Parse::PlainConfig::Legacy; \& \& $conf = new Parse::PlainConfig::Legacy; \& $conf = Parse::PlainConfig\->new( \& \*(AqPARAM_DELIM\*(Aq => \*(Aq=\*(Aq, \& \*(AqFILE\*(Aq => \*(Aq.myrc\*(Aq, \& \*(AqMAX_BYTES\*(Aq => 65536, \& \*(AqSMART_PARSER => 1, \& ); \& \& $conf\->property(PARAM_DELIM => \*(Aq=\*(Aq); \& \& $rv = $conf\->read(\*(Aqmyconf.conf\*(Aq); \& $rv = $conf\->read; \& $rv = $conf\->readIfNewer; \& $conf\->write(\*(Aq.myrc\*(Aq, 2); \& \& $conf\->purge; \& \& @parameters = $conf\->parameters; \& $conf\->parameter(FOO => "bar"); \& $value = $conf\->parameter(FOO); \& $conf\->describe(FOO => \*(AqThis is foo\*(Aq); \& $conf\->coerce("string", qw(FOO BAR)); \& \& @order = $conf\->order; \& $conf\->order(@new_order); \& \& $errstr = Parse::PlainConfig::Parse::PlainConfig::Legacy::ERROR; \& \& $rv = $conf\->hasParameter(\*(AqFOO\*(Aq); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Parse::PlainConfig::Legacy provides \s-1OO\s0 objects which can parse and generate human-readable configuration files. .SH "SUBROUTINES/METHODS" .IX Header "SUBROUTINES/METHODS" .SS "new" .IX Subsection "new" .Vb 7 \& $conf = new Parse::PlainConfig; \& $conf = Parse::PlainConfig\->new( \& \*(AqPARAM_DELIM\*(Aq => \*(Aq=\*(Aq, \& \*(AqFILE\*(Aq => \*(Aq.myrc\*(Aq, \& \*(AqMAX_BYTES\*(Aq => 65536, \& \*(AqSMART_PARSER => 1, \& ); .Ve .PP The object constructor can be called with or without arguments. Arguments available for use include: .PP .Vb 10 \& Argument Default Purpose \& ============================================================= \& ORDER [] Specifies specific order of \& fields to be used while writing \& FILE undef Filename for read/write ops \& PARAM_DELIM \*(Aq:\*(Aq Field/value delimiter \& LIST_DELIM \*(Aq,\*(Aq List delimiter within field values \& HASH_DELIM \*(Aq=>\*(Aq Hash key/value delimiter within \& field values \& AUTOPURGE 0 Autopurge enabled/disabled \& COERCE {} Field coercion hash \& DEFAULTS {} Default field values \& SMART_PARSER 0 Smart parser enabled/disabled \& MAX_BYTES 16384 Integer denoting maximum bytes \& to read in any given file \& DEFAULTS {} Specifies default values for config \& parameters if not specified/parsed .Ve .PP \&\fB\s-1COERCE\s0\fR is a hash of field name/data type pairs. If a field is listed in this hash then their values will always be returned in the requested format of either string, list, or hash. Any field coerced to string, for instance, will ignore list and hash delimiters and assume the entire value will always be string value. .PP \&\fB\s-1DEFAULTS\s0\fR is a hash of field name/value pairs. This ensures that even if a field is not explicitly set (either in a conf file or programmatically) a default value can still be retrieved. .PP \&\fB\s-1SMART_PARSER\s0\fR removes the need to backslash end-of-lines to continue the value onto the next. If the following line is indented further than the tag was it will automatically assume that the next line is a continuation of the previous. It also affects the need to encapsulate coerced datatypes with quotation marks for irrelevant delimiters. .PP \&\fB\s-1AUTOPURGE\s0\fR erases all stored parameters and values and applies the defaults (if any) before reading a file. This does not, however, erase any values set for \fB\s-1ORDER\s0\fR. .SS "property" .IX Subsection "property" .Vb 1 \& $conf\->property(PARAM_DELIM => \*(Aq=\*(Aq); .Ve .PP This method sets or retrieves the specified property. Please note that this \fBoverwrites\fR the current value, even for those properties that are references to lists and hashes. .PP If you're using this to set a property it will return a boolean true or false depending on the success of the operation. If you're just retrieving a property it will return the value of the property. If you ask for a nonexistent property it will \fBcroak\fR. .PP \&\fB\s-1NOTE:\s0\fR As of version 2.07 all hashes and lists are copied both in and out of the object, so any alterations to a referenced structure retrieved will have no effect on the property within the object. .SS "purge" .IX Subsection "purge" .Vb 1 \& $conf\->purge; .Ve .PP This method performs an immediate manual purge. Auto-purge mode clears the configuration hash each time a configuration file is read, so that the internal configuration data consists solely of what is in that file. If you wanted to combine the settings of multiple files that each may exclusively hold some directives, setting this to 'off' will load the combined configuration as you read each file. .PP You can still clobber configuration values, of course, if the same directive is defined in multiple files. In that case, the last file's value will be the one stored in the hash. .PP This does not clear the \fBorder\fR or \fBcoerce\fR properties. .PP Autopurge mode is disabled by default. .SS "read" .IX Subsection "read" .Vb 2 \& $rv = $conf\->read(\*(Aqmyconf.conf\*(Aq); \& $rv = $conf\->read; .Ve .PP The read method is called initially with a filename as the only argument. This causes the parser to read the file and extract all of the configuration directives from it. .PP You'll notice that you can also call the read method without an argument. The name of the file read is stored internally, and if already set to a valid value (either by a previous call to \fBread\fR with a filename argument or by setting the \fB\s-1FILE\s0\fR property) this will read that file's contents. .PP The return value will be one if the file was successfully read and parsed, or zero otherwise. The reason for failure can be read via \&\fBParse::PlainConfig::Parse::PlainConfig::Legacy::ERROR\fR. .PP This function will cause the program to croak if called without a filename ever being defined. .SS "readIfNewer" .IX Subsection "readIfNewer" .Vb 1 \& $rv = $conf\->readIfNewer; .Ve .PP This method is used to reread & parse the file only if the mtime appears newer than when last read. If the file was successfully reread or appears to be the same it will return true. Any errors will be stored in \&\fBParse::PlainConfig::Legacy::ERROR\fR and it will return a false value. .PP You can determine whether or not the file was read by the true value. If it was re-read it will return 1. If the file appears to be the same age it will return a 2. .SS "write" .IX Subsection "write" .Vb 1 \& $conf\->write(\*(Aq.myrc\*(Aq, 2); .Ve .PP This method writes the current configuration stored in memory to the specified file, either specified as the first argument, or as stored from an explicit or implicit \fBread\fR call. .PP The second argument specifies what kind of whitespace padding, if any, to use with the directive/value delimiter. The following values are recognised: .PP .Vb 6 \& Value Meaning \& ================================================ \& 0 No padding (i.e., written as KEY:VALUE) \& 1 Left padding (i.e., written as KEY :VALUE) \& 2 Right padding (i.e., written as KEY: VALUE) \& 3 Full padding (i.e., written as KEY : VALUE) .Ve .PP Both arguments are optional. .SS "parameters" .IX Subsection "parameters" .Vb 1 \& @parameters = $conf\->parameters; .Ve .PP This method returns a list of all the names of the directives currently stored in the configuration hash in no particular order. .SS "parameter" .IX Subsection "parameter" .Vb 6 \& $value = $conf\->parameter(\*(AqSCALAR1\*(Aq); \& @values = $conf\->parameter(\*(AqLIST1\*(Aq); \& %values = $conf\->parameter(\*(AqHASH1\*(Aq); \& $conf\->parameter(\*(AqSCALAR1\*(Aq, "foo"); \& $conf\->parameter(\*(AqLIST1\*(Aq, [qw(foo bar)]); \& $conf\->parameter(\*(AqHASH1\*(Aq, { foo => \*(Aqbar\*(Aq }); .Ve .PP This method sets or retrieves the specified parameter. Hash and list values are copied and returned as a list. If the specified parameter is set to be coerced into a specific data type the specified value will be converted to that datatype. This means you can do something like: .PP .Vb 3 \& # SCALAR1 will equal "foo , bar , roo" assuming LIST_DELIM is set to \*(Aq,\*(Aq \& $conf\->coerce(qw(string SCALAR1)); \& $conf\->parameter(\*(AqSCALAR1\*(Aq, [qw(foo bar roo)]); \& \& # SCALAR1 will equal "foo => bar : roo => \*(Aq\*(Aq" assuming HASH_DELIM is set \& # to \*(Aq=>\*(Aq and LIST_DELIM is set to \*(Aq:\*(Aq \& $conf\->parameter(\*(AqSCALAR1\*(Aq, { \*(Aqfoo\*(Aq => \*(Aqbar\*(Aq, \*(Aqroo\*(Aq => \*(Aq\*(Aq }); .Ve .PP In order for conversions to be somewhat predictable (in the case of hashes coerced into other values) hash key/value pairs will be assigned to string or list portions according to the alphabetic sort order of the keys. .SS "coerce" .IX Subsection "coerce" .Vb 1 \& $conf\->coerce("string", "FOO", "BAR"); .Ve .PP This method configures the parser to coerce values into the specified datatype (either string, list, or hash) and immediately convert any existing values and store them into that datatype as well. .PP \&\fB\s-1NOTE:\s0\fR Coercing existing values into another data type can provide for some interesting conversions. Strings, for instance, are split on the list delimiter when converting to arrays, and similarly on list and hash delimiters for hashes. Going from a hash or list to a string is done in the opposite manner, elements/key\-value pairs are joined with the applicable delimiters and concatenated into a string. .PP For this reason one should try to avoid coercing one data type into another if you can avoid it. Instead one should predefine what the data types for each parameter should be and define that in the \s-1COERCE\s0 hash passed during object instantiation, or via this method prior to reading and parsing a file. .SS "describe" .IX Subsection "describe" .Vb 1 \& $conf\->describe(KEY1 => \*(AqThis is foo\*(Aq, KEY2 => \*(AqThis is bar\*(Aq); .Ve .PP The describe method takes any number of key/description pairs which will be used as comments preceding the directives in any newly written conf file. You are responsible for prepending a comment character to each line, as well as splitting along your desired text width. .SS "order" .IX Subsection "order" .Vb 2 \& @order = $conf\->order; \& $conf\->order(@new_order); .Ve .PP This method returns the current order of the configuration directives as read from the file. If called with a list as an argument, it will set the directive order with that list. This method is probably of limited use except when you wish to control the order in which directives are written in new conf files. .PP Please note that if there are more directives than are present in this list, those extra keys will still be included in the new file, but will appear in alphabetically sorted order at the end, after all of the keys present in the list. .SS "hasParameter" .IX Subsection "hasParameter" .Vb 1 \& $rv = $conf\->hasParameter(\*(AqFOO\*(Aq); .Ve .PP This function allows you to see if a parameter has been defined or has a default set for it. Returns a boolean value. .SH "DEPRECATED METHODS" .IX Header "DEPRECATED METHODS" .SS "delim" .IX Subsection "delim" .Vb 1 \& $conf\->delim(\*(Aq=\*(Aq); .Ve .PP This method gets and/or sets the parameter name/value delimiter to be used in the conf files. The default delimiter is ':'. This can be multiple characters. .SS "directives" .IX Subsection "directives" .Vb 1 \& @directives = $conf\->directives; .Ve .PP This method returns a list of all the names of the directives currently stored in the configuration hash in no particular order. .SS "get" .IX Subsection "get" .Vb 2 \& $field = $conf\->get(\*(AqKEY1\*(Aq); \& ($field1, $field2) = $conf\->get(qw(KEY1 KEY2)); .Ve .PP The get method takes any number of directives to retrieve, and returns them. Please note that both hash and list values are passed by reference. In order to protect the internal state information, the contents of either reference is merely a copy of what is in the configuration object's hash. This will \fBnot\fR pass you a reference to data stored internally in the object. Because of this, it's perfectly safe for you to shift off values from a list as you process it, and so on. .SS "set" .IX Subsection "set" .Vb 1 \& $conf\->set(KEY1 => \*(Aqfoo\*(Aq, KEY2 => \*(Aqbar\*(Aq); .Ve .PP The set method takes any number of directive/value pairs and copies them into the internal configuration hash. .SS "get_ref" .IX Subsection "get_ref" .Vb 1 \& $href = $conf\->get_ref .Ve .PP \&\fBNote\fR: This used to give you a reference to the internal configuration hash so you could manipulate it directly. It now only gives you a \fBcopy\fR of the internal hash (actually, it's reconstructed has to make it look like the old data structure). In short, any changes you make to this hash \fBwill be lost\fR. .SS "error" .IX Subsection "error" .Vb 1 \& warn $conf\->error; .Ve .PP This method returns a zero-length string if no errors were registered with the last operation, or a text message describing the error. .SS "\s-1ERROR\s0" .IX Subsection "ERROR" .Vb 1 \& $error = Parse::PlainConfig::ERROR(); .Ve .PP Lvalue subroutine storing the last error which may have occurred. .SH "DEPENDENCIES" .IX Header "DEPENDENCIES" .IP "o" 4 .IX Item "o" Paranoid .IP "o" 4 .IX Item "o" Text::ParseWords .IP "o" 4 .IX Item "o" Text::Tabs .SH "FILE SYNTAX" .IX Header "FILE SYNTAX" .SS "\s-1TRADITIONAL USAGE\s0" .IX Subsection "TRADITIONAL USAGE" The plain parser supports the reconstructions of relatively simple data structures. Simple string assignments and one-dimensional arrays and hashes are possible. Below are are various examples of constructs: .PP .Vb 3 \& # Scalar assignment \& FIRST_NAME: Joe \& LAST_NAME: Blow \& \& # Array assignment \& FAVOURITE_COLOURS: red, yellow, green \& ACCOUNT_NUMBERS: 9956\-234\-9943211, \e \& 2343232\-421231445, \e \& 004422\-03430\-0343 \& \& # Hash assignment \& CARS: crown_vic => 1982, \e \& geo => 1993 .Ve .PP As the example above demonstrates, all lines that begin with a '#' (leading whitespace is allowed) are ignored as comments. if '#" occurs in any other position, it is accepted as part of the passed value. This means that you \&\fBcannot\fR place comments on the same lines as values. .PP All directives and associated values will have both leading and trailing whitespace stripped from them before being stored in the configuration hash. Whitespace is allowed within both. .PP In traditional mode (meaning no parameters are set to be coerced into a specific datatype) one must encapsulate list and hash delimiters with quotation marks in order to prevent the string from being split and stored as a list or hash. Quotation marks that are a literal part of the string must be backslashed. .SS "\s-1SMART PARSER\s0" .IX Subsection "SMART PARSER" The new parser now provides some options to make the file syntax more convenient. You can activate the smart parser by setting \fB\s-1SMART_PARSER\s0\fR to a true value during object instantiation or via the \fBproperty\fR method. .PP With the traditional parser you had to backslach the end of all preceding lines if you wanted to split a value into more than one line: .PP .Vb 2 \& FOO: This line starts here \e \& and ends here... .Ve .PP With the smart parser enabled that is no longer necessary as long as the following lines are indented further than the first line: .PP .Vb 2 \& FOO: This line starts here \& and ends here... .Ve .PP \&\fBNote:\fR The indentation is compared by byte count with no recognition of tab stops. That means if you indent with spaces on the first line and indent with tabs on the following it may not concantenate those values. .PP Another benefit of the smart parser is found when you specify a parameter to be of a specific datatype via the \fB\s-1COERCE\s0\fR hash during object instantiation or the \fBcoerce\fR method. For instance, the traditional parser requires you to encapsulate strings with quotation marks if they contain list or hash delimiters: .PP .Vb 1 \& Quote: "\e"It can\*(Aqt be that easy,\e" he said." .Ve .PP Also note how you had to escape quotation marks if they were to be a literal part of the string. With this parameter set to be coerced to a scalar you can simply write: .PP .Vb 1 \& Quote: "It can\*(Aqt be that easy," he said. .Ve .PP Similarly, you don't have to quote hash delimiters in parameters set to be coerced into lists. Quotation marks as part of an element value must be escaped, though, since unescaped quotation marks are assumed to encapsulate strings containing list delimiters you don't want to split on. .PP \&\fBNote:\fR The previous versions of Parse::PlainConfig did not allow the user to set keys like: .PP .Vb 2 \& FOO: \e \& bar .Ve .PP or save empty assignments like .PP .Vb 1 \& FOO: .Ve .PP This is no longer the case. Both are now valid and honoured. .SH "SECURITY" .IX Header "SECURITY" \&\fB\s-1WARNING:\s0\fR This parser will attempt to open what ever you pass to it for a filename as is. If this object is to be used in programs that run with permissions other than the calling user, make sure you sanitize any user-supplied filename strings before passing them to this object. .PP This also uses a blocking b call to open the file for reading and writing. .SH "DIAGNOSTICS" .IX Header "DIAGNOSTICS" Through the use of \fBParanoid::Debug\fR this module will produce internal diagnostic output to \s-1STDERR. \s0 It begins logging at log level 7. To enable debugging output please see the pod for Paranoid::Debug. .SH "BUGS AND LIMITATIONS" .IX Header "BUGS AND LIMITATIONS" .SH "AUTHOR" .IX Header "AUTHOR" Arthur Corliss (corliss@digitalmages.com) .SH "LICENSE AND COPYRIGHT" .IX Header "LICENSE AND COPYRIGHT" This software is licensed under the same terms as Perl, itself. Please see http://dev.perl.org/licenses/ for more information. .PP (c) 2002 \- 2016, Arthur Corliss (corliss@digitalmages.com)