.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" 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 .\" ======================================================================== .\" .IX Title "Config::Tiny 3" .TH Config::Tiny 3 2023-10-15 "perl v5.38.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::Tiny \- Read/Write .ini style files with as little code as possible .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 2 \& # In your configuration file \& rootproperty=blah \& \& [section] \& one=twp \& greetings[]=Hello \& three= four \& Foo =Bar \& greetings[]=World! \& empty= \& \& # In your program \& use Config::Tiny; \& \& # Create an empty config \& my $Config = Config::Tiny\->new; \& \& # Create a config with data \& my $config = Config::Tiny\->new({ \& _ => { rootproperty => "Bar" }, \& section => { one => "value", Foo => 42 } }); \& \& # Open the config \& $Config = Config::Tiny\->read( \*(Aqfile.conf\*(Aq ); \& $Config = Config::Tiny\->read( \*(Aqfile.conf\*(Aq, \*(Aqutf8\*(Aq ); # Neither \*(Aq:\*(Aq nor \*(Aq<:\*(Aq prefix! \& $Config = Config::Tiny\->read( \*(Aqfile.conf\*(Aq, \*(Aqencoding(iso\-8859\-1)\*(Aq); \& \& # Reading properties \& my $rootproperty = $Config\->{_}\->{rootproperty}; \& my $one = $Config\->{section}\->{one}; \& my $Foo = $Config\->{section}\->{Foo}; \& \& # Changing data \& $Config\->{newsection} = { this => \*(Aqthat\*(Aq }; # Add a section \& $Config\->{section}\->{Foo} = \*(AqNot Bar!\*(Aq; # Change a value \& delete $Config\->{_}; # Delete a value or section \& \& # Save a config \& $Config\->write( \*(Aqfile.conf\*(Aq ); \& $Config\->write( \*(Aqfile.conf\*(Aq, \*(Aqutf8\*(Aq ); # Neither \*(Aq:\*(Aq nor \*(Aq>:\*(Aq prefix! \& \& # Shortcuts \& my($rootproperty) = $$Config{_}{rootproperty}; \& \& my($config) = Config::Tiny \-> read_string(\*(Aqalpha=bet\*(Aq); \& my($value) = $$config{_}{alpha}; # $value is \*(Aqbet\*(Aq. \& \& my($config) = Config::Tiny \-> read_string("[init]\enalpha=bet"); \& my($value) = $$config{init}{alpha}; # $value is \*(Aqbet\*(Aq. .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" \&\f(CW\*(C`Config::Tiny\*(C'\fR is a Perl class to read and write .ini style configuration files with as little code as possible, reducing load time and memory overhead. .PP Most of the time it is accepted that Perl applications use a lot of memory and modules. .PP The \f(CW*::Tiny\fR family of modules is specifically intended to provide an ultralight alternative to the standard modules. .PP This module is primarily for reading human written files, and anything we write shouldn't need to have documentation/comments. If you need something with more power move up to Config::Simple, Config::General or one of the many other \f(CW\*(C`Config::*\*(C'\fR modules. .PP Lastly, Config::Tiny does \fBnot\fR preserve your comments, whitespace, or the order of your config file. .PP See Config::Tiny::Ordered (and possibly others) for the preservation of the order of the entries in the file. .SH "CONFIGURATION FILE SYNTAX" .IX Header "CONFIGURATION FILE SYNTAX" Files are the same format as for MS Windows \f(CW\*(C`*.ini\*(C'\fR files. For example: .PP .Vb 3 \& [section] \& var1=value1 \& var2=value2 .Ve .PP But see also ARRAY SYNTAX just below. .PP If a property is outside of a section at the beginning of a file, it will be assigned to the \f(CW"root section"\fR, available at \f(CW\*(C`$Config\->{_}\*(C'\fR. .PP Lines starting with \f(CW\*(Aq#\*(Aq\fR or \f(CW\*(Aq;\*(Aq\fR are considered comments and ignored, as are blank lines. .PP When writing back to the config file, all comments, custom whitespace, and the ordering of your config file elements are discarded. If you need to keep the human elements of a config when writing back, upgrade to something better, this module is not for you. .SH "ARRAY SYNTAX" .IX Header "ARRAY SYNTAX" .SS "Basic Syntax" .IX Subsection "Basic Syntax" As of V 2.30, this module supports the case of a key having an array of values. .PP Sample data (copied from t/test.conf): .PP .Vb 1 \& root=something \& \& [section] \& greetings[]=Hello \& one=two \& Foo=Bar \& greetings[]=World! \& this=Your Mother! \& blank= \& \& [Section Two] \& something else=blah \& remove = whitespace .Ve .PP Note specifically that the key name greetings has the empty bracket pair [] as a suffix. This tells the code that it is not to overwrite the 1st value with the 2nd value, but rather to push these values onto a stack called 'greetings'. .PP Note also that you could have used: .PP .Vb 7 \& [section] \& greetings[]=Hello \& greetings[]=World! \& one=two \& Foo=Bar \& this=Your Mother! \& blank= .Ve .PP Clearly, the 2 lines using greetings[] do not have to be side-by-side. .PP If you use e.g. Data::Dumper::Concise to give you a \fBDumper()\fR function (not method), then \&'say Dumper($Config)' the output will look like: .PP .Vb 10 \& bless( { \& "Section Two" => { \& remove => "whitespace", \& "something else" => "blah", \& }, \& _ => { \& root => "something", \& }, \& section => { \& Foo => "Bar", \& blank => "", \& greetings => [ \& "Hello", \& "World!", \& ], \& one => "two", \& this => "Your Mother!", \& }, \& }, \*(AqConfig::Tiny\*(Aq ) .Ve .PP You can see this structure in t/02.main.t starting at line 45. Observe too that the key names are reported in alphabetical order (by the module Data::Dumper::Concise) despite the differing order in the setting of these keys, and that the array syntax result is that greetings has an array for a value. .PP To access these values, use code like this: .PP .Vb 6 \& Dumper($Config); \& Dumper($Config\->{section}); \& Dumper($Config\->{section}\->{greetings}); \& Dumper($Config\->{section}\->{greetings}\->[0]); \& Dumper($Config\->{section}\->{greetings}\->[1]); \& Dumper(ref $Config); .Ve .SS Warning .IX Subsection "Warning" \&\f(CW$Config\fR is a blessed value, which means it is accessed differently than if it was a hash ref. The latter could be accessed as: .PP .Vb 1 \& Dumper($$Config{section}{greetings}); # Don\*(Aqt do this for blessed values! .Ve .PP Finally, if a hash ref rather than a blessed value, you could also use, as above: .PP .Vb 1 \& Dumper($Config\->{section}\->{greetings}); # Don\*(Aqt do this for blessed values! .Ve .PP My (Ron Savage) personal preference for hashrefs is the one without the gross '\->' chars, but that requires you to double up the initial $ character (which I hope you noticed!). .SH METHODS .IX Header "METHODS" .SS \fBerrstr()\fP .IX Subsection "errstr()" Returns a string representing the most recent error, or the empty string. .PP You can also retrieve the error message from the \f(CW$Config::Tiny::errstr\fR variable. .SS new([$config]) .IX Subsection "new([$config])" Here, the [] indicate an optional parameter. .PP The constructor \f(CW\*(C`new\*(C'\fR creates and returns a \f(CW\*(C`Config::Tiny\*(C'\fR object. .PP This will normally be a new, empty configuration, but you may also pass a hashref here which will be turned into an object of this class. This hashref should have a structure suitable for a configuration file, that is, a hash of hashes where the key \f(CW\*(C`_\*(C'\fR is treated specially as the root section. .SS "read($filename, [$encoding])" .IX Subsection "read($filename, [$encoding])" Here, the [] indicate an optional parameter. .PP The \f(CW\*(C`read\*(C'\fR constructor reads a config file, \f(CW$filename\fR, and returns a new \&\f(CW\*(C`Config::Tiny\*(C'\fR object containing the properties in the file. .PP \&\f(CW$encoding\fR may be used to indicate the encoding of the file, e.g. 'utf8' or 'encoding(iso\-8859\-1)'. .PP Do not add a prefix to \f(CW$encoding\fR, such as '<' or '<:'. .PP Returns the object on success, or \f(CW\*(C`undef\*(C'\fR on error. .PP When \f(CW\*(C`read\*(C'\fR fails, \f(CW\*(C`Config::Tiny\*(C'\fR sets an error message internally you can recover via \f(CW\*(C`Config::Tiny\->errstr\*(C'\fR. Although in \fBsome\fR cases a failed \f(CW\*(C`read\*(C'\fR will also set the operating system error variable \f(CW$!\fR, not all errors do and you should not rely on using the \f(CW$!\fR variable. .PP See t/04.utf8.t and t/04.utf8.txt. .SS read_string($string) .IX Subsection "read_string($string)" The \f(CW\*(C`read_string\*(C'\fR method takes as argument the contents of a config file as a string and returns the \f(CW\*(C`Config::Tiny\*(C'\fR object for it. .SS "write($filename, [$encoding])" .IX Subsection "write($filename, [$encoding])" Here, the [] indicate an optional parameter. .PP The \f(CW\*(C`write\*(C'\fR method generates the file content for the properties, and writes it to disk to the filename specified. .PP \&\f(CW$encoding\fR may be used to indicate the encoding of the file, e.g. 'utf8' or 'encoding(iso\-8859\-1)'. .PP Do not add a prefix to \f(CW$encoding\fR, such as '>' or '>:'. .PP Returns true on success or \f(CW\*(C`undef\*(C'\fR on error. .PP See t/04.utf8.t and t/04.utf8.txt. .SS \fBwrite_string()\fP .IX Subsection "write_string()" Generates the file content for the object and returns it as a string. .SH FAQ .IX Header "FAQ" .SS "What happens if a key is repeated?" .IX Subsection "What happens if a key is repeated?" Case 1: The last value is retained, overwriting any previous values. .PP See t/06.repeat.key.t for sample code. .PP Case 2: However, by using the new array syntax, as of V 2.30, you can assign a set of values to a key. .PP For details, see the "ARRAY SYNTAX" section above for sample code. .PP See t/test.conf for sample data. .SS "Why can't I put comments at the ends of lines?" .IX Subsection "Why can't I put comments at the ends of lines?" .IP "o The # char is only introduces a comment when it's at the start of a line." 4 .IX Item "o The # char is only introduces a comment when it's at the start of a line." So a line like: .Sp .Vb 1 \& key=value # A comment .Ve .Sp Sets key to 'value # A comment', which, presumably, you did not intend. .Sp This conforms to the syntax discussed in "CONFIGURATION FILE SYNTAX". .IP "o Comments matching /\es\e;\es.+$//g; are ignored." 4 .IX Item "o Comments matching /s;s.+$//g; are ignored." This means you can't preserve the suffix using: .Sp .Vb 1 \& key = Prefix ; Suffix .Ve .Sp Result: key is now 'Prefix'. .Sp But you can do this: .Sp .Vb 1 \& key = Prefix;Suffix .Ve .Sp Result: key is now 'Prefix;Suffix'. .Sp Or this: .Sp .Vb 1 \& key = Prefix; Suffix .Ve .Sp Result: key is now 'Prefix; Suffix'. .PP See t/07.trailing.comment.t. .SS "Why can't I omit the '=' signs?" .IX Subsection "Why can't I omit the '=' signs?" E.g.: .PP .Vb 5 \& [Things] \& my = \& list = \& of = \& things = .Ve .PP Instead of: .PP .Vb 5 \& [Things] \& my \& list \& of \& things .Ve .PP Because the use of '=' signs is a type of mandatory documentation. It indicates that that section contains 4 items, and not 1 odd item split over 4 lines. .SS "Why do I have to assign the result of a method call to a variable?" .IX Subsection "Why do I have to assign the result of a method call to a variable?" This question comes from RT#85386. .PP Yes, the syntax may seem odd, but you don't have to call both \fBnew()\fR and \fBread_string()\fR. .PP Try: .PP .Vb 1 \& perl \-MData::Dumper \-MConfig::Tiny \-E \*(Aqmy $c=Config::Tiny\->read_string("one=s"); say Dumper $c\*(Aq .Ve .PP Or: .PP .Vb 2 \& my($config) = Config::Tiny \-> read_string(\*(Aqalpha=bet\*(Aq); \& my($value) = $$config{_}{alpha}; # $value is \*(Aqbet\*(Aq. .Ve .PP Or even, a bit ridiculously: .PP .Vb 1 \& my($value) = ${Config::Tiny \-> read_string(\*(Aqalpha=bet\*(Aq)}{_}{alpha}; # $value is \*(Aqbet\*(Aq. .Ve .SS "Can I use a file called '0' (zero)?" .IX Subsection "Can I use a file called '0' (zero)?" Yes. See t/05.zero.t (test code) and t/0 (test data). .SH CAVEATS .IX Header "CAVEATS" Some edge cases in section headers are not supported, and additionally may not be detected when writing the config file. .PP Specifically, section headers with leading whitespace, trailing whitespace, or newlines anywhere in the section header, will not be written correctly to the file and may cause file corruption. .SH Repository .IX Header "Repository" .SH SUPPORT .IX Header "SUPPORT" Bugs should be reported via the CPAN bug tracker at .PP .PP For other issues, or commercial enhancement or support, contact the author. .SH AUTHOR .IX Header "AUTHOR" Adam Kennedy .PP Maintanence from V 2.15: Ron Savage . .SH ACKNOWLEGEMENTS .IX Header "ACKNOWLEGEMENTS" Thanks to Sherzod Ruzmetov for Config::Simple, which inspired this module by being not quite "simple" enough for me :). .SH "SEE ALSO" .IX Header "SEE ALSO" See, amongst many: Config::Simple and Config::General. .PP See Config::Tiny::Ordered (and possibly others) for the preservation of the order of the entries in the file. .PP IOD. Ini On Drugs. .PP IOD::Examples .PP App::IODUtils .PP Config::IOD::Reader .PP Config::Perl::V. Config data from Perl itself. .PP Config::Onion .PP Config::IniFiles .PP Config::INIPlus .PP Config::Hash. Allows nested data. .PP Config::MVP. Author: RJBS. Uses Moose. Extremely complex. .PP Config::TOML. See next few lines: .PP .PP . 1 Star rating. .PP .SH COPYRIGHT .IX Header "COPYRIGHT" Copyright 2002 \- 2011 Adam Kennedy. .PP This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. .PP The full text of the license can be found in the LICENSE file included with this module.