3 # If you thought Config::Simple was small...
8 $Config::Tiny::VERSION = '2.12';
9 $Config::Tiny::errstr = '';
12 # Create an empty object
13 sub new { bless {}, shift }
15 # Create an object from a file
17 my $class = ref $_[0] ? ref shift : shift;
20 my $file = shift or return $class->_error( 'You did not specify a file name' );
21 return $class->_error( "File '$file' does not exist" ) unless -e $file;
22 return $class->_error( "'$file' is a directory, not a file" ) unless -f _;
23 return $class->_error( "Insufficient permissions to read '$file'" ) unless -r _;
27 open CFG, $file or return $class->_error( "Failed to open file '$file': $!" );
31 $class->read_string( $contents );
34 # Create an object from a string
36 my $class = ref $_[0] ? ref shift : shift;
37 my $self = bless {}, $class;
38 return undef unless defined $_[0];
43 foreach ( split /(?:\015{1,2}\012|\015|\012)/, shift ) {
46 # Skip comments and empty lines
47 next if /^\s*(?:\#|\;|$)/;
49 # Remove inline comments
52 # Handle section headers
53 if ( /^\s*\[\s*(.+?)\s*\]\s*$/ ) {
54 # Create the sub-hash if it doesn't exist.
55 # Without this sections without keys will not
56 # appear at all in the completed struct.
57 $self->{$ns = $1} ||= {};
62 if ( /^\s*([^=]+?)\s*=\s*(.*?)\s*$/ ) {
63 $self->{$ns}->{$1} = $2;
67 return $self->_error( "Syntax error at line $counter: '$_'" );
73 # Save an object to a file
76 my $file = shift or return $self->_error(
77 'No file name provided'
80 # Write it to the file
81 open( CFG, '>' . $file ) or return $self->_error(
82 "Failed to open file '$file' for writing: $!"
84 print CFG $self->write_string;
88 # Save an object to a string
93 foreach my $section ( sort { (($b eq '_') <=> ($a eq '_')) || ($a cmp $b) } keys %$self ) {
94 my $block = $self->{$section};
95 $contents .= "\n" if length $contents;
96 $contents .= "[$section]\n" unless $section eq '_';
97 foreach my $property ( sort keys %$block ) {
98 $contents .= "$property=$block->{$property}\n";
106 sub errstr { $Config::Tiny::errstr }
107 sub _error { $Config::Tiny::errstr = $_[1]; undef }
117 Config::Tiny - Read/Write .ini style files with as little code as possible
121 # In your configuration file
134 my $Config = Config::Tiny->new();
137 $Config = Config::Tiny->read( 'file.conf' );
140 my $rootproperty = $Config->{_}->{rootproperty};
141 my $one = $Config->{section}->{one};
142 my $Foo = $Config->{section}->{Foo};
145 $Config->{newsection} = { this => 'that' }; # Add a section
146 $Config->{section}->{Foo} = 'Not Bar!'; # Change a value
147 delete $Config->{_}; # Delete a value or section
150 $Config->write( 'file.conf' );
154 C<Config::Tiny> is a perl class to read and write .ini style configuration
155 files with as little code as possible, reducing load time and memory
156 overhead. Most of the time it is accepted that Perl applications use a lot
157 of memory and modules. The C<::Tiny> family of modules is specifically
158 intended to provide an ultralight alternative to the standard modules.
160 This module is primarily for reading human written files, and anything we
161 write shouldn't need to have documentation/comments. If you need something
162 with more power move up to L<Config::Simple>, L<Config::General> or one of
163 the many other C<Config::> modules. To rephrase, L<Config::Tiny> does B<not>
164 preserve your comments, whitespace, or the order of your config file.
166 =head1 CONFIGURATION FILE SYNTAX
168 Files are the same format as for windows .ini files. For example:
174 If a property is outside of a section at the beginning of a file, it will
175 be assigned to the C<"root section">, available at C<$Config-E<gt>{_}>.
177 Lines starting with C<'#'> or C<';'> are considered comments and ignored,
180 When writing back to the config file, all comments, custom whitespace,
181 and the ordering of your config file elements is discarded. If you need
182 to keep the human elements of a config when writing back, upgrade to
183 something better, this module is not for you.
189 The constructor C<new> creates and returns an empty C<Config::Tiny> object.
191 =head2 read $filename
193 The C<read> constructor reads a config file, and returns a new
194 C<Config::Tiny> object containing the properties in the file.
196 Returns the object on success, or C<undef> on error.
198 When C<read> fails, C<Config::Tiny> sets an error message internally
199 you can recover via C<<Config::Tiny->errstr>>. Although in B<some>
200 cases a failed C<read> will also set the operating system error
201 variable C<$!>, not all errors do and you should not rely on using
204 =head2 read_string $string;
206 The C<read_string> method takes as argument the contents of a config file
207 as a string and returns the C<Config::Tiny> object for it.
209 =head2 write $filename
211 The C<write> method generates the file content for the properties, and
212 writes it to disk to the filename specified.
214 Returns true on success or C<undef> on error.
218 Generates the file content for the object and returns it as a string.
222 When an error occurs, you can retrieve the error message either from the
223 C<$Config::Tiny::errstr> variable, or using the C<errstr()> method.
225 =head2 property_string
227 This method is called to produce the string used to represent the property in a
228 section. It is passed the section name and property name.
232 This is a convenience is called to set a value found in the parsed config string. It is
233 passed the section name, property name, and value.
237 Bugs should be reported via the CPAN bug tracker at
239 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Config-Tiny>
241 For other issues, or commercial enhancement or support, contact the author.
245 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
247 =head1 ACKNOWLEGEMENTS
249 Thanks to Sherzod Ruzmetov E<lt>sherzodr@cpan.orgE<gt> for
250 L<Config::Simple>, which inspired this module by being not quite
251 "simple" enough for me :)
255 L<Config::Simple>, L<Config::General>, L<ali.as>
259 Copyright 2002 - 2007 Adam Kennedy.
261 This program is free software; you can redistribute
262 it and/or modify it under the same terms as Perl itself.
264 The full text of the license can be found in the
265 LICENSE file included with this module.