refactor _load(). fix an issue with use_ext => 0. use_ext now on by default. throw...
[p5sagit/Config-Any.git] / lib / Config / Any / INI.pm
CommitLineData
f0e3c221 1package Config::Any::INI;
2
3use strict;
4use warnings;
5
6our $MAP_SECTION_SPACE_TO_NESTED_KEY = 1;
7
8=head1 NAME
9
10Config::Any::INI - Load INI config files
11
12=head1 DESCRIPTION
13
14Loads INI files. Example:
15
16 name=TestApp
17
18 [Controller::Foo]
19 foo=bar
20
21 [Model::Baz]
22 qux=xyzzy
23
24=head1 METHODS
25
26=head2 extensions( )
27
28return an array of valid extensions (C<ini>).
29
30=cut
31
32sub extensions {
33 return qw( ini );
34}
35
36=head2 load( $file )
37
38Attempts to load C<$file> as an INI file.
39
40=cut
41
42sub load {
43 my $class = shift;
44 my $file = shift;
45
46 require Config::Tiny;
47 my $config = Config::Tiny->read( $file );
48
92a04e78 49 my $main = delete $config->{ _ };
f0e3c221 50 my $out;
92a04e78 51 $out->{ $_ } = $main->{ $_ } for keys %$main;
f0e3c221 52
92a04e78 53 for my $k ( keys %$config ) {
f0e3c221 54 my @keys = split /\s+/, $k if $MAP_SECTION_SPACE_TO_NESTED_KEY;
92a04e78 55 my $ref = $config->{ $k };
f0e3c221 56
92a04e78 57 if ( @keys > 1 ) {
58 my ( $a, $b ) = @keys[ 0, 1 ];
59 $out->{ $a }->{ $b } = $ref;
60 }
61 else {
62 $out->{ $k } = $ref;
f0e3c221 63 }
64 }
65 return $out;
66}
67
68=head1 PACKAGE VARIABLES
69
70=over 4
71
72=item $MAP_SECTION_SPACE_TO_NESTED_KEY (boolean)
73
74This variable controls whether spaces in INI section headings will be expanded into nested hash keys.
75e.g. it controls whether [Full Power] maps to $config->{'Full Power'} or $config->{'Full'}->{'Power'}
76
77By default it is set to 1 (i.e. true).
78
79Set it to 0 to preserve literal spaces in section headings:
80
81 use Config::Any;
82 use Config::Any::INI;
83 $Config::Any::INI::MAP_SECTION_SPACE_TO_NESTED_KEY = 0;
84
85=back
86
a918b0b8 87=head1 AUTHORS
f0e3c221 88
a918b0b8 89Brian Cassidy E<lt>bricas@cpan.orgE<gt>
f0e3c221 90
a918b0b8 91Joel Bernstein E<lt>rataxis@cpan.orgE<gt>
f0e3c221 92
93=head1 COPYRIGHT AND LICENSE
94
a918b0b8 95Copyright 2007 by Brian Cassidy, portions copyright 2006, 2007 by Joel Bernstein
f0e3c221 96
97This library is free software; you can redistribute it and/or modify
98it under the same terms as Perl itself.
99
100=head1 SEE ALSO
101
102=over 4
103
104=item * L<Catalyst>
105
106=item * L<Config::Any>
107
108=item * L<Config::Tiny>
109
110=back
111
112=cut
113
1141;