##// END OF EJS Templates
Added Config::Tiny as a local module....
Jani Honkonen -
r1150:27b7fe27c99d
parent child
Show More
@@ -0,0 +1,280
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.14';
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 my $string = $self->write_string;
82 return undef unless defined $string;
83 open( CFG, '>' . $file ) or return $self->_error(
84 "Failed to open file '$file' for writing: $!"
85 );
86 print CFG $string;
87 close CFG;
88 }
89
90 # Save an object to a string
91 sub write_string {
92 my $self = shift;
93
94 my $contents = '';
95 foreach my $section ( sort { (($b eq '_') <=> ($a eq '_')) || ($a cmp $b) } keys %$self ) {
96 # Check for several known-bad situations with the section
97 # 1. Leading whitespace
98 # 2. Trailing whitespace
99 # 3. Newlines in section name
100 return $self->_error(
101 "Illegal whitespace in section name '$section'"
102 ) if $section =~ /(?:^\s|\n|\s$)/s;
103 my $block = $self->{$section};
104 $contents .= "\n" if length $contents;
105 $contents .= "[$section]\n" unless $section eq '_';
106 foreach my $property ( sort keys %$block ) {
107 return $self->_error(
108 "Illegal newlines in property '$section.$property'"
109 ) if $block->{$property} =~ /(?:\012|\015)/s;
110 $contents .= "$property=$block->{$property}\n";
111 }
112 }
113
114 $contents;
115 }
116
117 # Error handling
118 sub errstr { $Config::Tiny::errstr }
119 sub _error { $Config::Tiny::errstr = $_[1]; undef }
120
121 1;
122
123 __END__
124
125 =pod
126
127 =head1 NAME
128
129 Config::Tiny - Read/Write .ini style files with as little code as possible
130
131 =head1 SYNOPSIS
132
133 # In your configuration file
134 rootproperty=blah
135
136 [section]
137 one=twp
138 three= four
139 Foo =Bar
140 empty=
141
142 # In your program
143 use Config::Tiny;
144
145 # Create a config
146 my $Config = Config::Tiny->new;
147
148 # Open the config
149 $Config = Config::Tiny->read( 'file.conf' );
150
151 # Reading properties
152 my $rootproperty = $Config->{_}->{rootproperty};
153 my $one = $Config->{section}->{one};
154 my $Foo = $Config->{section}->{Foo};
155
156 # Changing data
157 $Config->{newsection} = { this => 'that' }; # Add a section
158 $Config->{section}->{Foo} = 'Not Bar!'; # Change a value
159 delete $Config->{_}; # Delete a value or section
160
161 # Save a config
162 $Config->write( 'file.conf' );
163
164 =head1 DESCRIPTION
165
166 C<Config::Tiny> is a perl class to read and write .ini style configuration
167 files with as little code as possible, reducing load time and memory
168 overhead. Most of the time it is accepted that Perl applications use a lot
169 of memory and modules. The C<::Tiny> family of modules is specifically
170 intended to provide an ultralight alternative to the standard modules.
171
172 This module is primarily for reading human written files, and anything we
173 write shouldn't need to have documentation/comments. If you need something
174 with more power move up to L<Config::Simple>, L<Config::General> or one of
175 the many other C<Config::> modules. To rephrase, L<Config::Tiny> does B<not>
176 preserve your comments, whitespace, or the order of your config file.
177
178 =head1 CONFIGURATION FILE SYNTAX
179
180 Files are the same format as for windows .ini files. For example:
181
182 [section]
183 var1=value1
184 var2=value2
185
186 If a property is outside of a section at the beginning of a file, it will
187 be assigned to the C<"root section">, available at C<$Config-E<gt>{_}>.
188
189 Lines starting with C<'#'> or C<';'> are considered comments and ignored,
190 as are blank lines.
191
192 When writing back to the config file, all comments, custom whitespace,
193 and the ordering of your config file elements is discarded. If you need
194 to keep the human elements of a config when writing back, upgrade to
195 something better, this module is not for you.
196
197 =head1 METHODS
198
199 =head2 new
200
201 The constructor C<new> creates and returns an empty C<Config::Tiny> object.
202
203 =head2 read $filename
204
205 The C<read> constructor reads a config file, and returns a new
206 C<Config::Tiny> object containing the properties in the file.
207
208 Returns the object on success, or C<undef> on error.
209
210 When C<read> fails, C<Config::Tiny> sets an error message internally
211 you can recover via C<Config::Tiny-E<gt>errstr>. Although in B<some>
212 cases a failed C<read> will also set the operating system error
213 variable C<$!>, not all errors do and you should not rely on using
214 the C<$!> variable.
215
216 =head2 read_string $string;
217
218 The C<read_string> method takes as argument the contents of a config file
219 as a string and returns the C<Config::Tiny> object for it.
220
221 =head2 write $filename
222
223 The C<write> method generates the file content for the properties, and
224 writes it to disk to the filename specified.
225
226 Returns true on success or C<undef> on error.
227
228 =head2 write_string
229
230 Generates the file content for the object and returns it as a string.
231
232 =head2 errstr
233
234 When an error occurs, you can retrieve the error message either from the
235 C<$Config::Tiny::errstr> variable, or using the C<errstr()> method.
236
237 =head1 CAVEATS
238
239 =head2 Unsupported Section Headers
240
241 Some edge cases in section headers are not support, and additionally may not
242 be detected when writing the config file.
243
244 Specifically, section headers with leading whitespace, trailing whitespace,
245 or newlines anywhere in the section header, will not be written correctly
246 to the file and may cause file corruption.
247
248 =head1 SUPPORT
249
250 Bugs should be reported via the CPAN bug tracker at
251
252 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Config-Tiny>
253
254 For other issues, or commercial enhancement or support, contact the author.
255
256 =head1 AUTHOR
257
258 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
259
260 =head1 ACKNOWLEGEMENTS
261
262 Thanks to Sherzod Ruzmetov E<lt>sherzodr@cpan.orgE<gt> for
263 L<Config::Simple>, which inspired this module by being not quite
264 "simple" enough for me :)
265
266 =head1 SEE ALSO
267
268 L<Config::Simple>, L<Config::General>, L<ali.as>
269
270 =head1 COPYRIGHT
271
272 Copyright 2002 - 2011 Adam Kennedy.
273
274 This program is free software; you can redistribute
275 it and/or modify it under the same terms as Perl itself.
276
277 The full text of the license can be found in the
278 LICENSE file included with this module.
279
280 =cut
@@ -1,6 +1,6
1 package Jobs;
1 package Jobs;
2 use File::Basename;
2 use File::Basename;
3 use Config::Tiny;
3 use Tiny;
4
4
5 sub get {
5 sub get {
6 my $inifile = shift;
6 my $inifile = shift;
General Comments 0
You need to be logged in to leave comments. Login now