phpman > perldoc > Config::Tiny(3pm)

Markdown | JSON | MCP    

NAME
    Config::Tiny - Read/Write .ini style files with as little code as possible

SYNOPSIS
            # In your configuration file
            rootproperty=blah

            [section]
            one=twp
            three= four
            Foo =Bar
            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( 'file.conf' );
            $Config = Config::Tiny->read( 'file.conf', 'utf8' ); # Neither ':' nor '<:' prefix!
            $Config = Config::Tiny->read( 'file.conf', 'encoding(iso-8859-1)');

            # Reading properties
            my $rootproperty = $Config->{_}->{rootproperty};
            my $one = $Config->{section}->{one};
            my $Foo = $Config->{section}->{Foo};

            # Changing data
            $Config->{newsection} = { this => 'that' }; # Add a section
            $Config->{section}->{Foo} = 'Not Bar!';     # Change a value
            delete $Config->{_};                        # Delete a value or section

            # Save a config
            $Config->write( 'file.conf' );
            $Config->write( 'file.conf', 'utf8' ); # Neither ':' nor '>:' prefix!

            # Shortcuts
            my($rootproperty) = $$Config{_}{rootproperty};

            my($config) = Config::Tiny -> read_string('alpha=bet');
            my($value)  = $$config{_}{alpha}; # $value is 'bet'.

            my($config) = Config::Tiny -> read_string("[init]\nalpha=bet");
            my($value)  = $$config{init}{alpha}; # $value is 'bet'.

DESCRIPTION
    "Config::Tiny" is a Perl class to read and write .ini style configuration files with as little
    code as possible, reducing load time and memory overhead.

    Most of the time it is accepted that Perl applications use a lot of memory and modules.

    The *::Tiny family of modules is specifically intended to provide an ultralight alternative to
    the standard modules.

    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 "Config::*" modules.

    Lastly, Config::Tiny does not preserve your comments, whitespace, or the order of your config
    file.

    See Config::Tiny::Ordered (and possibly others) for the preservation of the order of the entries
    in the file.

CONFIGURATION FILE SYNTAX
    Files are the same format as for MS Windows "*.ini" files. For example:

            [section]
            var1=value1
            var2=value2

    If a property is outside of a section at the beginning of a file, it will be assigned to the
    "root section", available at "$Config->{_}".

    Lines starting with '#' or ';' are considered comments and ignored, as are blank lines.

    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.

METHODS
  errstr()
    Returns a string representing the most recent error, or the empty string.

    You can also retrieve the error message from the $Config::Tiny::errstr variable.

  new([$config])
    Here, the [] indicate an optional parameter.

    The constructor "new" creates and returns a "Config::Tiny" object.

    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 "_" is treated specially as the root
    section.

  read($filename, [$encoding])
    Here, the [] indicate an optional parameter.

    The "read" constructor reads a config file, $filename, and returns a new "Config::Tiny" object
    containing the properties in the file.

    $encoding may be used to indicate the encoding of the file, e.g. 'utf8' or
    'encoding(iso-8859-1)'.

    Do not add a prefix to $encoding, such as '<' or '<:'.

    Returns the object on success, or "undef" on error.

    When "read" fails, "Config::Tiny" sets an error message internally you can recover via
    "Config::Tiny->errstr". Although in some cases a failed "read" will also set the operating
    system error variable $!, not all errors do and you should not rely on using the $! variable.

    See t/04.utf8.t and t/04.utf8.txt.

  read_string($string)
    The "read_string" method takes as argument the contents of a config file as a string and returns
    the "Config::Tiny" object for it.

  write($filename, [$encoding])
    Here, the [] indicate an optional parameter.

    The "write" method generates the file content for the properties, and writes it to disk to the
    filename specified.

    $encoding may be used to indicate the encoding of the file, e.g. 'utf8' or
    'encoding(iso-8859-1)'.

    Do not add a prefix to $encoding, such as '>' or '>:'.

    Returns true on success or "undef" on error.

    See t/04.utf8.t and t/04.utf8.txt.

  write_string()
    Generates the file content for the object and returns it as a string.

