File Coverage

blib/lib/Config/Any/INI.pm
Criterion Covered Total %
statement 23 23 100.0
branch 4 4 100.0
condition n/a
subroutine 4 4 100.0
pod 2 2 100.0
total 33 33 100.0


line stmt bran cond sub pod time code
1             package Config::Any::INI;
2              
3 6     6   106 use strict;
  6         57  
  6         93  
4 6     6   188 use warnings;
  6         58  
  6         93  
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 7     7 1 103     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 11     11 1 151     my $class = shift;
44 11         104     my $file = shift;
45              
46 11         2428     require Config::Tiny;
47 11         246     my $config = Config::Tiny->read( $file );
48              
49 11         127     my $main = delete $config->{ _ };
50 11         95 my $out;
51 11         89 $out->{$_} = $main->{$_} for keys %$main;
  11         166  
52              
53 11         131    for my $k (keys %$config) {
54 22 100       336 my @keys = split /\s+/, $k if $MAP_SECTION_SPACE_TO_NESTED_KEY;
55 22         222 my $ref = $config->{$k};
56              
57 22 100       232 if (@keys > 1) {
58 18         178 my ($a, $b) = @keys[0,1];
59 18         239 $out->{$a}->{$b} = $ref;
60             } else {
61 4         45 $out->{$k} = $ref;
62             }
63             }
64 11         369     return $out;
65             }
66              
67             =head1 PACKAGE VARIABLES
68            
69             =over 4
70            
71             =item $MAP_SECTION_SPACE_TO_NESTED_KEY (boolean)
72            
73             This variable controls whether spaces in INI section headings will be expanded into nested hash keys.
74             e.g. it controls whether [Full Power] maps to $config->{'Full Power'} or $config->{'Full'}->{'Power'}
75            
76             By default it is set to 1 (i.e. true).
77            
78             Set 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            
98             Copyright 2006 by Brian Cassidy, portions copyright 2006, 2007 by Joel Bernstein
99            
100             This library is free software; you can redistribute it and/or modify
101             it 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              
117             1;
118