Commit | Line | Data |
f0e3c221 |
1 | package Config::Any::INI; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | our $MAP_SECTION_SPACE_TO_NESTED_KEY = 1; |
7 | |
8 | =head1 NAME |
9 | |
10 | Config::Any::INI - Load INI config files |
11 | |
12 | =head1 DESCRIPTION |
13 | |
14 | Loads 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 | |
28 | return an array of valid extensions (C<ini>). |
29 | |
30 | =cut |
31 | |
32 | sub extensions { |
33 | return qw( ini ); |
34 | } |
35 | |
36 | =head2 load( $file ) |
37 | |
38 | Attempts to load C<$file> as an INI file. |
39 | |
40 | =cut |
41 | |
42 | sub load { |
43 | my $class = shift; |
44 | my $file = shift; |
45 | |
46 | require Config::Tiny; |
47 | my $config = Config::Tiny->read( $file ); |
5a2e0210 |
48 | my $out = delete $config->{ _ } || {}; |
f0e3c221 |
49 | |
92a04e78 |
50 | for my $k ( keys %$config ) { |
5a2e0210 |
51 | my @keys = split /\s+/, $k; |
92a04e78 |
52 | my $ref = $config->{ $k }; |
f0e3c221 |
53 | |
5a2e0210 |
54 | if ( $MAP_SECTION_SPACE_TO_NESTED_KEY && @keys > 1 ) { |
92a04e78 |
55 | my ( $a, $b ) = @keys[ 0, 1 ]; |
56 | $out->{ $a }->{ $b } = $ref; |
57 | } |
58 | else { |
5a2e0210 |
59 | $out->{ $k } = { %{ $out->{ $k } || {} }, %$ref }; |
f0e3c221 |
60 | } |
61 | } |
5a2e0210 |
62 | |
f0e3c221 |
63 | return $out; |
64 | } |
65 | |
72628dc7 |
66 | =head2 is_supported( ) |
67 | |
68 | Returns true if L<Config::Tiny> is available. |
69 | |
70 | =cut |
71 | |
72 | sub is_supported { |
73 | eval { require Config::Tiny; }; |
74 | return $@ ? 0 : 1; |
75 | } |
76 | |
f0e3c221 |
77 | =head1 PACKAGE VARIABLES |
78 | |
79 | =over 4 |
80 | |
81 | =item $MAP_SECTION_SPACE_TO_NESTED_KEY (boolean) |
82 | |
83 | This variable controls whether spaces in INI section headings will be expanded into nested hash keys. |
84 | e.g. it controls whether [Full Power] maps to $config->{'Full Power'} or $config->{'Full'}->{'Power'} |
85 | |
86 | By default it is set to 1 (i.e. true). |
87 | |
88 | Set it to 0 to preserve literal spaces in section headings: |
89 | |
90 | use Config::Any; |
91 | use Config::Any::INI; |
92 | $Config::Any::INI::MAP_SECTION_SPACE_TO_NESTED_KEY = 0; |
93 | |
94 | =back |
95 | |
a918b0b8 |
96 | =head1 AUTHORS |
f0e3c221 |
97 | |
a918b0b8 |
98 | Brian Cassidy E<lt>bricas@cpan.orgE<gt> |
f0e3c221 |
99 | |
a918b0b8 |
100 | Joel Bernstein E<lt>rataxis@cpan.orgE<gt> |
f0e3c221 |
101 | |
102 | =head1 COPYRIGHT AND LICENSE |
103 | |
a918b0b8 |
104 | Copyright 2007 by Brian Cassidy, portions copyright 2006, 2007 by Joel Bernstein |
f0e3c221 |
105 | |
106 | This library is free software; you can redistribute it and/or modify |
107 | it under the same terms as Perl itself. |
108 | |
109 | =head1 SEE ALSO |
110 | |
111 | =over 4 |
112 | |
113 | =item * L<Catalyst> |
114 | |
115 | =item * L<Config::Any> |
116 | |
117 | =item * L<Config::Tiny> |
118 | |
119 | =back |
120 | |
121 | =cut |
122 | |
123 | 1; |