FAQ
  What happens if a key is repeated?
    The last value is retained, overwriting any previous values.

    See t/06.repeat.key.t.

  Why can't I put comments at the ends of lines?
    o The # char is only introduces a comment when it's at the start of a line.
        So a line like:

                key=value # A comment

        Sets key to 'value # A comment', which, presumably, you did not intend.

        This conforms to the syntax discussed in "CONFIGURATION FILE SYNTAX".

    o Comments matching /\s\;\s.+$//g; are ignored.
        This means you can't preserve the suffix using:

                key = Prefix ; Suffix

        Result: key is now 'Prefix'.

        But you can do this:

                key = Prefix;Suffix

        Result: key is now 'Prefix;Suffix'.

        Or this:

                key = Prefix; Suffix

        Result: key is now 'Prefix; Suffix'.

    See t/07.trailing.comment.t.

  Why can't I omit the '=' signs?
    E.g.:

            [Things]
            my =
            list =
            of =
            things =

    Instead of:

            [Things]
            my
            list
            of
            things

    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.

  Why do I have to assign the result of a method call to a variable?
    This question comes from RT#85386.

    Yes, the syntax may seem odd, but you don't have to call both new() and read_string().

    Try:

            perl -MData::Dumper -MConfig::Tiny -E 'my $c=Config::Tiny->read_string("one=s"); say Dumper $c'

    Or:

            my($config) = Config::Tiny -> read_string('alpha=bet');
            my($value)  = $$config{_}{alpha}; # $value is 'bet'.

    Or even, a bit ridiculously:

            my($value) = ${Config::Tiny -> read_string('alpha=bet')}{_}{alpha}; # $value is 'bet'.

  Can I use a file called '0' (zero)?
    Yes. See t/05.zero.t (test code) and t/0 (test data).

CAVEATS
    Some edge cases in section headers are not supported, and additionally may not be detected when
    writing the config file.

    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.

Repository
    <https://github.com/ronsavage/Config-Tiny.git>

SUPPORT
    Bugs should be reported via the CPAN bug tracker at

    <https://github.com/ronsavage/Config-Tiny/issues>

    For other issues, or commercial enhancement or support, contact the author.

AUTHOR
    Adam Kennedy <adamk AT cpan.org>

    Maintanence from V 2.15: Ron Savage <http://savage.net.au/>.

ACKNOWLEGEMENTS
    Thanks to Sherzod Ruzmetov <sherzodr AT cpan.org> for Config::Simple, which inspired this module by
    being not quite "simple" enough for me :).

SEE ALSO
    See, amongst many: Config::Simple and Config::General.

    See Config::Tiny::Ordered (and possibly others) for the preservation of the order of the entries
    in the file.

    IOD. Ini On Drugs.

    IOD::Examples

    App::IODUtils

    Config::IOD::Reader

    Config::Perl::V. Config data from Perl itself.

    Config::Onion

    Config::IniFiles

    Config::INIPlus

    Config::Hash. Allows nested data.

    Config::MVP. Author: RJBS. Uses Moose. Extremely complex.

    Config::TOML. See next few lines:

    <https://github.com/dlc/toml>

    <https://github.com/alexkalderimis/config-toml.pl>. 1 Star rating.

    <https://github.com/toml-lang/toml>

COPYRIGHT
    Copyright 2002 - 2011 Adam Kennedy.

    This program is free software; you can redistribute it and/or modify it under the same terms as
    Perl itself.

    The full text of the license can be found in the LICENSE file included with this module.

Config::Tiny(3pm)
NAME SYNOPSIS DESCRIPTION CONFIGURATION FILE SYNTAX METHODS FAQ CAVEATS Repository SUPPORT AUTHOR ACKNOWLEGEMENTS SEE ALSO COPYRIGHT

Generated by phpman v3.7.12 Author: Che Dong Under GNU General Public License
2026-06-13 14:42 @216.73.216.28
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 TransitionalValid CSS!

^_back to top