added a simple cache to the perl parser so it can load the same file twice (RT #28812)
[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
49 my $main = delete $config->{ _ };
50 my $out;
51 $out->{$_} = $main->{$_} for keys %$main;
52
53 for my $k (keys %$config) {
54 my @keys = split /\s+/, $k if $MAP_SECTION_SPACE_TO_NESTED_KEY;
55 my $ref = $config->{$k};
56
57 if (@keys > 1) {
58 my ($a, $b) = @keys[0,1];
59 $out->{$a}->{$b} = $ref;
60 } else {
61 $out->{$k} = $ref;
62 }
63 }
64 return $out;
65}
66
67=head1 PACKAGE VARIABLES
68
69=over 4
70
71=item $MAP_SECTION_SPACE_TO_NESTED_KEY (boolean)
72
73This variable controls whether spaces in INI section headings will be expanded into nested hash keys.
74e.g. it controls whether [Full Power] maps to $config->{'Full Power'} or $config->{'Full'}->{'Power'}
75
76By default it is set to 1 (i.e. true).
77
78Set it to 0 to preserve literal spaces in section headings:
79
80 use Config::Any;
81 use Config::Any::INI;
82 $Config::Any::INI::MAP_SECTION_SPACE_TO_NESTED_KEY = 0;
83
84=back
85
86=head1 AUTHOR
87
88=over 4
89
90=item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>
91
92=item * Joel Bernstein E<lt>rataxis@cpan.orgE<gt>
93
94=back
95
96=head1 COPYRIGHT AND LICENSE
97
98Copyright 2006 by Brian Cassidy, portions copyright 2006, 2007 by Joel Bernstein
99
100This library is free software; you can redistribute it and/or modify
101it under the same terms as Perl itself.
102
103=head1 SEE ALSO
104
105=over 4
106
107=item * L<Catalyst>
108
109=item * L<Config::Any>
110
111=item * L<Config::Tiny>
112
113=back
114
115=cut
116
1171;