Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / Config / Tiny.pm
1 package Config::Tiny;
2
3 # If you thought Config::Simple was small...
4
5 use strict;
6 BEGIN {
7         require 5.004;
8         $Config::Tiny::VERSION = '2.12';
9         $Config::Tiny::errstr  = '';
10 }
11
12 # Create an empty object
13 sub new { bless {}, shift }
14
15 # Create an object from a file
16 sub read {
17         my $class = ref $_[0] ? ref shift : shift;
18
19         # Check the file
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 _;
24
25         # Slurp in the file
26         local $/ = undef;
27         open CFG, $file or return $class->_error( "Failed to open file '$file': $!" );
28         my $contents = <CFG>;
29         close CFG;
30
31         $class->read_string( $contents );
32 }
33
34 # Create an object from a string
35 sub read_string {
36         my $class = ref $_[0] ? ref shift : shift;
37         my $self  = bless {}, $class;
38         return undef unless defined $_[0];
39
40         # Parse the file
41         my $ns      = '_';
42         my $counter = 0;
43         foreach ( split /(?:\015{1,2}\012|\015|\012)/, shift ) {
44                 $counter++;
45
46                 # Skip comments and empty lines
47                 next if /^\s*(?:\#|\;|$)/;
48
49                 # Remove inline comments
50                 s/\s\;\s.+$//g;
51
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} ||= {};
58                         next;
59                 }
60
61                 # Handle properties
62                 if ( /^\s*([^=]+?)\s*=\s*(.*?)\s*$/ ) {
63                         $self->{$ns}->{$1} = $2;
64                         next;
65                 }
66
67                 return $self->_error( "Syntax error at line $counter: '$_'" );
68         }
69
70         $self;
71 }
72
73 # Save an object to a file
74 sub write {
75         my $self = shift;
76         my $file = shift or return $self->_error(
77                 'No file name provided'
78                 );
79
80         # Write it to the file
81         open( CFG, '>' . $file ) or return $self->_error(
82                 "Failed to open file '$file' for writing: $!"
83                 );
84         print CFG $self->write_string;
85         close CFG;
86 }
87
88 # Save an object to a string
89 sub write_string {
90         my $self = shift;
91
92         my $contents = '';
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";
99                 }
100         }
101         
102         $contents;
103 }
104
105 # Error handling
106 sub errstr { $Config::Tiny::errstr }
107 sub _error { $Config::Tiny::errstr = $_[1]; undef }
108
109 1;
110
111 __END__
112
113 =pod
114
115 =head1 NAME
116
117 Config::Tiny - Read/Write .ini style files with as little code as possible
118
119 =head1 SYNOPSIS
120
121     # In your configuration file
122     rootproperty=blah
123
124     [section]
125     one=twp
126     three= four
127     Foo =Bar
128     empty=
129
130     # In your program
131     use Config::Tiny;
132
133     # Create a config
134     my $Config = Config::Tiny->new();
135
136     # Open the config
137     $Config = Config::Tiny->read( 'file.conf' );
138
139     # Reading properties
140     my $rootproperty = $Config->{_}->{rootproperty};
141     my $one = $Config->{section}->{one};
142     my $Foo = $Config->{section}->{Foo};
143
144     # Changing data
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
148
149     # Save a config
150     $Config->write( 'file.conf' );
151
152 =head1 DESCRIPTION
153
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.
159
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.
165
166 =head1 CONFIGURATION FILE SYNTAX
167
168 Files are the same format as for windows .ini files. For example:
169
170         [section]
171         var1=value1
172         var2=value2
173
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>{_}>.
176
177 Lines starting with C<'#'> or C<';'> are considered comments and ignored,
178 as are blank lines.
179
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.
184
185 =head1 METHODS
186
187 =head2 new
188
189 The constructor C<new> creates and returns an empty C<Config::Tiny> object.
190
191 =head2 read $filename
192
193 The C<read> constructor reads a config file, and returns a new
194 C<Config::Tiny> object containing the properties in the file. 
195
196 Returns the object on success, or C<undef> on error.
197
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
202 the C<$!> variable.
203
204 =head2 read_string $string;
205
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.
208
209 =head2 write $filename
210
211 The C<write> method generates the file content for the properties, and
212 writes it to disk to the filename specified.
213
214 Returns true on success or C<undef> on error.
215
216 =head2 write_string
217
218 Generates the file content for the object and returns it as a string.
219
220 =head2 errstr
221
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.
224
225 =head2 property_string
226
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.
229
230 =head2 set
231
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.
234
235 =head1 SUPPORT
236
237 Bugs should be reported via the CPAN bug tracker at
238
239 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Config-Tiny>
240
241 For other issues, or commercial enhancement or support, contact the author.
242
243 =head1 AUTHOR
244
245 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
246
247 =head1 ACKNOWLEGEMENTS
248
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 :)
252
253 =head1 SEE ALSO
254
255 L<Config::Simple>, L<Config::General>, L<ali.as>
256
257 =head1 COPYRIGHT
258
259 Copyright 2002 - 2007 Adam Kennedy.
260
261 This program is free software; you can redistribute
262 it and/or modify it under the same terms as Perl itself.
263
264 The full text of the license can be found in the
265 LICENSE file included with this module.
266
267 =cut