/usr/share/perl5/Config/Tiny.pm is in libconfig-tiny-perl 2.14-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | package Config::Tiny;
# If you thought Config::Simple was small...
use strict;
BEGIN {
require 5.004;
$Config::Tiny::VERSION = '2.14';
$Config::Tiny::errstr = '';
}
# Create an empty object
sub new { bless {}, shift }
# Create an object from a file
sub read {
my $class = ref $_[0] ? ref shift : shift;
# Check the file
my $file = shift or return $class->_error( 'You did not specify a file name' );
return $class->_error( "File '$file' does not exist" ) unless -e $file;
return $class->_error( "'$file' is a directory, not a file" ) unless -f _;
return $class->_error( "Insufficient permissions to read '$file'" ) unless -r _;
# Slurp in the file
local $/ = undef;
open( CFG, $file ) or return $class->_error( "Failed to open file '$file': $!" );
my $contents = <CFG>;
close( CFG );
$class->read_string( $contents );
}
# Create an object from a string
sub read_string {
my $class = ref $_[0] ? ref shift : shift;
my $self = bless {}, $class;
return undef unless defined $_[0];
# Parse the file
my $ns = '_';
my $counter = 0;
foreach ( split /(?:\015{1,2}\012|\015|\012)/, shift ) {
$counter++;
# Skip comments and empty lines
next if /^\s*(?:\#|\;|$)/;
# Remove inline comments
s/\s\;\s.+$//g;
# Handle section headers
if ( /^\s*\[\s*(.+?)\s*\]\s*$/ ) {
# Create the sub-hash if it doesn't exist.
# Without this sections without keys will not
# appear at all in the completed struct.
$self->{$ns = $1} ||= {};
next;
}
# Handle properties
if ( /^\s*([^=]+?)\s*=\s*(.*?)\s*$/ ) {
$self->{$ns}->{$1} = $2;
next;
}
return $self->_error( "Syntax error at line $counter: '$_'" );
}
$self;
}
# Save an object to a file
sub write {
my $self = shift;
my $file = shift or return $self->_error(
'No file name provided'
);
# Write it to the file
my $string = $self->write_string;
return undef unless defined $string;
open( CFG, '>' . $file ) or return $self->_error(
"Failed to open file '$file' for writing: $!"
);
print CFG $string;
close CFG;
}
# Save an object to a string
sub write_string {
my $self = shift;
my $contents = '';
foreach my $section ( sort { (($b eq '_') <=> ($a eq '_')) || ($a cmp $b) } keys %$self ) {
# Check for several known-bad situations with the section
# 1. Leading whitespace
# 2. Trailing whitespace
# 3. Newlines in section name
return $self->_error(
"Illegal whitespace in section name '$section'"
) if $section =~ /(?:^\s|\n|\s$)/s;
my $block = $self->{$section};
$contents .= "\n" if length $contents;
$contents .= "[$section]\n" unless $section eq '_';
foreach my $property ( sort keys %$block ) {
return $self->_error(
"Illegal newlines in property '$section.$property'"
) if $block->{$property} =~ /(?:\012|\015)/s;
$contents .= "$property=$block->{$property}\n";
}
}
$contents;
}
# Error handling
sub errstr { $Config::Tiny::errstr }
sub _error { $Config::Tiny::errstr = $_[1]; undef }
1;
__END__
=pod
=head1 NAME
Config::Tiny - Read/Write .ini style files with as little code as possible
=head1 SYNOPSIS
# In your configuration file
rootproperty=blah
[section]
one=twp
three= four
Foo =Bar
empty=
# In your program
use Config::Tiny;
# Create a config
my $Config = Config::Tiny->new;
# Open the config
$Config = Config::Tiny->read( 'file.conf' );
# 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' );
=head1 DESCRIPTION
C<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 C<::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 L<Config::Simple>, L<Config::General> or one of
the many other C<Config::> modules. To rephrase, L<Config::Tiny> does B<not>
preserve your comments, whitespace, or the order of your config file.
=head1 CONFIGURATION FILE SYNTAX
Files are the same format as for 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 C<"root section">, available at C<$Config-E<gt>{_}>.
Lines starting with C<'#'> or C<';'> 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 is 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.
=head1 METHODS
=head2 new
The constructor C<new> creates and returns an empty C<Config::Tiny> object.
=head2 read $filename
The C<read> constructor reads a config file, and returns a new
C<Config::Tiny> object containing the properties in the file.
Returns the object on success, or C<undef> on error.
When C<read> fails, C<Config::Tiny> sets an error message internally
you can recover via C<Config::Tiny-E<gt>errstr>. Although in B<some>
cases a failed C<read> will also set the operating system error
variable C<$!>, not all errors do and you should not rely on using
the C<$!> variable.
=head2 read_string $string;
The C<read_string> method takes as argument the contents of a config file
as a string and returns the C<Config::Tiny> object for it.
=head2 write $filename
The C<write> method generates the file content for the properties, and
writes it to disk to the filename specified.
Returns true on success or C<undef> on error.
=head2 write_string
Generates the file content for the object and returns it as a string.
=head2 errstr
When an error occurs, you can retrieve the error message either from the
C<$Config::Tiny::errstr> variable, or using the C<errstr()> method.
=head1 CAVEATS
=head2 Unsupported Section Headers
Some edge cases in section headers are not support, 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.
=head1 SUPPORT
Bugs should be reported via the CPAN bug tracker at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Config-Tiny>
For other issues, or commercial enhancement or support, contact the author.
=head1 AUTHOR
Adam Kennedy E<lt>adamk@cpan.orgE<gt>
=head1 ACKNOWLEGEMENTS
Thanks to Sherzod Ruzmetov E<lt>sherzodr@cpan.orgE<gt> for
L<Config::Simple>, which inspired this module by being not quite
"simple" enough for me :)
=head1 SEE ALSO
L<Config::Simple>, L<Config::General>, L<ali.as>
=head1 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.
=cut
|